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
- Members count up from the first explicit value.
CIntextracts the number; WriteLine prints the name.- Square brackets let the
Enumkeyword act as a type name. GetValues(Of Roast)()enumerates every member in order.
Keywords and builtins used here
CIntDimEachEndEnumForIfNextOfRoastThen
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.
Step 2 of 3 in Value types, step 20 of 29 in Language basics.