0

Ok guys, well i'd like to achieve an effect of nested enumeration for easy grouping some constant strings. Something like the pseudo code bellow:

Enum gKS
    Colby = "Hello"
    Hays = "World"
end Enum

Enum gMA
    Dodge = "Seven"
    Muscatine = "Ports"
end Enum

Enum gCountry
    north as gMA
    south as gKS
end Enum

Public USA as gCountry

So the code bellow should output a "Seven" message:

sub dol()
    msgbox USA.north.Dodge
end sub

I don't want use types or classes because no initialisation is needed since all values are know (constants as i said).

Any suggestions?

thx.

3
  • In what are you using your VBA (i.e. Excel, Access)? You can create class modules without requiring installation Commented May 12, 2011 at 22:15
  • No can do. In fact, all your sample Enum declarations are illegal in VBA. The only values that Enum elements can take are Long (Integer). See answer by YYY for a useful suggestion. Commented May 12, 2011 at 22:48
  • Thomas: i'm using Excel VBA for it. And Roland the code above is a 'pseudo' code... Commented May 13, 2011 at 11:48

3 Answers 3

3

Classes are the way to go on this. Enums are simply long values, where limited selection is needed. This will allow for the greatest flexibility with your objects, in case you need these objects to have other function/subs.

Here is a simple layout:

gCountry Class:

Public North As gMA
Public South As gKS

Private Sub Class_Initialize()
    Set North = New gMA
    Set South = New gKS
End Sub

gKS Class:

Public Property Get Colby() As String
    Colby = "Hello"
End Property

Public Property Get Hays() As String
    Hays = "World"
End Property

gMA Class:

Public Property Get Dodge() As String
    Dodge = "Seven"
End Property

Public Property Get Muscatine() As String
    Muscatine = "Ports"
End Property

Testing It:

Public Sub TestIt()

    Dim USA As New gCountry

    MsgBox USA.North.Dodge
End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah... Classes can be used, but, there ll be dozens of diferent classes and the code maintenance is unpraticable using the vba ide.
I don't understand your logic, how are types or enums more practical than classes for clearly defined separate objects?
Fink, just because i want to avoid all those 'public property Get' calls. I've found below just what i need besides its not the most elegant solution its working fine so far.
2

I don't believe you're going to be able to do embedded enums the way you are hoping, because enums are considered primitives in the CLR (source). You may as well try to embed ints within ints.

I realize you said you didn't want to use classes, but this is the sort of situation static classes are meant to fill in the .NET world. It will be easily and arbitrarily accessible with no initialization and quick when compiled. This page has more information on statics if you're not familiar with them. You should be able to do whatever you need to do to get the information set up the way you want within that class, be it multiple static classes, a hash table, a multi-dimensional array, or whatever have you.

1 Comment

Cant do it using classes for two reasons: 1st theres no static classes on VBA so far. 2nd making it that way i would have to create a whole new class for every "country" object.
2

THX,

So. i've decided to solve that issue using types:

    Public Type fCAOCC
        itRGI As String
        ...
    End Type
    Public Type fCAOBF
        itRGI As String
        ibEnviar As String
        ...
    End Type
    Public Type gTELAS
        CAOBF As fCAOBF
        CAOCC As fCAOCC
        ...
    End Type

    Public CSI As gTELAS

    Sub iniGLOBALS()
        CSI.CAOBF.itRGI = "DIVNRRGILIG"
        CSI.CAOBF.ibEnviar = "DUMMYNAME1"
        CSI.CAOCC.itRGI = "Hello"
...
    End Sub

And thats ready for later use on code...

cya

Comments

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.