Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
  Tip: Enforce Enum case  
 
 

Unfortunately in Visual Basic 6 and previous versions, if you type an Enum value with the wrong case the system changes the case to match what you typed. For example, suppose you define an Enum like this:

    Private Enum People
        perManager
        perEmployee
        perCustomer
    End Enum
Now suppose you are writing code and you type:
    the_customer.PersonType = percustomer
Visual Basic updates the definition of the Enum so this value is spelled percustomer rather than perCustomer. Not fatal but annoying!

You can prevent this by declaring variables with the same names and the proper case. The coolest part is that the declarations don't need to be incorporated into the compiled code so you can hide them using compiler directives.

    #If False Then
        Dim perManager
        Dim perEmployee
        Dim perCustomer
    #End If

    Private Enum People
        perManager
        perEmployee
        perCustomer
    End Enum
Now if you type the Enum value in the wrong case, Visual Basic corrects it so it matches the hidden variable declarations.

 

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated