Verification code error?

Example:

Public Enum Vehicles As Integer
    Car = 0
    MotorBike = 1
    Plane = 2
End Enum

      

To get the value of each vehicle ( 0

, 1

, 2

), I believe that I need to do it as follows:

DirectCast(Vehicles.Car, Integer)

      

This works great, however Resharper throws this warning in intellisense:

"Cannot express expression of type Vehicles to enter Integer"

Why? This is mistake? Is there any other way for me to get the value of each member without conversion? (Cint, etc.)

+3


source to share


1 answer


First, the convention dictates that your enumeration should be named Vehicle

, that is, single, not Vehicles

, that is, plural. You should only use a plural name for the enumeration if you are using 2 values ​​for values ​​and apply an attribute Flags

, thereby allowing a variable of this type to store multiple values ​​at the same time, eg.

<Flags>
Public Enum Vehicles As Integer
    Car = 1
    MotorBike = 2
    Plane = 4
End Enum

      



Obviously this is appropriate in this particular case, but there are many enums declared this way.

As for the question, it appears to be a bug in ReSharper because the VB code compiles and runs without issue. My guess is that when used DirectCast

, ReSharper is looking for a direct inheritance or implementation relationship that it won't find in the case of enums and integral types. If you use CInt

this instead, you will not receive this warning because it CInt

does not require such an attitude.

+2


source







All Articles