在 VB.NET 中反转枚举
我需要在 VB.NET (.NET 2) 中对枚举进行 NOT 操作,这可能吗?
<DefaultValue(0)> _
Public Enum Orientation
Descending = -1
Undefined = 0
Ascending = 1
End Enum
通过 ex 定义一个 Not 操作来执行
myObj1.Orientation = Not myObj2.Orientation
规则:
Desceding > Ascending,
Ascending > Desceding,
Undefined > Undefined
I need a NOT operation on Enumeration in VB.NET (.NET 2), is it possible?
<DefaultValue(0)> _
Public Enum Orientation
Descending = -1
Undefined = 0
Ascending = 1
End Enum
by ex define a Not operation in order to do
myObj1.Orientation = Not myObj2.Orientation
Rules:
Desceding > Ascending,
Ascending > Desceding,
Undefined > Undefined
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有通用的方法可以做到这一点,因为枚举是整型,而整型上的
Not
会执行按位运算,这不是您想要的。但是,在您的情况下,您可以简单地编写一个反转方向的方法:用法:
There is no general way to do this because enumerations are an integral type and
Not
on integral types does a bitwise operation which is not what you want here. However, in your case you can simply write a method that inverts the orientation:Usage:
一般来说,您需要编写特定的方法,因为只有您知道每个枚举值的倒数是什么。
对于简单的枚举可能有更简单的方法,但如果值之间存在复杂的关系,则将其明确化是唯一的方法。
(您必须原谅 C# 风格的伪代码。)
In general you'll need to code up a specific method as only you know what's the inverse of each of your enumeration values is.
There might be easier approaches for simple enumerations, but if there's a complicated relationship between the values making it explicit is the only way to go.
(You'll have to forgive the C# style pseudo code.)
您可以创建一个扩展方法:
然后您可以像这样使用它:
You can create an extension method:
Then you can use it like this: