在 VB.NET 中反转枚举

发布于 2024-09-19 03:22:44 字数 427 浏览 5 评论 0原文

我需要在 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 技术交流群。

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

发布评论

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

评论(3

静待花开 2024-09-26 03:22:44

没有通用的方法可以做到这一点,因为枚举是整型,而整型上的 Not 会执行按位运算,这不是您想要的。但是,在您的情况下,您可以简单地编写一个反转方向的方法:

Module OrientationExtensions

    <Extension()>
    Public Function Invert(ByVal orientation As Orientation) As Orientation
        Return -1 * orientation
    End Function

End Module

用法:

Dim orientation As Orientation
orientation = Module1.Orientation.Ascending
orientation = orientation.Invert()

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:

Module OrientationExtensions

    <Extension()>
    Public Function Invert(ByVal orientation As Orientation) As Orientation
        Return -1 * orientation
    End Function

End Module

Usage:

Dim orientation As Orientation
orientation = Module1.Orientation.Ascending
orientation = orientation.Invert()
最笨的告白 2024-09-26 03:22:44

一般来说,您需要编写特定的方法,因为只有您知道每个枚举值的倒数是什么。

对于简单的枚举可能有更简单的方法,但如果值之间存在复杂的关系,则将其明确化是唯一的方法。

(您必须原谅 C# 风格的伪代码。)

public Orientation invert(Orientation original)
{
    Orientation result;
    switch (original)
    {
        case Orientation.Descending:
            result = Orientation.Ascending;
            break;
        case Orientation.Ascending:
            result = Orientation.Descending;
            break;
        default:
            result = original;
            break;
    }
    return result;
}

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.)

public Orientation invert(Orientation original)
{
    Orientation result;
    switch (original)
    {
        case Orientation.Descending:
            result = Orientation.Ascending;
            break;
        case Orientation.Ascending:
            result = Orientation.Descending;
            break;
        default:
            result = original;
            break;
    }
    return result;
}
清晨说晚安 2024-09-26 03:22:44

您可以创建一个扩展方法:

Imports System.Runtime.CompilerServices

Module OrientationExtensions

  <Extension()>
  Public Function Invert(ByVal orientation As Orientation) As Orientation
    If orientation = Orientation.Ascending Then
      Return Orientation.Descending
    ElseIf orientation = Orientation.Descending Then
      Return Orientation.Ascending
    Else
      Return Orientation.Undefined
    End If
  End Function

End Module

然后您可以像这样使用它:

Dim orientation As Orientation = Orientation.Ascending
Dim inverseOrientation As Orientation = orientation.Invert

You can create an extension method:

Imports System.Runtime.CompilerServices

Module OrientationExtensions

  <Extension()>
  Public Function Invert(ByVal orientation As Orientation) As Orientation
    If orientation = Orientation.Ascending Then
      Return Orientation.Descending
    ElseIf orientation = Orientation.Descending Then
      Return Orientation.Ascending
    Else
      Return Orientation.Undefined
    End If
  End Function

End Module

Then you can use it like this:

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