验证枚举值在 VB.NET 中是否有效
我的英语可能不是很好,所以如果我说得不清楚,请原谅。
我的任务是验证枚举包含递增值。
代码如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您设置第一个值,然后排除所有后续值,它们将是增量值。例如...
所以
second
将是 1002third
将是 1003 等等...如果由于某种原因您确实需要跳过某些值,那么您可以强制增量为从更高的值开始,例如......
因此,在此示例中,
fourth
将是 2000,fifth
将是 2001If you set the first value and then exclude all following values they will be incremental. eg...
So
second
will be 1002third
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..
So in this example
fourth
will be 2000 andfifth
will be 2001要迭代枚举,您可以尝试以下操作:
我不确定这种方式的值是否是连续的,但快速测试可以为您解决这个问题。
或者您可以将所有值存储在一个数组中并检查这些值是否是连续的:
To iterate through an enum, you could try the following:
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: