我正在控制台应用程序上构建一个联系人管理器应用程序,我已经创建了一个联系对象类,但我不断收到错误: “联系人”类型已包含“ContactTypes”的定义。 我不知道如何解决这个问题
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 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 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 :(得分:3)
您有两个具有相同名称的类成员:
public ContactTypes ContactTypes
enum ContactTypes { Family, Friend, Professional }
只需重命名其中一个。
或者,正如评论中所建议的那样,您可以将ContactTypes
枚举定义移到Contact
类之外。
答案 1 :(得分:0)
您的类和枚举具有相同名称的ContactTypes。这就是您收到错误的原因。考虑重命名类。