简体   繁体   English

嵌套VB(VBA)枚举

[英]Nested VB (VBA) Enumeration

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. 谢谢。

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: gCountry类别:

Public North As gMA
Public South As gKS

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

gKS Class: gKS类别:

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

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

gMA Class: gMA类别:

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

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 ). 我不相信您将能够以您希望的方式进行嵌入式枚举,因为枚举被视为CLR( source )中的原语。 You may as well try to embed ints within ints. 您也可以尝试将int嵌入int中。

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. 我意识到您说过您不想使用类,但这是静态类用于填充.NET世界的一种情况。 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. 您应该能够做任何需要做的事情,以便在该类中以所需的方式设置信息,无论是多个静态类,哈希表,多维数组还是任何您拥有的东西。

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 ya

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM