嵌套 VB (VBA) 枚举

发布于 2024-11-06 11:49:21 字数 474 浏览 1 评论 0原文

好吧,伙计们,我想实现嵌套枚举的效果,以便轻松对一些常量字符串进行分组。像下面的伪代码一样:

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

所以下面的代码应该输出“”消息:

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

我不想使用类型或类,因为不需要初始化,因为所有值都是已知的(正如我所说的常量) )。

有什么建议吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夕色琉璃 2024-11-13 11:49:21

上课是解决这个问题的方法。枚举只是长值,需要有限的选择。如果您需要这些对象具有其他功能/子功能,这将为您的对象提供最大的灵活性。

这是一个简单的布局:

gCountry 类:

Public North As gMA
Public South As gKS

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

gKS 类:

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

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

gMA 类:

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

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

测试它:

Public Sub TestIt()

    Dim USA As New gCountry

    MsgBox USA.North.Dodge
End Sub

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
兲鉂ぱ嘚淚 2024-11-13 11:49:21

我不相信您能够按照您希望的方式执行嵌入式枚举,因为枚举在 CLR 中被视为基元(来源)。您也可以尝试在整数中嵌入整数。

我知道您说过您不想使用类,但静态类就是为了填补 .NET 世界的这种情况。无需初始化即可轻松、任意地访问它,并且编译时速度很快。 此页面提供有关静态的详细信息如果您不熟悉它们。您应该能够做任何您需要做的事情,以便在该类中按照您想要的方式设置信息,无论是多个静态类、哈希表、多维数组还是您拥有的任何内容。

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.

糖果控 2024-11-13 11:49:21

谢谢,

所以。我决定使用类型来解决这个问题:

    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

这已准备好供以后在代码中使用...

cya

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文