Get Text Value of Enum

speakers_86

Registered User.
Local time
Today, 06:43
Joined
May 17, 2007
Messages
1,919
Any ideas?

Code:
Public Enum Test
a=0
end enum


How can I return a instead of 0?
 
It's one of those things that can't be done, that's why it's called an enumeration. If you want that sort of mapping then you want to create a class or an even simpler solution would be a Dictionary.
 
The point of an Enum is to allow you to type the text value and have VBA substitute the numeric value similar to how the various VB, Access, DAO, etc. enums presently work. So for example, you use vbYes or vbBlack or vbOkayOnly or vbCRLF rather than the actual numeric values.

In your case, you would refer to a as test.a
When you type "test.", Access will provide intellisense to show you the "a" option.
 
So I finally took the time to figure this out. It seems the Type procedure is what I was looking for. But here is what I tried out:

Code:
Public g_strVariable As typTest

Type typTest
    str As String
    int As Integer
End Type

Sub SetTypeValues()
    g_strVariable.str = "a"
End Sub

If you run the sub SetTypeValues, you can now declare a variable as typTest, use intellisense, and return any data type.



An alternative is class modules:

Code:
'In a class module
Property Get strTest() As String
    strTest = "test"
End Property

'in a standard module
Sub ClassTest()
    Dim a As Class1
    Set a = New Class1
    MsgBox a.strTest
End Sub


Again, you can use intellisense and return any data type. It seems the Type option is the way to go for me.
 
Type is not the same as Enum.

A Type is like a simple version of a Class.

The Type typtest you set up above is defining an object prototype which contains two variables str and int. (These would be properties if it was a Class as you have shown in your second example.)

This is using the properties part of Intellisense.

Enum is setting the named constants which can be used as values for a particular declared variable type. It works further down the Intellisense when you are offered the names as potential values for the variable of a particular type.

Enum can be used to designate the allowed values for an object's property (which is of course a variable itself).

Chip Pearson has a good explanation of Enum
http://www.cpearson.com/excel/Enums.aspx
 

Users who are viewing this thread

Back
Top Bottom