Question

I have the following enum declared:

 public enum TransactionTypeCode { Shipment = 'S', Receipt = 'R' }

How do I get the value 'S' from a TransactionTypeCode.Shipment or 'R' from TransactionTypeCode.Receipt ?

Simply doing TransactionTypeCode.ToString() gives a string of the Enum name "Shipment" or "Receipt" so it doesn't cut the mustard.

Was it helpful?

Solution

Try this:

string value = (string)TransactionTypeCode.Shipment;

OTHER TIPS

You have to check the underlying type of the enumeration and then convert to a proper type:

public enum SuperTasks : int
    {
        Sleep = 5,
        Walk = 7,
        Run = 9
    }

    private void btnTestEnumWithReflection_Click(object sender, EventArgs e)
    {
        SuperTasks task = SuperTasks.Walk;
        Type underlyingType = Enum.GetUnderlyingType(task.GetType());
        object value = Convert.ChangeType(task, underlyingType); // x will be int
    }    

I believe Enum.GetValues() is what you're looking for.

The underlying type of your enum is still int, just that there's an implicit conversion from char to int for some reason. Your enum is equivalent to

TransactionTypeCode { Shipment = 83, Receipt = 82, }

Also note that enum can have any integral type as underlying type except char, probably for some semantic reason. This is not possible:

TransactionTypeCode : char { Shipment = 'S', Receipt = 'R', }

To get the char value back, you can just use a cast.

var value = (char)TransactionTypeCode.Shipment;

// or to make it more explicit:
var value = Convert.ToChar(TransactionTypeCode.Shipment);

The second one causes boxing, and hence should preform worse. So may be slightly better is

var value = Convert.ToChar((int)TransactionTypeCode.Shipment);

but ugly. Given performance/readability trade-off I prefer the first (cast) version..

I was Searching For That and i get the Solution Use the Convert Class

int value = Convert.ToInt32(TransactionTypeCode.Shipment);

see how it easy

This is how I generally set up my enums:

public enum TransactionTypeCode {

  Shipment("S"),Receipt ("R");

  private final String val;

  TransactionTypeCode(String val){
    this.val = val;
  }

  public String getTypeCode(){
    return val;
  }
}

System.out.println(TransactionTypeCode.Shipment.getTypeCode());

the underlying values of the enum has to be numeric. If the type of underlying values are known, then a simple cast returns the underlying value for a given instance of the enum.

enum myEnum : byte {Some = 1, SomeMore, Alot, TooMuch};
myEnum HowMuch = myEnum.Alot;
Console.Writeline("How much: {0}", (byte)HowMuch);

OUTPUT: How much: 3

OR (closer to the original question)

enum myFlags:int {None='N',Alittle='A',Some='S',Somemore='M',Alot='L'};
myFlags howMuch = myFlags.Some;
Console.WriteLine("How much: {0}", (char)howMuch);
//If you cast as int you get the ASCII value not the character.

This is recurring question for me, I always forget that a simple cast gets you the value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top