VB6 中枚举常量作为数组下标

发布于 2024-10-31 16:12:55 字数 375 浏览 0 评论 0原文

我正在定义一个枚举,它最终可能会被用作像这样的数组下标...

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 技术交流群。

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

发布评论

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

评论(3

非要怀念 2024-11-07 16:12:55

请注意,在 VB6 中使用“隐藏枚举值”也很常见,如下所示:

Public Enum MY_ENUM
    THIS_ENUM_VALUE = 1
    THAT_ENUM_VALUE
    THE_OTHER_ENUM_VALUE
    [_first] = THIS_ENUM_VALUE 
    [_last] = THE_OTHER_ENUM_VALUE
End Enum

您可以将其与其他建议结合起来:

Public Enum MY_ENUM
    [_first]
    THIS_ENUM_VALUE
    THAT_ENUM_VALUE
    THE_OTHER_ENUM_VALUE
    [_last]
End Enum

Dim MyArray(MY_ENUM.[_first] To MY_ENUM.[_last]) As MY_ENUM

Note that it's also fairly typical in VB6 to use "hidden enum values" like this:

Public Enum MY_ENUM
    THIS_ENUM_VALUE = 1
    THAT_ENUM_VALUE
    THE_OTHER_ENUM_VALUE
    [_first] = THIS_ENUM_VALUE 
    [_last] = THE_OTHER_ENUM_VALUE
End Enum

You could combine this with the other suggestions:

Public Enum MY_ENUM
    [_first]
    THIS_ENUM_VALUE
    THAT_ENUM_VALUE
    THE_OTHER_ENUM_VALUE
    [_last]
End Enum

Dim MyArray(MY_ENUM.[_first] To MY_ENUM.[_last]) As MY_ENUM
海的爱人是光 2024-11-07 16:12:55

我认为这就是要走的路。如果您按照概述进行操作,则可以在不破坏代码的情况下扩展枚举。

我认为唯一需要做的就是通知其他使用您的代码基于枚举声明数组的人使用 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).

悲念泪 2024-11-07 16:12:55

实践是,我们使用类似的声明

Public Enum MY_ENUM
    THIS_ENUM_VALUE = 1
    THAT_ENUM_VALUE
    THE_OTHER_ENUM_VALUE
    MAXIMUM_ENUM_VALUE
End Enum

,您甚至不需要编辑最后一个声明:) 当然,您的数组比需要的长一个元素,但恕我直言,这不是大问题。

Practice is that we use declaration like

Public Enum MY_ENUM
    THIS_ENUM_VALUE = 1
    THAT_ENUM_VALUE
    THE_OTHER_ENUM_VALUE
    MAXIMUM_ENUM_VALUE
End Enum

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.

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