Consider this statement:
MsgBox “Hello!”, vbCritical + vbYesNo
vbCritical and vbYesNo are members of a built-in enumeration called vbMsgBoxStyle. You can create your own enumerations containing constants of your choice for use in your applications. To do so, you use the Enum statement at the module level.
Enum dkMsgText
dkHello = 1
dkWorld = 2
dkExclaim = 4
End Enum
In the above example, the members are assigned specific values, but those values are optional. If you omit the values, they are automatically assigned consecutive long integers starting with zero (0,1,2…).
Enumerations can be additive or non-additive. The enumeration for a MsgBox style is additive, meaning that you can add members together. There’s nothing special in the Enum statement that identifies additive enumerations, it’s the code that accepts the enumeration that handles the additive nature. For built-in, additive enumerations, the values assigned to the members are usually 1, 2, 4, 8, 16, etc. so you can add two or more together and get a unique number.
Here’s a sub that accepts the above enumeration to create a message box.
Sub SayHello(eText As dkMsgText)
Dim sText As String
Select Case eText
Case 1
sText = “Hello “
Case 2
sText = “World “
Case 3
sText = “Hello World “
Case 4
sText = “!”
Case 5
sText = “Hello !”
Case 6
sText = “World !”
Case 7
sText = “Hello World !”
End Select
MsgBox sText
End Sub
Sub CallSayHello()
SayHello dkHello + dkWorld
End Sub
Testing the additive nature of the enumeration can be done without testing every possible combination by using bitwise comparisons. But that’s a subject for another post.
wow, i never knew you could do that! cool
“Sub SayHello(eText As dkMsgText)”
Sorry to be pedantic but it is Friday… Why pass the eText argument by reference?
Jamie.
Jamie: I’m ashamed to say that it’s pure laziness. At one point I resolved to always use ByRef and ByVal explicitly so that I could get in the habit or really choosing instead of just accepting the default. As you can see, that didn’t last.
Hi, Dick. In your “Custom Enumerations” article on 7/20/04 you said that “Testing the additive nature of the enumeration can be done without testing every possible combination by using bitwise comparisons. But that’s a subject for another post.”
I’m currently checking every combination with If/ElseIf/End If and Select Case / End Case statements. It’s a pain the keister.
I’m familiar with using binary series and custom enumerations, but haven’t quite got the hang of setting bits in a number to avoid having to check every comparison.
Can you shed some light on how to do this? Did you every write that other post you talked about?
Here’s a quick re-write:
Sub SayHello(ByVal eText As dkMsgText)
Dim sText As String
If eText And dkHello Then
sText = “Hello”
End If
If eText And dkWorld Then
sText = sText & LTrim$(sText & ” World”)
End If
If eText And dkExclaim Then
sText = sText & “!”
End If
MsgBox sText
End Sub
Is there an easy way to check if a value corresponds to an Enum, such as dkMsgText above?
Say I had a Long variable: faveNum = 2
Could I somehow check if faveNum’s current value is valid in dkMsgText? It should be in this case, since it matches dkWorld’s value.
I’ve tried things like “For Each e in dkWorld”, but it doesn’t let you do that.