ENUMERATED DATA TYPES

An enumerated type is a discrete list of specific values. You define the enumerated type and the values allowed. Later, if you declare a variable of that data type, it can take only those values.

For example, suppose that you are building a large application where users can have one of three access levels: clerk, supervisor, and administrator. You could define an enumerated type named AccessLevels that allows the values Clerk, Supervisor, and Administrator. Now, if you declare a variable to be of type AccessLevels, Visual Basic will only allow the variable to take those values.

The following code shows a simple example. It defines the AccessLevels type and declares the variable AccessLevel using the type. Later the MakeSupervisor subroutine sets AccessLevel to the value AccessLevels.Supervisor. Note that the value is prefixed with the enumerated type’s name.

Public Enum AccessLevels
    Clerk
    Supervisor
    Administrator
End Enum
             
Private AccessLevel As AccessLevels ' The user's access level.
             
' Set supervisor access level.
Public Sub MakeSupervisor()
    AccessLevel = AccessLevels.Supervisor
End Sub

The syntax for declaring an enumerated type is as follows:

[attribute_list] [accessibility] [Shadows] Enum name [As type]
    [attribute_list] value_name [= initialization_expression]
    [attribute_list] value_name [= initialization_expression]
   ...
End Enum

Most of these terms, including attribute_list and accessibility, are similar to those used by variable declarations. See the section “Variable Declarations” earlier in this chapter for more information.

The type value must be an integral type and can be Byte, Short, Integer, or Long. If you omit this value, Visual Basic stores the enumerated type values as integers.

The value_name pieces are the names you want to allow the enumerated type to have. You can include an initialization_expression for each value if you like. That value must be compatible with the underlying data type (Byte, Short, Integer, or Long). If you omit a value’s initialization expression, the value is set to one greater than the previous value, with the first value equal to zero by default.

In the previous example, Clerk = 0, Supervisor = 1, and Administrator = 2. The following code changes the numeric assignments so Clerk = 10, Supervisor = 11, and Administrator = ’1:

Public Enum AccessLevels
    Clerk = 10
    Supervisor
    Administrator = -1
End Enum

Usually, all that’s important about an enumerated type is that its values are unique, so you don’t need to explicitly initialize the values.

Note that you can give enumerated values the same integer value either explicitly or implicitly. For example, the following code defines several equivalent AccessLevels values. The first three values, Clerk, Supervisor, and Administrator, default to 0, 1, and 2, respectively. The code explicitly sets User to 0, so it is the same as Clerk. The values Manager and SysAdmin then default to the next two values, 1 and 2 (the same as Supervisor and Administrator). Finally, the code explicitly sets Superuser = SysAdmin.

Public Enum AccessLevels
    Clerk
    Supervisor
    Administrator
    User = 0
    Manager
    SysAdmin
    Superuser = SysAdmin
End Enum

This code is somewhat confusing. The following version makes it more obvious that some values are synonyms for others:

Public Enum AccessLevel
    Clerk
    Supervisor
    Administrator
 
    User = Clerk
    Manager = Supervisor
    SysAdmin = Administrator
    Superuser = Administrator
End Enum

If you really need to set an enumerated variable to a calculated value for some reason, you can use the CType function to convert an integer value into the enumerated type. For example, the following statement uses the value in the variable integer_value to set the value of the variable AccessLevel. Making you use CType to perform this type of conversion makes it less likely that you will set an enumerated value accidentally.

AccessLevel = CType(integer_value, AccessLevel)

Another benefit of enumerated types is that they allow Visual Basic to provide IntelliSense help. If you type AccessLevel =, Visual Basic provides a list of the allowed AccessLevels values.

A final benefit of enumerated types is that they provide a ToString method that returns the textual name of the value. For example, the following code displays the message “Clerk”:

Dim access_level As AccessLevel = Clerk
MessageBox.Show(access_level.ToString())

Example program AccessLevelEnum, which is available for download on the book’s website, makes an AccessLevels Enum and then displays the results returned by calling ToString for each of its values.

If you have a variable that can take only a fixed number of values, you should probably make it an enumerated type. Also, if you discover that you have defined a series of constants to represent related values, you should consider converting them into an enumerated type. Then you can gain the benefits of the improved Visual Basic type checking and IntelliSense.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.116.69