Option Strict On does not cause compilation error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Justin

    Option Strict On does not cause compilation error

    I have set Option Strict On
    I also have Enum AssemblyLineSta tus As Integer
    My question is how come there is no compilation error when I compile
    following code?

    If CInt(ddlStatus. SelectedItem.Va lue) = AssemblyLineSta tus.Client Then
    ' do whatever
    End If

    Shouldn't this cause a compilation error? I know in C#, you have to
    explicitly either convert enumation to int or int to enumuation.

    Just curious.
    Thanks


  • Larry Lard

    #2
    Re: Option Strict On does not cause compilation error


    Justin wrote:[color=blue]
    > I have set Option Strict On
    > I also have Enum AssemblyLineSta tus As Integer
    > My question is how come there is no compilation error when I compile
    > following code?
    >
    > If CInt(ddlStatus. SelectedItem.Va lue) = AssemblyLineSta tus.Client Then
    > ' do whatever
    > End If
    >
    > Shouldn't this cause a compilation error? I know in C#, you have to
    > explicitly either convert enumation to int or int to enumuation.[/color]

    VB is different, and less strict here. In VB, **a widening conversion
    (essentially, one guaranteed not to lose information) can always occur
    implicitly**. Conversion from an enum value to the enum's underlying
    type is obviously widening, so in the above code,
    AssemblyLineSta tus.Client gets widened to its Integer value.

    In C#, on the other hand, there is no such general rule of implicitness
    for widening conversions, and the set of implicit conversions is hence
    much smaller. All conversions between enum types and numeric types -
    even an enum's underlying type - must be explicit.

    VB.NET is stricter than VB6, but it still isn't as strict as C#.

    --
    Larry Lard
    Replies to group please

    Comment

    • Phill W.

      #3
      Re: Option Strict On does not cause compilation error

      Justin wrote:[color=blue]
      > I have set Option Strict On[/color]

      Good Start ...
      [color=blue]
      > I also have Enum AssemblyLineSta tus As Integer[/color]
      ....[color=blue]
      > If CInt(ddlStatus. SelectedItem.Va lue) = AssemblyLineSta tus.Client Then
      > ' do whatever
      > End If
      >
      > Shouldn't this cause a compilation error?[/color]

      CInt() returns an Integer and all Enums are defined as Integer.
      I suspect VB sees Enum-to-Integer as a Widening Conversion, much like
      Short-to-Integer.
      [color=blue]
      > I know in C#, you have to explicitly either convert[/color]

      If you want to do the comparsion "as" the Enumeration, try this

      ' Throws ArgumentExcepti on (IIRC) for invalid values
      Dim eValue as AssemblyLineSta tus _
      = CType(ddlStatus .SelectedItem.V alue _
      , AssemblyLineSta tus)

      If eValue = AssemblyLineSta tus.Client Then
      ' do whatever
      End If

      HTH,
      Phill W.

      Comment

      Working...