Jul 23rd, 2008, 10:50 AM
Classic VB - What is an Enum, and how can I use it?
An Enumeration is basically a new data type (like String or Integer) that you design yourself, which has an associated list of possible values (known as elements).
You might think it would be better to use a String to hold the current value instead, but there are several reasons why an Enum is better, such as:
- The list of valid options will be shown in a pop-up list when you are writing code (see the picture below, after the first Code snippet), so you don't need to remember them or even type them - instead you can just select them with your mouse or keyboard.
- It is harder to make a typo that causes bugs, as not only are the values shown in the list for you to pick from, but also valid entries you type will be changed to the case you declared them - so you can see when the case is not automatically corrected for you.
- If you use Option Explicit (which is highly recommended anyway), any typo's will cause a message warning you about them.
- They take less memory than Strings, and code that uses them runs faster than the String equivalent.
To set up an Enumeration, use code like this in the "General" - "Declarations" section of a code file (at the very top):
To use it, declare a variable (or parameter, etc) using the Enum as the data type, and then when you are typing you will see a list of possible elements that you can select from (or type if you prefer):
Private Enum MyValues ' this is the name of our Enumeration
Second '} these are the elements/values that will be allowed
Third '} (add as many as you like, there must be at least one!)
To work with them, you can use the element names just as you would usually use a variable/const, eg:
How it works
Dim MyVariable As MyValues
MyVariable = First
If MyVariable = Second Then
MsgBox "This message will not be shown!"
The Enum itself is just a way to group the elements (like First and Second above), each of which is basically created as a Const with a data type of Long.
Each of them is automatically assigned a unique number, which you never actually need to know, because you can just use the name instead (as shown in the previous Code snippet).
Using the names is the best thing to do, as it makes your code more readable (you don't need to remember which element the number 37 refers to!), and it also means that if the numbers ever change (like when you add an extra element to the list) the code that uses them doesn't need to change.
If you really want to, you can actually specify the number that will be associated with each element - but unless you have a good reason it is just a waste of time (as the values will automatically be unique anyway). For example, it could have been declared like this:
(even if you do this, you should still use the names in the rest of your code)
Private Enum MyValues
First = -23
Second = 4
Third = 8723
- No matter what order you declared the elements in, they are always listed alphabetically (so "Two" would be listed after "Three"). If you want them to be listed in a certain order, change the element names (perhaps by adding a prefix) so that the alphabetical order is the same as the order you want.
- The values are not strictly limited to the list of elements you provided - just to the Long data type (so MyVariable = 527934 is allowed!). It is therefore a good idea to do some validation to ensure one of the elements you want has been selected, eg:
Select Case MyVariable
'code for when the value is First
'code for when the value is Second
'code for when the value is Third
'value is not apt - warn the user
MsgBox "That value is not allowed!"
- Just like with variables, you can declare an Enum with the keywords Private or Public, and so use them from different code files in your project... however, unlike variables you cannot specify which code file the Enum was declared in, so it is not a good idea to create two or more Enum's with the same name (depending on the circumstances you could get the wrong one).
Advanced - Multiple values at the same time
In some cases you might want to allow multiple values at the same time, just like can be done with MsgBox, which allows things like this:
To be able to do that, specifying the values is needed - as you will want to ensure that the values do not accidentally overlap an existing value when they are combined (for example you wouldn't want vbOKCancel + vbQuestion to be the same as vbOKOnly !). The best way to do this is with powers of 2, as it ensures uniqueness without making the numbers growing quicker than needed, eg:
MsgBox "OK?", vbOKCancel + vbQuestion
You can then set multiple values without any problems, eg:
Private Enum MyMultiValues
Option1 = 1 '2^0
Option2 = 2 '2^1
Option3 = 4 '2^2
Option4 = 8 '2^3
Option5 = 16 '2^4
Reading the values out of it is slightly harder, as you only want to get part of the overall value (and don't want to write lots of extra code to check all the possible permutations!).
Dim MyOptions as MyMultiValues
MyOptions = Option1 + Option2 'will contain 3, which can only mean Option1 + Option2
The way to read just one part of it is with bit masking, which you can do like this:
(note that the And used here is not the logical And you normally use, but a bitwise operator instead - see the help on And for more details)
If (MyOptions And Option1) = Option1 Then
MsgBox "Option1 was selected!"
If (MyOptions And Option2) = Option2 Then
MsgBox "Option2 was selected!"
If you want to validate the value, just check if it is at least the lowest single value (in this case, Option1), and less than double the highest single value (Option5). This is because the maximum is all elements added together, which is one less than double the highest value (in this case, 1+2+4+8+16= 31, which is the same as (16*2)-1 ), eg:
If (MyOptions >= Option1) And (MyOptions < Option5 * 2) Then
MsgBox "Value is valid"
MsgBox "Value is not valid"
Last edited by si_the_geek; Jul 24th, 2008 at 06:04 AM.
Jul 24th, 2008, 03:45 PM
Re: Classic VB - What is an Enum, and how can I use it?
You may also name the Enum values with spaces:
Try to use values that are unlikely to be used by anything else, because you really want these values to be unique and not to be confused with anything else.
Public Enum MyEnum
[My Special Value]
[My Extra Super Special Value]
I'd highly recommend always using the Or operator instead of the + operator when doing bit level manipulation
Click Here to Expand Forum to Full Width
Survey posted by VBForums.