我需要使用enum
,以便我的应用程序中的验证易于阅读,并且易于理解和管理:
然而,我觉得我的做法有点无关紧要。有没有办法用更少的代码来做到这一点?
请注意:为了简洁起见,我已从代码中删除了实际验证。
代码:
public enum LastName
{
ContainNumbers,
ContainIlligalCharacters,
ContainEmptyString,
PassedValidation,
}
public enum FirstName
{
ContainNumbers,
ContainIlligalCharacters,
ContainEmptyString,
PassedValidation,
}
public class DataMember
{
int _lastNameState;
int _firstNameState;
public DataMember(string lastName, string firstName)
{
LastName lName;
FirstName fName;
_lastNameState = (int)(lName = (lastName == "A" ?
LastName.PassedValidation : LastName.ContainEmptyString));
_firstNameState = (int)(fName = (firstName == "B" ?
FirstName.PassedValidation : FirstName.ContainEmptyString));
}
public int UserLastName
{
get
{
return _lastNameState;
}
set
{
_lastNameState = value;
}
}
public int UserFirstName
{
get
{
return _firstNameState;
}
set
{
_firstNameState = value;
}
}
}
致电代码:
protected void btnSubmit_Click(object sender, EventArgs e)
{
DataMember dataMember = new DataMember(txtLastName.Text, txtFirstName.Text);
bool isValidLastName = (dataMember.UserLastName == (int)LastName.PassedValidation);
bool isValidFirstName = (dataMember.UserFirstName == (int)FirstName.PassedValidation);
lblDisplayLastNameStatus.Text = isValidLastName.ToString();
lblDisplayFirstNameStatus.Text = isValidFirstName.ToString();
}
答案 0 :(得分:3)
为姓氏和名字定义单个枚举。
public enum NameValidation
{
[Description("Contains Numbers"]
ContainNumbers,
[Description("Contains Illigal Characters"]
ContainIlligalCharacters,
[Description("Contains empty strings"]
ContainEmptyString,
[Description("Success. Passed Valiation"]
PassedValidation,
}
使用Enum Helper扩展方法来阅读说明:
public static class EnumHelper
{
public static string GetDescription<T>(this T enumVal) where T : struct
{
Type type = enumVal.GetType();
if (!type.IsEnum)
{
throw new ArgumentException("Must be enum type","enumVal");
}
MemberInfo[] memberInfo = type.GetMember(enumVal.ToString());
if (memberInfo.Length > 0)
{
object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs.Length > 0)
{
return ((DescriptionAttribute)attrs[0]).Description;
}
}
return enumVal.ToString();
}
}
在“提交”按钮上,将说明分配给标签文本。
lblDisplayFirstNameStatus.Text= ValidateFirstName().GetDescription();
lblDisplayFirstNameStatus.Text= ValidateLastName().GetDescription();
此处ValidateFirstName和ValidateLastName返回NameValidation枚举类型。
答案 1 :(得分:2)
为什么需要两个相同的枚举?只需将其命名并巧妙地命名变量即可。
public enum NameValidation
{
ContainNumbers,
ContainIlligalCharacters,
ContainEmptyString,
PassedValidation,
}
NameValidation lNameValidation;
NameValidation fNameValidation;
如果你想定义两个,但它们有不同但相似的值集,你总是可以定义一个类并继承。
public abstract class BaseValidation {
public const ContainNumbers int = 1;
public const ContainIlligalCharacters = 2;
public const ContainEmptyString = 3;
public const PassedValidation = 4
}
public class FirstName: BaseValidation {
public const SomethingDifferent = 5;
}
public class LastName: BaseValidation {
public const SomethingMoreDifferent = 5;
public const SomethingEvenMoreDifferent = 6;
}
我不推荐我的第二个解决方案,因为它太过分了,但这是你可以采取的另一种方式。
答案 2 :(得分:1)
以下是对顶级代码的更新,以便进行更清晰的基本验证。 [Flags]属性的好处是它可以扩展你知道所有无效状态的能力。换句话说,您可以与客户共享一条消息,表明其名称包含数字和非法字符。
也没有必要将枚举转换为int。您可以使用与int相同的方式比较枚举。您也可以使用默认属性。
[Flags]
public enum NameValidation
{
PassedValidation = 0,
ContainNumbers = 1,
ContainIllegalCharacters = 2,
ContainEmptyString = 4,
}
public class DataMember
{
public NameValidation FirstNameState {get; set;}
public NameValidation LastNameState {get; set;}
public DataMember(string lastName, string firstName)
{
FirstNameState = ValidateName(firstName);
LastNameState = ValidateName(lastName);
}
private NameValidation ValidateName(string name)
{
var validation = NameValidation.PassedValidation;
if(string.IsNullOrEmpty(name)) validation |= NameValidation.ContainEmptyString;
//Validate illegal characters
//Validate numbers
return validation;
}
}
我将这些打印输出为真/假,因为这是原始调用所做的。但是,您也可以打印出标志值以提供验证状态。
protected void btnSubmit_Click(object sender, EventArgs e)
{
DataMember dataMember = new DataMember(txtLastName.Text, txtFirstName.Text);
lblDisplayLastNameStatus.Text = (dataMember.LastNameState == NameValidation.PassedValidation).ToString();
lblDisplayFirstNameStatus.Text = (dataMember.FirstNameState == NameValidation.PassedValidation).ToString();
}