嵌套 VB (VBA) 枚举
好吧,伙计们,我想实现嵌套枚举的效果,以便轻松对一些常量字符串进行分组。像下面的伪代码一样:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
上课是解决这个问题的方法。枚举只是长值,需要有限的选择。如果您需要这些对象具有其他功能/子功能,这将为您的对象提供最大的灵活性。
这是一个简单的布局:
gCountry 类:
gKS 类:
gMA 类:
测试它:
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:
gKS Class:
gMA Class:
Testing It:
我不相信您能够按照您希望的方式执行嵌入式枚举,因为枚举在 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.
谢谢,
所以。我决定使用类型来解决这个问题:
这已准备好供以后在代码中使用...
cya
THX,
So. i've decided to solve that issue using types:
And thats ready for later use on code...
cya