问题描述
我正在尝试使用列表来存储和显示数据,从而在控制台应用程序中构建联系人管理器程序。我需要查看显示可用联系人摘要的报告,然后有一个菜单允许用户与该程序进行交互。我有一个方法来创建一个联系人和一个联系人对象。我还有一种更新联系人的方法,但我想让用户能够选择联系人的名字,姓氏,电子邮件地址,电话号码,类型,并能够使用用户输入的信息进行更新。但是在这一行c1.GetContactType =(ContactTypes)Enum.Parse(typeof(ContactTypes),Console.ReadLine(),true);我一直收到错误:
无法找到类型或命名空间名称'ContactTypes'(您是否缺少using指令或程序集引用?)
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 contact and a contact object. I also have a method to update a contact but I want to have the user to be able to pick a contact first name, last name, email address, phone number, type and be able to update with the information the user entered. However on this line c1.GetContactType = (ContactTypes)Enum.Parse(typeof(ContactTypes), Console.ReadLine(), true); I keep getting the error:
The type or namespace name 'ContactTypes' could not be found (are you missing a using directive or an assembly reference?)
public static void createContact()
{
Contact c1 = new Contact();
try {
Console.WriteLine("\nGetFirstName");
c1.GetFirstName = Console.ReadLine();
}
catch (System.NullReferenceException)
{
Console.WriteLine("Contact create failed");
}
Console.WriteLine("\nGetLastName");
c1.GetLastName = Console.ReadLine();
Console.WriteLine("\nGetEmailAddress");
c1.GetEmailAddress = Console.ReadLine();
Console.WriteLine("\nGetPhoneNumber");
c1.GetPhoneNumber = Console.ReadLine();
Console.WriteLine("\nContactTypes");
//ERROR LINE//
c1.GetContactType = (ContactTypes)Enum.Parse(typeof(ContactTypes), Console.ReadLine(), true);
//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);
Console.WriteLine(c.ContactTypes);
}
Console.ReadLine();
}
如果需要,这是我的联系课程
Here is my Contact class if needed
class Contact
{
//private member variables
private String _firstName;
private String _lastName;
public ContactTypes _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 ContactTypes GetContactType
{
get
{
//Return member variable value
return _contactTypes;
}
set
{
//Validate value and throw exception if necessary
if (value == " ")
{
//throw new Exception("ContactType must have a value");
}
else
{
//Otherwise set member variable value
_contactTypes = value;
}
}
}
public enum ContactTypes { 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}");
if (!isValid)
{
throw new Exception("PhoneNumber must have a value");
}
else
{
_phoneNumber = value;
}*/
//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.ContactTypes
代替
ContactTypes
当您想要调用此枚举时。
when you want to call this Enum.
这篇关于找不到类型或命名空间名称''(您是否缺少using指令或程序集引用?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!