验证枚举值在 VB.NET 中是否有效

发布于 2024-10-25 05:45:14 字数 585 浏览 1 评论 0原文

我的英语可能不是很好,所以如果我说得不清楚,请原谅。

我的任务是验证枚举包含递增值。

代码如下所示:

Public Enum AccountMessageDescriptor
  AccountInvalid = 1001
  AccountReviewPending=1002
  ...
  InvalidOperationOnAccount=1382
End Enum

每个枚举值也有一个描述(自定义)属性。文件和描述属性在其他地方使用...看起来程序员更新了枚举并使用自定义属性来获取帮助消息,而不是具有新整数值的资源文件等...

例如,可能会添加另一个错误:

NewEnumItem=1383

我需要确保(在构建期间,我们有一个自定义的 MSBuild 任务,该任务会执行许多其他处理)枚举是否按顺序递增并且尚未使用。

有人建议使用集合,检查枚举值是否已存在,如果不存在则插入它。

我正在考虑迭代并检查当前值是否为前一个值+1(因此它是连续的,并且不能已在使用中,因为它必须始终为+1)。这似乎有缺陷,还是我不理解 .NET 枚举的某些内容?

埃米

My English may not be very good, so please excuse me if I don't make sense.

I am tasked with verifying an Enum contains a incrementing value.

The code looks like this:

Public Enum AccountMessageDescriptor
  AccountInvalid = 1001
  AccountReviewPending=1002
  ...
  InvalidOperationOnAccount=1382
End Enum

Each Enum value has a description (Custom) attribute also. The file and description attribute is used elsewhere... It looks like the programmers update the enums and use the custom attribute for help messages instead of resource files etc with a new integer value...

For example, another error may be added as:

NewEnumItem=1383

I need to make sure (during build time we have a custom MSBuild task that does lots of other processing) if the enum is incrementing sequentially and that its not already in use.

Someone suggested using a collection, check if the enum value already exists and if not insert it.

I was thinking of just iterating through and checking if the current value is +1 the previous value (so its sequential and it can't be already in use because it has to always be +1). Does this seem flawed or am I not understanding something about .NET enums?

Emmi

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

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

发布评论

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

评论(2

你曾走过我的故事 2024-11-01 05:45:14

如果您设置第一个值,然后排除所有后续值,它们将是增量值。例如...

Public Enum testenum
    first = 1001
    second
    third
End Enum

所以 second 将是 1002 third 将是 1003 等等...

如果由于某种原因您确实需要跳过某些值,那么您可以强制增量为从更高的值开始,例如......

Public Enum testenum
    first = 1001
    second
    third
    fourth = 2000
    fifth
End Enum

因此,在此示例中,fourth 将是 2000,fifth 将是 2001

If you set the first value and then exclude all following values they will be incremental. eg...

Public Enum testenum
    first = 1001
    second
    third
End Enum

So second will be 1002 third will be 1003 etc etc...

If for some reason you do need to skip certain values then you can force the increment to start at a higher value eg..

Public Enum testenum
    first = 1001
    second
    third
    fourth = 2000
    fifth
End Enum

So in this example fourth will be 2000 and fifth will be 2001

往事随风而去 2024-11-01 05:45:14

要迭代枚举,您可以尝试以下操作:

For Each amd As AccountMessageDescriptor In [Enum].GetValues(GetType(AccountMessageDescriptor))
        'Do your thing here
Next

我不确定这种方式的值是否是连续的,但快速测试可以为您解决这个问题。

或者您可以将所有值存储在一个数组中并检查这些值是否是连续的:

Dim allAMD As AccountMessageDescriptor() = DirectCast([Enum].GetValues(GetType(AccountMessageDescriptor)), AccountMessageDescriptor())

'Loop through array and check with previous

To iterate through an enum, you could try the following:

For Each amd As AccountMessageDescriptor In [Enum].GetValues(GetType(AccountMessageDescriptor))
        'Do your thing here
Next

I'm not enterily sure if this way the values are sequential, but a quick test could sort this out for you.

Or you can store all the values in an array and check if the values are sequential:

Dim allAMD As AccountMessageDescriptor() = DirectCast([Enum].GetValues(GetType(AccountMessageDescriptor)), AccountMessageDescriptor())

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