0
public enum eEstado
{
    Active = 3,
    Inactive = 4
}

I need to settle diferent names(like alias) to the enum properties. For example, for "Activo", something like "Activo" and others. Also I need to get that String from the value (3, or 4).

Thanks.

Flezcano
  • 1,647
  • 5
  • 22
  • 39

3 Answers3

3

To convert from an alias to an enum value:

public static eEstado AliasToEstado(string alias)
{
    switch(alias)
    {
        case "Activo": return eEstado.Active;
        case "Inactivo": return eEstado.Inactive;
        default: throw new ArgumentException(alias);
    }
}

to convert from an enum value to an alias:

 public static string EstadoToAlias(eEstado value)
 {
    switch(value)
    {
        case eEstado.Active: return "Activo";
        case eEstado.Inactive: return "Inactivo";
        default: throw new ArgumentException(value);
    }
 }

to convert from an int value to an alias:

int value = 3;
string alias = EstadoToAlias((eEstado)value);
JLRishe
  • 99,490
  • 19
  • 131
  • 169
3

It is difficult to understand what exactly you are asking but I think this will help:

You can have multiple names with the same value on an enum like:

public enum Status
{
  Undefined = 0,
  Active = 1,
  Inactive = 2,
  Default = Active
}

and you can parse from a string with

static void Main(string[] args)
{
    Status status = Status.Default;

    string new_status = "Inactive";

    status = (Status)Enum.Parse(typeof(Status), new_status);

    Console.WriteLine(status.ToString());
    // prints "Inactive"

    status = (Status)1;
    Console.WriteLine(status.ToString());
    // prints "Active"

}
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
-2

you can use any name in enum but you can not use enum for string

like

public enum eEstado
{
// its an valid enum
    Activo = 3,  
    Inactivo = 4
}

and for use enum like string

you can convert it in string

exm.

strinf s=eEstado.Activo.ToString();

then

if(s==eEstado.Activo.ToString())
{
  // do sumthing
}

in this the value of 's' will not 3 . it will 'Activo' i think it will helping for you

welcome for your thank's

best of luck

sourabh devpura
  • 605
  • 8
  • 25