C# 使用对象类以正确的格式提供电子邮件和电话号码
我正在尝试为我的contact Manager应用程序创建一个contact类,我需要电子邮件必须包含1个且仅包含1个@符号,并确保电话号码的格式为123-456-7890,但我不确定如何执行此操作 如有任何指导,将不胜感激C# 使用对象类以正确的格式提供电子邮件和电话号码,c#,C#,我正在尝试为我的contact Manager应用程序创建一个contact类,我需要电子邮件必须包含1个且仅包含1个@符号,并确保电话号码的格式为123-456-7890,但我不确定如何执行此操作 如有任何指导,将不胜感激 class Contact { class Contact { //private member variables private String _firstName; private String _la
class Contact
{
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;
}
}
}
}
我同意@Enigmativity的评论——在访问器集函数中抛出异常不是一个好主意。也就是说,在
GetPhoneNumber
中,您的isValid
正则表达式测试已经完成了一半
set
{
bool isValid = Regex.IsMatch(value, @"/d{3}-/d{3}-/d{4}");
if (isValid) // Set member variable value
_phoneNumber = value;
}
您可以对电子邮件执行类似的正则表达式测试,也可以使用
System.Net.Mail.MailAddress
类。看到这一点。当您得到错误的值时,您真的不应该抛出异常。例外情况应适用于硬盘空间不足或连接中断等异常情况。应使用数据验证技术处理无效输入-在分配前预验证或在对象上实现IDataErrorInfo
。