【发布时间】: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类。另外,突出显示错误发生的位置。