JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Creating Enumerations

You've got a hundred constants to declare, and you would like to break them up into functional groups—isn't there an easy way to handle this? There is—you can create an enumeration, which is a related set of constants. You create enumerations with the Enum statement at module, class, structure, procedure, or block level:

[ <attrlist> ] [{ Public | Protected | Friend | Protected Friend |
Private }] [ Shadows ] Enum name [ As type ]
   [<attrlist1 >] membname1 [ = initexpr1 ]
   [<attrlist2 >] membname2 [ = initexpr2 ]
   [<attrlistn>] membnamen [ = initexprn ]
End Enum

The parts of this statement are the same as for constants (see the previous Solution). Here's an example that shows how this works; in this case, I'm setting up an enumeration that assigns a constant to every day of the week:

Module Module1
    Enum Days
        Sunday = 1
        Monday = 2
        Tuesday = 3
        Wednesday = 4
        Thursday = 5
        Friday = 6
        Saturday = 7
    End Enum

    Sub Main()
        System.Console.WriteLine("Friday is day " & Days.Friday)
    End Sub

End Module

To use a constant in the enumeration, you refer to it like this: Days.Friday, Days.Monday, and so on. Here's the result of this code:

Friday is day 6
Press any key to continue
Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor