Option Strict - Bit Enumerations

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
 
M

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
 
S

Samuel R. Neff

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
 
L

Larry Serflaten

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top