【问题标题】:Cannot implicitly convert type 'string' to 'System.Type'无法将类型“字符串”隐式转换为“System.Type”
【发布时间】:2015-07-28 05:46:58
【问题描述】:

我正在尝试在控制台应用程序中构建联系人管理器程序,使用列表来存储和显示数据。我需要查看显示可用联系人摘要的报告,然后有一个菜单允许用户与程序进行交互。我有一种方法可以创建一个包含数据和联系人对象的列表,但我不断收到错误无法在我的 createContact() 方法中将类型“字符串”隐式转换为“System.Type”。我不确定如何解决这个问题。

任何指导将不胜感激

     public static void createContact()
    {
        Contact c1 = new Contact();
        Console.WriteLine("\nGetFirstName");
        c1.GetFirstName = Console.ReadLine();
        Console.WriteLine("\nGetLastName");
        c1.GetLastName = Console.ReadLine();
        Console.WriteLine("\nGetEmailAddress");
        c1.GetEmailAddress = Console.ReadLine();
        Console.WriteLine("\nGetPhoneNumber");
        c1.GetPhoneNumber = Console.ReadLine();
        Console.WriteLine("\nContactTypes");
        c1.ContactTypes = Console.ReadLine();

        //Create more contacts...

        //Add all contacts here
        ContactCollection contactList = new ContactCollection();
        contactList.Add(c1);

        //Loop through list
        foreach (Contact c in contactList)
        {
            Console.WriteLine(c.GetFirstName);
            Console.WriteLine(c.GetLastName);
            Console.WriteLine(c.GetEmailAddress);
            Console.WriteLine(c.GetPhoneNumber);
           // error line
            Console.WriteLine(c.ContactTypes);

        }

        Console.ReadLine();

    }

这是我的联系方式

class Contact
{

    //private member variables
    private String _firstName;
    private String _lastName;
    private Type _contactTypes;
    private String _phoneNumber;
    private String _emailAddress;




    //Public constructor that takes five arguments
    public Contact()
    {
        //Call the appropriate setter (e.g. FirstName) to set the member variable value
        /*GetFirstName = firstName;
        GetLastName = lastName;
        ContactTypes = contactTypes;
        GetPhoneNumber = phoneNumber;
        GetEmailAddress = emailAddress;*/

    }


    /*********************************************************************
     * Public accessors used to get and set private member variable values
     *********************************************************************/
    //Public  ContactTypes accessor
    public Type ContactTypes
    {
        get
        {
            //Return member variable value
            return _contactTypes;
        }
        set
        {
              //Validate value and throw exception if necessary
            if (value == null)
                throw new Exception("ContactType must have a value");
            else
                //Otherwise set member variable value*/
                _contactTypes = value;
        }
    }
    enum ContactTypesEnum { Family, Friend, Professional }
    //Public FirstName accessor: Pascal casing
    public String GetFirstName
    {
        get
        {
            //Return member variable value
            return _firstName;
        }
        set
        {
            //Validate value and throw exception if necessary
            if (value == "")
                throw new Exception("First name must have a value");
            else
                //Otherwise set member variable value
                _firstName = value;
        }
    }

    //Public LastName accessor: Pascal casing
    public String GetLastName
    {
        get
        {
            //Return member variable value
            return _lastName;
        }
        set
        {
            //Validate value and throw exception if necessary
            if (value == "")
                throw new Exception("Last name must have a value");
            else
                //Otherwise set member variable value
                _lastName = value;
        }
    }



    //Public PhoneNumber accessor
    public String GetPhoneNumber
    {
        get
        {
            //Return member variable value
            return _phoneNumber;
        }
        set
        {
            bool isValid = Regex.IsMatch(value, @"/d{3}-/d{3}-/d{4}"); 
            //Validate value and throw exception if necessary
            if (value == "")
                throw new Exception("PhoneNumber must have a value");
            else
                //Otherwise set member variable value
                _phoneNumber = value;
        }
    }



    //Public Email accessor
    public String GetEmailAddress
    {
        get
        {
            //Return member variable value
            return _emailAddress;
        }
        set
        {
            //Validate value and throw exception if necessary
            if (value == "")
                throw new Exception("EmailAddress must have a value");
            else
                //Otherwise set member variable value
                _emailAddress = value;
        }
    }

}

【问题讨论】:

  • 您的Contact 班级是什么样的?您正在尝试将 string (这就是 Console.ReadLine() 返回的内容)分配给它的所有属性。都是string吗?
  • 你还没有发布有这个问题的代码,你至少要发布Contact类。另外,突出显示错误发生的位置。

标签: c# .net enums


【解决方案1】:

确保您的联系人类属性(GetFirstName、GetLastName 等)是字符串, 或将 input(Console.ReadLine()) 值转换为所需类型。

【讨论】:

    【解决方案2】:

    Console.ReadLine() 只返回一个字符串。如果您想创建任何对象,即:ContactTypes,您应该使用该字符串创建其实例。

    ContactTypes 应该是枚举而不是类型。

    public Type ContactTypes 应该是public ContactTypes ContactTypes

    enum ContactTypesEnum { Family, Friend, Professional } 应该是enum ContactTypes { Family, Friend, Professional }

    您应该执行以下操作。最后一个参数是ignoreCare,我设置为true

    c1.ContactTypes = (ContactTypes) Enum.Parse(typeof(ContactTypes ), Console.ReadLine(), true);
    

    【讨论】:

      【解决方案3】:

      我认为你打错了 Contact 类

      也许

      public Type ContactTypes
      

      应该是这样的:

      public ContactTypesEnum Type 
      

      还有

      private Type _contactTypes;
      

      像这样:

      private ContactTypesEnum _contactTypes;
      

      【讨论】:

        【解决方案4】:

        Console.ReadLine() 返回一个字符串。 你想要的是从 Readline() 返回的类型。

        c1.ContactTypes = Console.ReadLine().GetType();
        

        但这没有任何意义,因为 Console.Readline 将永远是“Sytem.String”。您可能需要将返回的值(字符串)转换为您喜欢的对象。

        var currString = Console.ReadLine().GetType();
        object currObject = currString;
        if(//Check if numeric for ex.)
        {
            currObject = Convert.ToInt32(currString);     
        }
        //Do some more validation
        //Now getType()
        c1.ContactTypes = Console.ReadLine();
        

        另见 CharithJ 和 Chandrashekar Jupalli 的回答

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-09
          • 2014-05-15
          • 2014-02-04
          • 2011-05-28
          相关资源
          最近更新 更多