The information in this article applies to:
- Microsoft Visual Basic Professional and Enterprise Editions,
for Windows, version 5.0
SUMMARY
Microsoft Visual Basic version 5.0 introduces the concept of Enumerated
Types to the programming language. Enumerations provide a convenient way to
work with sets of related constants and to associate constant values with
identifiers. The identifier may be defined as either a normal identifier or
a more "friendly" identifier that can contain spaces.
NOTE: Other identifiers, such as variable names and procedure names, can
not contain spaces.
MORE INFORMATION
Because Enumerated types are defined at the module level, you need to do
the following:
- Start Microsoft Visual Basic version 5.0. Create a new Standard EXE.
This will add Form1 to our new project.
- From the Project menu, click Add Module.
- Add the following enumerated type to Module1:
Enum SpecialCharacters1
TabKey = 12
EnterKey = 13
SpaceBar = 32
End Enum
This allows you to use the constant identifiers TabKey, EnterKey, and
SpaceBar in place of the values 12, 13, and 32 respectively when you use
the variable X. However, you are not limited to identifiers that have
only one word (no spaces) in them. You can allow your enumerated type to
have much more friendly names by doing the following:
- Add the following enumerated type to Module1:
Enum SpecialCharacters2
[Tab Key] = 12
[Enter Key] = 13
[Space Bar] = 32
End Enum
By enclosing the identifier in opening and closing brackets ([]), you
can define a much more readable identifier. You can now use these two
different enumerated types by defining some procedures:
- Add the following code to Module1:
Sub Main ()
End Sub
Sub Test1 (newVar as SpecialCharacters1)
End Sub
Sub Test2 (newVar as SpecialCharacters2)
End Sub
- Now add the statements "Test1" and "Test2" (without quotes) to the
Main procedure, which should look like the following:
Sub Main ()
Test1
Test2
End Sub
- Press the spacebar to add the required parameter to the Test1
and Test2 statements. You are provided with the following list of
values:
Test1 EnterKey
SpaceBar
TabKey
Test2 Enter Key
Space Bar
Tab Key
Note that there are no brackets shown in the list Test2's parameter.
- Select the "SpaceBar" value for Test1 and the "Space Bar" value for
Test2. Your Main procedure should look like the following:
Sub Main ()
Test1 SpaceBar
Test2 [Space Bar]
End Sub
Note that the brackets are automatically included for your second
enumerated type. This is a crucial point. If an enumerated type contains
friendly identifiers, the brackets must be used to evaluated the
identifier.
- Add a third line to the Main procedure as follows:
Sub Main ()
Test1 SpaceBar
Test2 [Space Bar]
Test2 Space Bar 'No brackets
End Sub
This code will generate a Compile Error on the second "Test2" line. This is
due to the compiler expecting an end-of-statement after the word "Space."
REFERENCES
Microsoft Visual Basic 5.0 Books Online
"Using Enumerations to Work with Sets of Constants"
Microsoft Visual Basic Online Help
"Enum Statement"
Keywords : vb5all vb5howto VBKBProgramming vbwin GnrlVb kbprg
Technology : kbvba
Version : WINDOWS:5.0
Platform : WINDOWS
Issue type : kbhowto