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 AccessLevel that allows the values Clerk, Supervisor, and Administrator. Now, if you declare a variable to be of type AccessLevel, Visual Basic will only allow the variable to take those values.

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

  Public Enum AccessLevel     Clerk     Supervisor     Administrator End Enum Private m_AccessLevel As AccessLevel    ' The user's access level. ' Set supervisor access level. Public Sub MakeSupervisor()     m_AccessLevel = AccessLevel.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. This 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. The first value is zero by default.

For example, in the previous example, Clerk = 0, Supervisor = 1, and Administrator = 2. The following code changes the default assignments so Clerk = 10, Supervisor = 11, and Administrator = -1:

  Public Enum AccessLevel     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 AccessLevel 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, respectively). Finally, the code explicitly sets Superuser = SysAdmin.

  Public Enum AccessLevel     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 

You can get an effect similar to enumerated types using integer variables and constants, as shown in the following code. This code does roughly the same thing as the previous examples.

  Public Const Clerk As Integer = 0 Public Const Supervisor As Integer = 1 Public Const Administrator As Integer = 2 Private m_AccessLevel As Integer        ' The user's access level. ' Set supervisor access level. Public Sub MakeSupervisor()     m_AccessLevel = Supervisor End Sub 

Declaring an enumerated type has a couple of advantages over using integers and constants, however. First, it prevents you from assigning nonsense values to the variable. In the previous code, you could set m_AccessLevel to 10, which wouldn’t make any sense.

Using an enumerated data type allows Visual Basic to verify that the value you are assigning to the variable makes sense. You can only set the variable equal to one of the values in the enumerated type or to the value stored in another variable of the same enumerated type.

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 m_AccessLevel. Making you use CType to perform this type of conversion makes it less likely that you will set an enumerated value accidentally.

  m_AccessLevel = CType(integer_value, AccessLevel) 

Another benefit of enumerated types is that they allow Visual Basic to provide IntelliSense help. If you type m_AccessLevel =, Visual Basic provides a list of the allowed AccessLevel 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()) 

If you have a value 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.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net