How to prevent comparing Enum with Integer?

I use the Enum type to prevent the use of false values: -

Public Class MyClass1

    Public Enum MyEnum As Byte
        FIRST
        SECOND
    End Enum

    Private my_var As MyEnum

    Public Property MyVar As MyEnum
        Get
            Return my_var
        End Get
        Set
            my_var = Value
        End Set
    End Property

End Class

Public Sub MyWorks

    Dim my_object As MyClass1 = New MyClass1

    my_object.MyVar = 1      ' Compilation Error
    my_object.MyVar = 33     ' Compilation Error

    If my_object.MyVar = 1 Then       ' No Compilation Error
    End If
    If my_object.MyVar = 27 Then      ' No Compilation Error
    End If
    If my_object.MyVar = 3.141 Then   ' No Compilation Error
    End If
    If my_object.MyVar = "Fred" Then  ' Compilation Error
    End If

End Sub

(Compiled with Option Strict On; Option Explicit On.

As I expected, attempts to assign the Enumeration property create compilation ( Option Strict On disallows implicit conversions from 'Integer' to 'MyClass.MyEnum') errors .

But there are no first three comparisons, where I rather hoped that they (especially the second and third, which are nonsense). The fourth comparison does not compile, but the error message seems strange: -

Option Strict On disallows implicit conversions from 'String' to 'Double'.

Does anyone know how I can make compilation errors appear for all of these comparisons?

+4
source share
1 answer
    my_object.MyVar = 1 ' Compilation Error

:
my_object.MyVar MyEnum 1 Integer (Byte/UShort/Etc.) , Option Strict On. :

    my_object.MyVar = MyEnum.SECOND
    ' .SECUND should mean "1" as FIRST would be "0" by default..

? "1" Byte, " " enum..! , () Option Strict . Strict Off ! , Option Strict On :

    Dim MyByteVar As Byte = 1 ' No compilation error

MyByteVar Byte , "1" Integer. , MyByteVar Byte, "1" , . .

. "1" MyEnum Strict On, , 1 , , . , , MSDN Option Strict.. , " " , :

, , . (Integer → Byte) Explicit On.
EDIT 2: ^^ , :/, , , -..

  • , .
  • .
  • , , .
  • . , , - , .

Enumerations, Option Strict Option Explicit, Dim my_var As MyEnum = 1. , /​​ , , , , -.

    If my_object.MyVar = 1 Then
    ' my_object.MyVar = MyEnum.FIRST = 0 -> Byte (strongly typed)
    ' 1 -> Integer by default
    ' Convert my_object.MyVar to Integer (always a widening conversion)
    ' 0 is different from 1 (Integer to Integer comparison)
    ' -> FALSE - No compilation error


    If my_object.MyVar = 27 Then
    ' Same as above and will return FALSE

    If my_object.MyVar = 3.141
    ' my_object.MyVar = MyEnum.FIRST = 0 -> Byte (strongly typed)
    ' 3.141 -> will default to Double (because you didn't used Type Character flag)
    ' Convert my_object.MyVar to Double (always a widening conversion)
    ' 0 is different from 3.141 (Double to Double comparison)
    ' -> FALSE - No compilation error

my_object.MyVar MyEnum.SECOND, TRUE:

    If my_object.MyVar = 1
    ' my_object.MyVar = MyEnum.SECUND = 1 -> Byte (strongly typed)
    ' 1 -> will default to Integer
    ' Convert my_object.MyVar to Integer = 1
    ' 1 = 1 => TRUE !

, :

    If my_object.MyVar = "Fred" Then '...

Strict On, Double String . , Strict . Double, Byte? , . Byte → Integer → Long → .. → Double.

shoud my_object.MyVar String, "Fred" . ( ), Option Strict On.


, . , . , , Strict, , , , .

= > /, ?
 = > Byte → Double , Strict ?

, ...

+4
source

All Articles