如何测试数字中是否包含枚举?

发布于 2025-02-03 21:51:00 字数 526 浏览 2 评论 0原文

在vb6中,我将几个dt_draw_flag值组合在一起:

dim lMyFlags As DT_DRAW_FLAG
lMyFlags = DT_CENTER OR DT_VCENTER OR DT_RTLREADING

这将导致lmyflags = 131077

现在测试是否在这样的组合标志值中包含某个标志,我会执行以下操作:

If (131077  And DT_RTLREADING) = DT_RTLREADING Then
    'DT_RTLREADING is contained. 
Else
    'DT_RTLREADING is NOT contained. 
End Enum

我将如何在vb.net中执行此操作?

我是否仍然必须使用这种“纯数学”方法,还是有类似的方法……

lMyFlags.ContainsEnum(DT_RTLREADING)

我还没有找到?

谢谢你!

In VB6 I combine several DT_DRAW_FLAG values like this:

dim lMyFlags As DT_DRAW_FLAG
lMyFlags = DT_CENTER OR DT_VCENTER OR DT_RTLREADING

This would result in lMyFlags = 131077

Now to test if a certain flag is contained in such a combine flags value, I would do the following:

If (131077  And DT_RTLREADING) = DT_RTLREADING Then
    'DT_RTLREADING is contained. 
Else
    'DT_RTLREADING is NOT contained. 
End Enum

How would I do this in VB.NET?

Do I still have to use this "pure math" approach, or is there a method like...

lMyFlags.ContainsEnum(DT_RTLREADING)

... which I have not found yet?

Thank you!

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

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

发布评论

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

评论(2

无悔心 2025-02-10 21:51:00

如果您有这样的枚举声明

<Flags()> Public Enum DT_DRAW_FLAG As Integer
    DT_CENTER = 1
    DT_VCENTER = 2
    DT_RTLREADING = 4
End Enum

,那么您可以使用hasflag来执行逻辑,

    Dim lMyFlags As DT_DRAW_FLAG = DT_DRAW_FLAG.DT_CENTER Or DT_DRAW_FLAG.DT_RTLREADING
    lMyFlags.HasFlag(DT_DRAW_FLAG.DT_RTLREADING) ' => true

重要的一点是单个枚举值是两个的幂。

If you have an enum declaration like this

<Flags()> Public Enum DT_DRAW_FLAG As Integer
    DT_CENTER = 1
    DT_VCENTER = 2
    DT_RTLREADING = 4
End Enum

Then you can use HasFlag to do your logic

    Dim lMyFlags As DT_DRAW_FLAG = DT_DRAW_FLAG.DT_CENTER Or DT_DRAW_FLAG.DT_RTLREADING
    lMyFlags.HasFlag(DT_DRAW_FLAG.DT_RTLREADING) ' => true

The important point here is that the single enum values are powers of two.

最终幸福 2025-02-10 21:51:00

公认的答案很好,它有效。就您而言,您的枚举可能已经具有标志属性,因为它的三个功能的组合为2:2^0 = 1,2^2 = 4,而2^17 = 131072

您的枚举可能看起来像

<Flags>
Public Enum DT_DRAW_FLAG As Long
    ''' <summary>
    ''' 2 ^ 0
    ''' </summary>
    DT_CENTER = 1
    ' 2 ^ 1 in here
    ''' <summary>
    ''' 2 ^ 4
    ''' </summary>
    DT_VCENTER = 4
    ' 2 ^ 3 through 2 ^ 16 in here
    ''' <summary>
    ''' 2 ^ 17
    ''' </summary>
    DT_RTLREADING = 131072
End Enum

< a href =“ https://learn.microsoft.com/en-us/dotnet/api/system.flagsattribute?view=net-6.0” rel =“ nofollow noreferrer”> flags

表示可以将枚举视为位。也就是说,一组标志。

但是,无论它是否具有标志属性,您都可以使用位和钻头以相同的方式对其进行处理。我相信Hasflags函数只是位逻辑的速记:

' Bitwise logic
If (lMyFlags And DT_DRAW_FLAG.DT_RTLREADING) = DT_DRAW_FLAG.DT_RTLREADING Then
    ' Reading is contained
Else
    ' Reading is not contained
End If
' HasFlags
If lMyFlags.HasFlag(DT_DRAW_FLAG.DT_RTLREADING) Then
    ' Reading is contained
Else
    ' Reading is not contained
End If

它肯定是较少的代码。

请注意,您不会以所显示的方式结合枚举,而没有其他转换。使用位或这样做

Dim lMyFlags = DT_DRAW_FLAG.DT_CENTER Or DT_DRAW_FLAG.DT_VCENTER Or DT_DRAW_FLAG.DT_RTLREADING

,您可以使用enum.hasflag,无论您是否使用了标志属性。据我所知,该属性只是用来向消费者发出信号,表明这些值是两个的不同功能,并且可以在它们上执行位逻辑。标志属性严格没有任何事情,因此消费者有一些信任可以知道该怎么做(我们假设原始作者也知道它)

Accepted answer is good, it works. In your case, your Enum might already have the flags attribute, because it is a combination of three powers of 2: 2^0=1, 2^2=4, and 2^17=131072

Your enum may look like this

<Flags>
Public Enum DT_DRAW_FLAG As Long
    ''' <summary>
    ''' 2 ^ 0
    ''' </summary>
    DT_CENTER = 1
    ' 2 ^ 1 in here
    ''' <summary>
    ''' 2 ^ 4
    ''' </summary>
    DT_VCENTER = 4
    ' 2 ^ 3 through 2 ^ 16 in here
    ''' <summary>
    ''' 2 ^ 17
    ''' </summary>
    DT_RTLREADING = 131072
End Enum

The Flags Attribute

Indicates that an enumeration can be treated as a bit field; that is, a set of flags.

However, whether or not it has the Flags attribute, you can treat it the same way using bitwise And. I believe the HasFlags function is just shorthand for the bitwise logic:

' Bitwise logic
If (lMyFlags And DT_DRAW_FLAG.DT_RTLREADING) = DT_DRAW_FLAG.DT_RTLREADING Then
    ' Reading is contained
Else
    ' Reading is not contained
End If
' HasFlags
If lMyFlags.HasFlag(DT_DRAW_FLAG.DT_RTLREADING) Then
    ' Reading is contained
Else
    ' Reading is not contained
End If

It is certainly less code.

Note, you don't combine enums in the way that you have shown, without some additional conversion. Use bitwise Or to do that

Dim lMyFlags = DT_DRAW_FLAG.DT_CENTER Or DT_DRAW_FLAG.DT_VCENTER Or DT_DRAW_FLAG.DT_RTLREADING

Also, you can use Enum.HasFlag regardless of whether you used the Flags Attribute. As far as I know, the attribute is just used to signal to the consumer that the values are distinct powers of two, and bitwise logic can be performed on them. There is nothing strictly going on with the flags attribute so there's some trust in the consumer to know what to do with it (and we assume the original author knew about it, too)

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