简体   繁体   English

无法将类型'string'隐式转换为'System.Type'

[英]Cannot implicitly convert type 'string' to 'System.Type'

I am trying to build a contact managers program in a console application using a list to store and display the data. 我试图在控制台应用程序中使用列表存储和显示数据来构建联系人管理器程序。 I need to view a report that displays a summary of contacts available and then have a menu to allow the user to interact with the program. 我需要查看一个报告,该报告显示可用联系人的摘要,然后有一个菜单,允许用户与程序进行交互。 I have a method to create a list with data and a contact object but I keep getting the error Cannot implicitly convert type 'string' to 'System.Type' in my createContact() method. 我有一个用数据和联系人对象创建列表的方法,但是我一直收到错误消息:在我的createContact()方法中,无法将类型'string'隐式转换为'System.Type'。 I am not sure how to fix this. 我不确定如何解决此问题。

any guidance would be appreciated 任何指导将不胜感激

     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();

    }

Here is my contact class 这是我的联系方式

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;
        }
    }

}

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

Console.ReadLine() just returns a string. Console.ReadLine()仅返回一个字符串。 If you want to create any object ie: ContactTypes you should create its instance by using that string. 如果要创建任何对象,例如: ContactTypes ,则应使用该字符串创建其实例。

ContactTypes should be an enum not a Type. ContactTypes应该是一个枚举而不是Type。

public Type ContactTypes should be public ContactTypes ContactTypes . public Type ContactTypes应该是public ContactTypes ContactTypes

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

You should do something like below. 您应该执行以下操作。 Last parameter is ignoreCare which I've set to true . 最后一个参数是ignoreCare ,我将其设置为true

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

I think you miss-typed the Contact class 我认为您错过了Contact类的输入

Perhaps 也许

public Type ContactTypes

should be like this instead: 应该是这样的:

public ContactTypesEnum Type 

And

private Type _contactTypes;

like this: 像这样:

private ContactTypesEnum _contactTypes;

Console.ReadLine() returns a string. Console.ReadLine()返回一个字符串。 What you want is the type which gets returned from Readline(). 您想要的是从Readline()返回的类型。

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

But this doesnt make any sense, because Console.Readline will always be "Sytem.String". 但这没有任何意义,因为Console.Readline始终为“ Sytem.String”。 You might have to Convert the returned value (string) to the object you prefer. 您可能必须将返回的值(字符串)转换为所需的对象。

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();

Also see CharithJ and Chandrashekar Jupalli answer 另请参阅CharithJ和Chandrashekar Jupalli的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM