strict data type check

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sam Sungshik Kong

    #1

    strict data type check

    Hello!

    I studied C# a little bit and am trying to compare it with VB.Net.

    There's 'Option Strict' in VB.Net.
    I thought that if I turn it on, it is as strict as C# when checking types.

    See the following codes.

    <vb.net>
    Option Strict On
    Imports System
    Module Main
    Sub Main()
    Dim S As Short = 0
    S += 1 '*******Error!! ! It should be S += 1S.
    Console.WriteLi ne(S.ToString() )
    End Sub
    End Module
    </vb.net>

    The above code generates an error because in the line of S+=1, 1 is integer
    and S is Short.

    In C#, the same code was compiled ok.

    <csharp>
    short s = 0;
    s += 1; //okay
    </csharp>

    What is the rule for that?
    An integer can be assigned to a short?

    If so, VB.Net with 'Option Strict On' is more strict than C#.
    What can I do to make C# compiler check very strictly?

    Thanks in advance!

    Sam



  • Ming Chen

    #2
    Re: strict data type check

    Well, C# compiler is checking very strictly in this case.
    The reason it doesn't report any error is that C# defines some Implicit
    constant expression conversion rules.

    See C# language spec 6.1.6: A constant-expression (Section 7.15) of type int
    can be converted to type sbyte, byte, short, ushort, uint, or ulong,
    provided the value of the constant-expression is within the range of the
    destination type.
    So the interger 1 in your code is safely converted into 1S. But if the
    number in your code is 65536, which is out of the range of a "short", the
    compiler will complain.

    Is there any way to make the C# compiler act just like the VB.net one? I
    don't know. :(
    Provided the C# compiler's behavior is pretty safe, I see no point of doing
    so.

    Hope it helps.
    Ming Chen

    "Sam Sungshik Kong" <ssk@chol.net > wrote in message
    news:%23tAevYTV DHA.2040@TK2MSF TNGP11.phx.gbl. ..[color=blue]
    > Hello!
    >
    > I studied C# a little bit and am trying to compare it with VB.Net.
    >
    > There's 'Option Strict' in VB.Net.
    > I thought that if I turn it on, it is as strict as C# when checking types.
    >
    > See the following codes.
    >
    > <vb.net>
    > Option Strict On
    > Imports System
    > Module Main
    > Sub Main()
    > Dim S As Short = 0
    > S += 1 '*******Error!! ! It should be S += 1S.
    > Console.WriteLi ne(S.ToString() )
    > End Sub
    > End Module
    > </vb.net>
    >
    > The above code generates an error because in the line of S+=1, 1 is[/color]
    integer[color=blue]
    > and S is Short.
    >
    > In C#, the same code was compiled ok.
    >
    > <csharp>
    > short s = 0;
    > s += 1; //okay
    > </csharp>
    >
    > What is the rule for that?
    > An integer can be assigned to a short?
    >
    > If so, VB.Net with 'Option Strict On' is more strict than C#.
    > What can I do to make C# compiler check very strictly?
    >
    > Thanks in advance!
    >
    > Sam
    >
    >
    >[/color]


    Comment

    • Val Savvateev

      #3
      Re: strict data type check

      Sam,

      What you're seeing is by desing. Section "6.1.6 Implicit constant expression
      conversions" of C# specs states:

      "An implicit constant expression conversion permits the following
      conversions:
      a.. A constant-expression (Section 7.15) of type int can be converted to
      type sbyte, byte, short, ushort, uint, or ulong, provided the value of the
      constant-expression is within the range of the destination type.
      ............... .."

      The mentioned range checking makes such implicit convertions safe, so
      there's really no need for "strict" option analogue. In fact I would say
      that C# has that options always on compared to VB....

      I'd suggest looking through the entire chapter "6. Conversions" to get
      better understanding of conversions...

      Regards,
      Val.


      "Sam Sungshik Kong" <ssk@chol.net > wrote in message
      news:%23tAevYTV DHA.2040@TK2MSF TNGP11.phx.gbl. ..[color=blue]
      > Hello!
      >
      > I studied C# a little bit and am trying to compare it with VB.Net.
      >
      > There's 'Option Strict' in VB.Net.
      > I thought that if I turn it on, it is as strict as C# when checking types.
      >
      > See the following codes.
      >
      > <vb.net>
      > Option Strict On
      > Imports System
      > Module Main
      > Sub Main()
      > Dim S As Short = 0
      > S += 1 '*******Error!! ! It should be S += 1S.
      > Console.WriteLi ne(S.ToString() )
      > End Sub
      > End Module
      > </vb.net>
      >
      > The above code generates an error because in the line of S+=1, 1 is[/color]
      integer[color=blue]
      > and S is Short.
      >
      > In C#, the same code was compiled ok.
      >
      > <csharp>
      > short s = 0;
      > s += 1; //okay
      > </csharp>
      >
      > What is the rule for that?
      > An integer can be assigned to a short?
      >
      > If so, VB.Net with 'Option Strict On' is more strict than C#.
      > What can I do to make C# compiler check very strictly?
      >
      > Thanks in advance!
      >
      > Sam
      >
      >
      >[/color]


      Comment

      Working...