1

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
0

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

9 Comments

what is the solution to my issue ? Is it better to declare constants ?
I am interested to know how constants would be better suited? An enum effectively is a constant.
@James it comes down to using the right type for the right job. Of course you can convert an enum to its string representation but if you're not even using the enum as is (as the post suggests) then what's the point declaring this enum at all? Just use plain string constants instead.
@James plus, obtaining the string representation of an enum implies reflection.
@Crono I am not saying that if the OP purely needs strings that const isn't a better (and in fact more efficient) way to go. However, I'd argue that your answer is very subjective. The OP has attributes associated with their enum therefore that implies context - what context can you give to a const? Another argument would be that it's more efficient/reliable to test int for equality than it is string so, actually, using an enum would be the right tool for the job. "...runtime implies reflection, which should be avoided" - again completely subjective.
|
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.