HOWTO: Use Spaces in Enumerated Types

Last reviewed: September 30, 1997
Article ID: Q161232
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:

  1. Start Microsoft Visual Basic version 5.0. Create a new Standard EXE. This will add Form1 to our new project.

  2. From the Project menu, click Add Module.

  3. 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:
    
    

  4. 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:
    
    

  5. Add the following code to Module1:

          Sub Main ()
    
          End Sub
    
          Sub Test1 (newVar as SpecialCharacters1)
    
          End Sub
    
          Sub Test2 (newVar as SpecialCharacters2)
    
          End Sub
    
    

  6. 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
    
    

  7. 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.
    
    

  8. 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.
    
    

  9. 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


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 30, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.