VB6 中枚举常量作为数组下标
我正在定义一个枚举,它最终可能会被用作像这样的数组下标...
Public Enum MY_ENUM
THIS_ENUM_VALUE = 1
THAT_ENUM_VALUE
THE_OTHER_ENUM_VALUE
MAXIMUM_ENUM_VALUE = THE_OTHER_ENUM_VALUE
End Enum
理论上,任何添加另一个枚举的人都会纠正最大枚举值,并且任何定义为的数组
Dim my_array(MAXIMUM_ENUM_VALUE) As Integer
都会根据需要放大。
我的问题是:我应该这样做吗?如果不应该,你有什么建议?
I'm defining an enumeration which may end up being used as an array subscript like this...
Public Enum MY_ENUM
THIS_ENUM_VALUE = 1
THAT_ENUM_VALUE
THE_OTHER_ENUM_VALUE
MAXIMUM_ENUM_VALUE = THE_OTHER_ENUM_VALUE
End Enum
The theory is that anyone adding another enum will correct the maximum enum value and any array defined as
Dim my_array(MAXIMUM_ENUM_VALUE) As Integer
will get enlarged as required.
My question is: ought I to be doing this, and if not, what would you suggest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意,在 VB6 中使用“隐藏枚举值”也很常见,如下所示:
您可以将其与其他建议结合起来:
Note that it's also fairly typical in VB6 to use "hidden enum values" like this:
You could combine this with the other suggestions:
我认为这就是要走的路。如果您按照概述进行操作,则可以在不破坏代码的情况下扩展枚举。
我认为唯一需要做的就是通知其他使用您的代码基于枚举声明数组的人使用
MAXIMUM_ENUM_VALUE
而不是他们添加的枚举值(在本例中为THE_OTHER_ENUM_VALUE )。
I think that's the way to go. If you do as you've outlined, the enum can be expanded without breaking the code.
The only thing I see needs to be done is to inform others who use your code to declare arrays based on the enum to use
MAXIMUM_ENUM_VALUE
instead of their enum value they added (in this case,THE_OTHER_ENUM_VALUE
).实践是,我们使用类似的声明
,您甚至不需要编辑最后一个声明:) 当然,您的数组比需要的长一个元素,但恕我直言,这不是大问题。
Practice is that we use declaration like
and you even don't need edit last declaration :) Sure you have your arrays one element longer than needed, but IMHO that is not big problem.