Link to home
Start Free TrialLog in
Avatar of Sonny G
Sonny G

asked on

Can someone give me an explanation of Enums and some samples?

I have recently been exposed to Enums and i could use a quick overview.
Avatar of Bill Prew
Bill Prew

These explain it better than I could quickly type up...



»bp
Short answer:
Enums are constant values grouped together.

They can mean anything, but it is best to declare an enum with members that have the same meaning.
Plus, using them wisely will increase the readability of your code (instead of using hard coded "magic" values).

Long answer:
See links posted by Bill.
Enums are enumerations. This means that you assign names to numbers. It is meant for easier memorizing numeric constants which belong to one group.

For example this method:

Public Function Calulate(Operand1 As Long, Operand2 As Long, Operation As Long)

Open in new window


where you define the operation +, -, *, / as numbers 1,2,3,4.
Instead of calling it as

Debug.Print Calculate(10, 2, 1)
Debug.Print Calculate(10, 2, 2)
Debug.Print Calculate(10, 2, 3)
Debug.Print Calculate(10, 2, 4)

Open in new window


you can define an enumeratuion as

Public Enum EnumOperation
  opAdd
  opSubtract
  opMultiply
  opDivide
End Enum

Open in new window

Now you can use those names instead of numbers:

Debug.Print Calculate(10, 2, opAdd)
Debug.Print Calculate(10, 2, opSubtract)
Debug.Print Calculate(10, 2, opMultiply)
Debug.Print Calculate(10, 2, opDivide)

Open in new window

Per default an enum in VBA starts with the value 1 and increments it by one for each enum value. It is an Long/Integer and casted as necessary. So you can use those names, but are not forced to. Another thing is, enums are supported by Intellisense.

But you can assign your own values to it, as long as these are unique and Long or Integer. This for example is used for constants which are used for bitwise operations like the constants for the MsgBox dialog (VBA.VbMsgBoxStyle). Here each enum value has its own assigned value following a bitwise pattern.


E.g. a custom sample:


Option Explicit

Public Enum EnumOperation
  opAdd
  opSubtract
  opMultiply
  opDivide
End Enum

Public Enum EnumOperation2
  opAdd2 = 1
  opSubtract2 = 2
  opMultiply2 = 4
  opDivide2 = 8
End Enum

Public Sub Test()

  Debug.Print "Without names"
  Calculate 10, 2, 1
  Calculate 10, 2, 2
  Calculate 10, 2, 3
  Calculate 10, 2, 4
  
  Debug.Print "With names"
  Calculate2 10, 2, opAdd
  Calculate2 10, 2, opSubtract
  Calculate2 10, 2, opMultiply
  Calculate2 10, 2, opDivide
  
  Debug.Print "As numbers are used in the background, we don't need to use the names."
  Calculate2 10, 2, 1
  Calculate2 10, 2, 2
  Calculate2 10, 2, 3
  Calculate2 10, 2, 4
    
  Debug.Print "Bitwise logic to allow to pass multipls values."
  Calculate3 10, 2, opAdd2 + opSubtract2
  Calculate3 10, 2, opMultiply2 + opDivide2
  
End Sub

Public Sub Calculate(Operand1 As Long, Operand2 As Long, Operation As Long)

  Select Case Operation
    Case Is = 1: Debug.Print Operand1 + Operand2
    Case Is = 2: Debug.Print Operand1 - Operand2
    Case Is = 3: Debug.Print Operand1 * Operand2
    Case Is = 4: Debug.Print Operand1 / Operand2
  End Select

End Sub

Public Sub Calculate2(Operand1 As Long, Operand2 As Long, Operation As EnumOperation)

  Select Case Operation
    Case Is = opAdd: Debug.Print Operand1 + Operand2
    Case Is = opSubtract: Debug.Print Operand1 - Operand2
    Case Is = opMultiply: Debug.Print Operand1 * Operand2
    Case Is = opDivide: Debug.Print Operand1 / Operand2
  End Select

End Sub

Public Sub Calculate3(Operand1 As Long, Operand2 As Long, Operation As EnumOperation2)

  If (Operation And opAdd2) = opAdd2 Then
    Debug.Print Operand1 + Operand2
  End If
  
  If (Operation And opSubtract2) = opSubtract2 Then
    Debug.Print Operand1 - Operand2
  End If
  
  If (Operation And opMultiply2) = opMultiply2 Then
    Debug.Print Operand1 * Operand2
  End If
  
  If (Operation And opDivide2) = opDivide2 Then
    Debug.Print Operand1 / Operand2
  End If

End Sub

Open in new window

An Enum is a very useful "self-documenting code" technique.  For example, you may have a frame control with several radio buttons.  You can setup an Enum to assign text labels to the option button numbers and use the enum instead of the numbers.

For example, Let's say you have a frame control (fraMyFrame) with the option buttons labeled "This Thing", "That Thing", and "Other Thing" (numbered 1,2,3)
Set up an enum:

Public Enum MyFrame
  ThisThing=1
  ThatThing=2
  OtherThing=3
End Enum

Open in new window


Now, in your code, you can make your frame control coding more readable:

Select Case fraMyFrame
Case MyFrame.ThisThing
  (code here....)
Case MyFrame.ThatThing
  (code here...)
Case MyFrame.OtherThing
  (code here...)
Case Else
  (code here...)
End Select

Open in new window




If you haven't discovered them already, I also recommend you learn to use "user-defined variable type" (google it:  "vba user-defined variable type").
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sonny G

ASKER

Thanks, All !  You all added to my knowledge. Thanks!

Martin's article was very helpful.
Welcome.

»bp
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial