ElektroStudios
ElektroStudios

Reputation: 20484

Represent Enum values as String

I have this Enum

Public Enum HotkeyModifiers As Short
    SHIFT = 1
    CONTROL = 2
    ALT = 4
    NONE = 0
End Enum

So 6 is equals to ALT+CONTROL, so when I do this:

 MsgBox((HotkeyModifiers.CONTROL Or HotkeyModifiers.ALT).ToString)
MsgBox([Enum].Parse(GetType(HotkeyModifiers), 6).ToString)

I expect to get this output as String:

CONTROL, ALT

Because If I try to do the same with a framework enum like for example the Keys enum:

MsgBox((Keys.Alt Or Keys.ControlKey).ToString)

I get this string:

ControlKey, Alt

Then what I'm missing to do in my Enumeration?

Upvotes: 0

Views: 137

Answers (2)

Simon Jensen
Simon Jensen

Reputation: 486

I seem to have no issues when doing like the following:

    Private Enum enumModul As Integer
        Modul_1 = 1
        Modul_2 = 2
        Modul_3 = 3
        Out_of_work = 4
        Pause = 5
    End Enum

    Dim Modul As enumModul = 0

    Label3.Text = Modul.ToString

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503280

You need to decorate your enum with FlagsAttribute.

<Flags>
Public Enum HotkeyModifiers As Short
    SHIFT = 1
    CONTROL = 2
    ALT = 4
    NONE = 0
End Enum

That affects the behaviour of both ToString and parsing.

Upvotes: 4

Related Questions