Two alternative approaches for you to consider.
The obvious solution: a simple switch statement:
var msg = Messages.NoPassword;
switch (msg)
{
case Messages.NoPassword:
MessageBox.Show("No password");
break;
case Messages.NoUserName:
MessageBox.Show("No user name");
break;
case Messages.NoUserNameOrPassword:
MessageBox.Show("No user name");
break;
case Messages.UserAlreadyExists:
MessageBox.Show("User already exists");
break;
}
Alternatively, add [Description] attributes to the enumeration values (assuming the enum is not defined in third party code):
MessageBox.Show(GetDescription(msg));
For which you'll need this function:
public static string GetDescription(Enum value)
{
FieldInfo field = value.GetType().GetField(value.ToString());
DescriptionAttribute attribute
= Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
as DescriptionAttribute;
return attribute == null ? value.ToString() : attribute.Description;
}
and each enumeration member will need to be decorated:
public enum Messages
{
[Description("User already exists")]
UserAlreadyExists,
[Description("No user name")]
NoUserName,
[Description("No password")]
NoPassword,
[Description("No user name")]
NoUserNameOrPassword
}
Neither of these solutions are localised of course.