Option Strict - Bit Enumerations

  • Thread starter Thread starter Mythran
  • Start date Start date
M

Mythran

Option Strict On

Public Enum MyBitFields As Integer
MyValue1 = 1
MyValue2 = 2
MyValue3 = 4
MyValue4 = 8
End Enum

<STAThread()> _
Public Sub Main()
Dim bits As MyBitFields = MyBitFields.MyValue1 + MyBitFields.MyValue2
End Sub


The above code doesn't work because the Option Strict'ness doesn't allow the
conversion from integer to enumeration? Can someone help me with this?
Option Strict is required and I don't control the enumeration. I am
Implementing the IDTCommandTarget interface and the parameter StatusOption
(EnvDTE.vsCommandStatus enumeration) is byref. I need to set this ref value
to vsCommandStatus.vsCommandStatusEnabled +
vsCommandStatus.vsCommandStatusSupported.

By the way, by required I meant my own requirements. I can turn it off, but
why should I have to? Isn't there a way to get around this?

Any help would be appreciated....

Thank you,
Mythran
 
Mythran said:
Option Strict On

Public Enum MyBitFields As Integer
MyValue1 = 1
MyValue2 = 2
MyValue3 = 4
MyValue4 = 8
End Enum

<STAThread()> _
Public Sub Main()
Dim bits As MyBitFields = MyBitFields.MyValue1 + MyBitFields.MyValue2
End Sub


The above code doesn't work because the Option Strict'ness doesn't allow
the conversion from integer to enumeration? Can someone help me with
this? Option Strict is required and I don't control the enumeration. I am
Implementing the IDTCommandTarget interface and the parameter StatusOption
(EnvDTE.vsCommandStatus enumeration) is byref. I need to set this ref
value to vsCommandStatus.vsCommandStatusEnabled +
vsCommandStatus.vsCommandStatusSupported.

By the way, by required I meant my own requirements. I can turn it off,
but why should I have to? Isn't there a way to get around this?

Any help would be appreciated....

Thank you,
Mythran

CType works :)

Mythran
 
Two things.

1. Use "Or" for bit combinations, not "+". Required with Option
Strict. Using CType to allow "+" is a hack.

2. Decorate the enum with the Flags attribute. Not required, but
encouraged.

HTH,

Sam

Option Strict On

Imports System

<Flags()> _
Public Enum MyBitFields As Integer
MyValue1 = 1
MyValue2 = 2
MyValue3 = 4
MyValue4 = 8
End Enum

Module Test

Public Sub Main()
Dim bits As MyBitFields = MyBitFields.MyValue1 Or
MyBitFields.MyValue2
Console.WriteLine(bits.ToString())
End Sub

End Module
 
Mythran said:
The above code doesn't work ...
Can someone help me with this?


<Flags()> _
Public Enum MyBitFields As Integer
MyValue1 = 1
MyValue2 = 2
MyValue3 = 4
MyValue4 = 8
End Enum


Dim bits As MyBitFields = MyBitFields.MyValue1 Or MyBitFields.MyValue2


See FlagsAttribute in Help for more info.

LFS
 
Back
Top