我正在尝试使用列表构建联系人管理器程序来存储和显示数据。我需要查看显示可用联系人摘要的报告,然后有一个菜单允许用户与该程序进行交互。我有一个方法来创建一个包含数据的列表和一个创建新联系人的方法,但我的联系人类和createcontact方法不断收到错误: 可访问性不一致:属性类型' Contact.ContactTypes'比财产更容易接近' Contact.GetContactType'。我不知道如何解决这个问题
任何指导都将不胜感激
public static void Main(string[] args)
{
//Declare the list
List<Contact> contactList = new List<Contact>();
//Main Driver
char menuItem;
Console.WriteLine("Contact List\n");
menuItem = GetMenuItem();
while (menuItem != 'Q')
{
ProcessMenuItem(menuItem, contactList);
menuItem = GetMenuItem();
}
Console.WriteLine("\nThank you, goodbye");
Console.ReadLine();
}
//Returns either a 'C', 'R', 'U', 'D', 'L', or 'X' to the caller
static char GetMenuItem()
{
char menuItem;
DisplayMenu();
menuItem = char.ToUpper(IOConsole.GetChar("\nPlease pick an item: "));
while (menuItem != 'C'
&& menuItem != 'R' && menuItem != 'Q' && menuItem != 'U' && menuItem != 'D' && menuItem != 'S' && menuItem != 'L' && menuItem != 'F' && menuItem != 'P' && menuItem != 'T')
{
Console.WriteLine("\nError - Invalid menu item");
DisplayMenu();
menuItem = char.ToUpper(IOConsole.GetChar("\nEnter option or M for menu:"));
}
return menuItem;
}
static void DisplayMenu()
{
Console.WriteLine("C-> Create Contacts");
Console.WriteLine("R-> Remove Contacts");
Console.WriteLine("U-> Update Contacts");
Console.WriteLine("D -> Load data from file");
Console.WriteLine("S-> Save data to file");
Console.WriteLine("L-> View sorted by last name");
Console.WriteLine("F-> View sorted by first name");
Console.WriteLine("P-> View by partial name search");
Console.WriteLine("T-> View by contact type");
Console.WriteLine("Q-> Quit");
}
//Routes to the appropriate process routine based on the user menu choice
static void ProcessMenuItem(Char menuItem, List<Contact> contactList)
{
switch (menuItem)
{
case 'C':
createContact();
break;
case 'R':
Console.WriteLine("enter the contact name To delete");
removeContact(contactList, Console.Read().ToString() );
break;
case 'U':
updateContact(contactList);
break;
case 'D':
LoadFromFile();
break;
case 'S':
saveToFile();
break;
case 'L':
sortByLastName(contactList);
break;
case 'F':
sortByFirstName(contactList);
break;
case 'P':
DisplayList(contactList);
break;
case 'T':
sortByContactType();
break;
case 'Q':
break;
}
}
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();
}
这是我的联系班级
class Contact
{
//private member variables
private String _firstName;
private String _lastName;
private 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 == null)
{
throw new Exception("ContactType must have a value");
}
else
{
//Otherwise set member variable value
_contactTypes = value;
}
}
}
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;
}
}
}
}
答案 0 :(得分:0)
如果您将contactType枚举设置为公开,则可能会解决问题
public enum ContactTypes { Family, Friend, Professional }