typestar

Enums in Visual Basic

Named constants, their integers, and [Enum] with brackets.

Dim order = Roast.Medium
Console.WriteLine(order)
Console.WriteLine(CInt(order))

If order > Roast.Light Then Console.WriteLine("bold enough")

For Each level In [Enum].GetValues(Of Roast)()
    Console.WriteLine($"{CInt(level)}: {level}")
Next

Enum Roast
    Light = 1
    Medium
    Dark
End Enum

How it works

  1. Members count up from the first explicit value.
  2. CInt extracts the number; WriteLine prints the name.
  3. Square brackets let the Enum keyword act as a type name.
  4. GetValues(Of Roast)() enumerates every member in order.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
283
Tokens
71
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 50 seconds.

Type this snippet

Step 2 of 3 in Value types, step 20 of 29 in Language basics.

← Previous Next →

Enums in other languages