0

Is it possible to show a MessageBox according to the selected enum item? E.g.:

public enum Messages
{
    UserAlreadyExists,
    NoUserName,
    NoPassword,
    NoUserNameOrPassword
}

If so, how do I handle each item in order to show a specific MessageBox?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
92AlanC
  • 1,327
  • 2
  • 14
  • 33
  • What did you try? A simple `switch` might work? – nvoigt Jun 06 '15 at 20:12
  • @nvoigt I don't know how to assign tasks to enum items mate, could you please show me an example? – 92AlanC Jun 06 '15 at 20:15
  • @Alan_UK `switch(val){case UserAlreadyExists: MessageBox.Show("User already exists"); break; case NoUserName: MessageBox.Show("No user name"); break; }`? – GSerg Jun 06 '15 at 20:20
  • @GSerg, seems like it might work, but where should I put that `switch` statement and what should I put inside the `()`? I'm sorry, I'm new to enums – 92AlanC Jun 06 '15 at 20:34
  • 2
    obviously where you need it and what for – Amit Kumar Ghosh Jun 06 '15 at 20:36
  • Or [decorate values with attributes](http://stackoverflow.com/q/2650080/11683). – GSerg Jun 06 '15 at 20:41
  • If you own the enumeration, consider renaming it. A plural is supposed to indicate a flags enum which this is not. It doesn't seem to be a message either. – Stephen Kennedy Jun 06 '15 at 20:49

2 Answers2

0

Try the following:

switch(input_value) 
{
case Messages.UserAlreadyExists: 
MessageBox.Show("User already exists");
break;
}
SreekanthGS
  • 988
  • 5
  • 12
0

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.

Community
  • 1
  • 1
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108