1

I am quite new to Class Modules.

I know how I can make an Enumeration in a Class, but is it possible to refer to its values too?

For example:

'Class Module
Public Enum Alphabeth
A = 1
B
C
End enum

Let's say I made an instance called 'Alpha' of this Class, can I then somehow refer to the value of "C" in the Enum? Something like Alpha.myEnum.C (which should give a value of "3").

4
  • From both inside and outside the class you can refer to the Enum like this: Alphabeth.C Commented Aug 18, 2021 at 12:15
  • But I cannot use the Class name with that? I was hoping I could combine Classname + Enumname + Enum, like Alpha.myEnum.C... Commented Aug 18, 2021 at 12:16
  • Does this answer your question? Nested VB (VBA) Enumeration Commented Aug 18, 2021 at 12:23
  • @Rezzy777 I don't believe you can include the class name like you want. Typically Enums are used as parameters or return values on methods. Commented Aug 18, 2021 at 12:30

2 Answers 2

2

In class module:

Public Enum Alphabet
    a = 1
    b
    c
End Enum

Public Property Get GetEnum(Letter As String) As Long
    Select Case Letter
        Case "a"
            GetEnum = Alphabet.a
        Case "b"
            GetEnum = Alphabet.b
        Case "c"
            GetEnum = Alphabet.c
        ' etc
    End Select
End Property

In a regular module:

Sub UseEnum()

    Dim Alphabet As clsAlphabet
    Set Alphabet = New clsAlphabet
    
    MsgBox Alphabet.GetEnum("a")

End Sub

Edit: Found a better solucion: In class module:

Public Enum Alphabet
    a = 1
    b
    c
End Enum

Public Property Get Alphabet(Letter As Alphabet) As Alphabet
    Alphabet = Letter
End Property

In a regular module:

Sub UseEnum()

    Dim Alphabet As clsAlphabet
    Set Alphabet = New clsAlphabet
    
    MsgBox Alphabet.Alphabet(a)

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is exactly what I meant. I actually thought using an Enum (inside a Class) would be better, but now I see it only makes everything very complex since you cannot refer to it directly. I'll try something else. Thanks again everyone!
1

Enum is not a class... It is a statement.

  1. Please, copy the Enum declaration on top of a standard module (in the declarations area):
Public Enum Alphabeth
    A = 1
    b = 2
    c = 3
End Enum
  1. Copy the next Sub in the same module, or an another one and run it:
Sub testEnumAlph()
   Debug.Print Alphabeth.A, Alphabeth.b, Alphabeth.c
End Sub

If write Alphabeth, intellisense will suggest the Enum members...

It will return in Immediate Window (Ctrl + G, being in VBE): 1 2 3...

5 Comments

I know it's not a Class, so it cannot be used in a Class either? Something like Class.myEnum.C?
@Rezzy777 You can, but not in the same way. You should create methods to get and let values and use them to return the enum members. I can show you a simple class with an example, if needed...
Thank you, but that won't be necessary anymore ;-)
@FaneDuru fyi might want to see how to define invisible [_Start] member to the Type definition at Type inside class module
@T.M. I know about these invisible members. I was preparing a class example containing Enum with an invisible member, but OP did not say that he is interested in an example and somebody else posted one. But, I will follow the link...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.