Question

Below is my Enum. When I access my Enum, it is showing 0 for N and 1 for IC.

I want to get N and IC

 Public Enum StatusCode
    <Description("New")> _
    N

    <Description("Incomplete")> _
    IC
 End Enum

This is my vb code

oBLL.StatusCode = StatusCode.N
Était-ce utile?

La solution

It's not showing the wrong value at all, it's showing the correct value (N = 0). However, it would appear that what you are really after is the name of the enum and not the value. To get the name you can simply call ToString

oBLL.StatusCode = StatusCode.N.ToString()

This will internally call Enum.GetValue.

Autres conseils

This is what Enum does. It acts as a front for int type (or other numeric type when specified). Since N is first and IC is second, they respectively are assigned int values 0 and 1.

My advice: constants will be best suited for what you're trying to achieve.

Public Shared Class StatusCode
    Public Const N As String = "N"
    Public Const IC As String = "IC"
End Class

' ...

oBLL.StatusCode = StatusCode.N

If however you insist on using enum, you can get the string representation this way:

oBLL.StatusCode = Enum.GetName(StatusCode.GetType(), StatusCode.N)

Note however that obtaining this value at runtime implies reflection, which should be avoided.

Did not test in VB
But in C# I do not get that behavior
The last two lines do not even pass the compiler (cannot implicitly convert)

public enum enumRole : byte { [Description("First Level Review")] fstLvlRev = 1, [Description("Second Level Review")] secLvlRev = 2 };  
public MainWindow() {
    System.Diagnostics.Debug.WriteLine(enumRole.secLvlRev);
    enumRole ee = enumRole.secLvlRev;
    string es = enumRole.secLvlRev.ToString();
    byte eb = (byte)enumRole.secLvlRev;

    string ees = enumRole.secLvlRev;
    byte eeb = enumRole.secLvlRev;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top