Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Understanding enums

Let's say we have an enum:

Public Enum Direction
    North
    East
    South
    West
End Enum

If I want to check all four directions, do I have to do it explicitly?

Checkroom(Direction.North)
Checkroom(Direction.South)
Checkroom(Direction.East)
Checkroom(Direction.West)

Or is there a way to "loop through" the values?

For each d in Direction
CheckRoom(d)
Next

Are they really represented by integers internally? Can I treat them that way?

For i=0 to 3
Checkroom(Ctype(i,direction))
Next i

Thanks!

BradC
Saturday, February 05, 2005

The Enum class has static methods that allow you to extract all defined values of an enumeration.

Use Enum.GetValues to get all values, and Enum.GetNames to get all names.

If you want the integer equivalent of an enumeration constant you have to cast it, as usual.

Chris Nahr
Saturday, February 05, 2005

Note that you'll probably have to type it as System.Enum, since Enum is also a keyword.  E.g.:

System.Enum.GetValues(GetType(NameOfYourEnum))

Kyralessa
Monday, February 07, 2005

Or you can put it in brackets:

[Enum].GetValues(GetType(NameOfYourEnum))

Kyralessa
Monday, February 07, 2005

Ah, the foibles of Visual Basic and its lack of case sensitivity...

Chris Nahr
Monday, February 07, 2005

For several thousand less shift-key-presses a day, a couple of brackets are a small price to pay.

Kyralessa
Thursday, February 10, 2005

*  Recent Topics

*  Fog Creek Home