Option Strict On

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

    #1

    Option Strict On

    With the option strict On set.....

    Dim fs As FileStream = File.OpenRead(s trFile)

    With fs
    Dim buffer(fs.Lengt h) As Byte ' <--- Option Strict On disallows implicit
    conversions from 'Long' to 'Integer'.
    ' other stuff..
    End With

    been a while i did VB code... can somebody help me what I am doing wrong
    here...

    Vijay


  • Renze de Waal

    #2
    Re: Option Strict On

    Op Fri, 31 Mar 2006 15:12:05 -0600 schreef Vijay:
    [color=blue]
    > With the option strict On set.....
    >
    > Dim fs As FileStream = File.OpenRead(s trFile)
    >
    > With fs
    > Dim buffer(fs.Lengt h) As Byte ' <--- Option Strict On disallows implicit
    > conversions from 'Long' to 'Integer'.
    > ' other stuff..
    > End With
    >
    > been a while i did VB code... can somebody help me what I am doing wrong
    > here...
    >
    > Vijay[/color]


    Vijay,

    The report is about the array bound. fs.length is a long (64 bits signed
    integer) and the dim statement expects an integer. VB would have to
    implicitely convert the long to an integer (32 bits signed integer).

    Unles you have really big files, the type conversion will be safe here.
    Make it explicit by using ctype(fs.length ,integer) instead of fs.length.

    Another note in case you don't realise: value fs.length is the upper bound
    of the array. The array starts at index 0, so you are declaring an array of
    fs.length + 1 long.

    Renze.

    Comment

    • zacks@construction-imaging.com

      #3
      Re: Option Strict On

      The length of an array is defined as a 32-bit integer. The Length
      property in the FileStream class is a Long. Convert it to an Int, but
      you should really check to see if the length of the file yo are trying
      to read isn't too large to fit in a 32-bit int.

      Comment

      • Vijay

        #4
        Re: Option Strict On

        Thanks Renze.. I just posted here to make sure I was not typing something
        wrong... its been a while I did VB coding...

        Vijay

        "Renze de Waal" <renze@dewaal.s peedlinq.nl> wrote in message
        news:uj99dkphze 7c.rj6wieg7oeaa $.dlg@40tude.ne t...[color=blue]
        > Op Fri, 31 Mar 2006 15:12:05 -0600 schreef Vijay:
        >[color=green]
        >> With the option strict On set.....
        >>
        >> Dim fs As FileStream = File.OpenRead(s trFile)
        >>
        >> With fs
        >> Dim buffer(fs.Lengt h) As Byte ' <--- Option Strict On disallows implicit
        >> conversions from 'Long' to 'Integer'.
        >> ' other stuff..
        >> End With
        >>
        >> been a while i did VB code... can somebody help me what I am doing wrong
        >> here...
        >>
        >> Vijay[/color]
        >
        >
        > Vijay,
        >
        > The report is about the array bound. fs.length is a long (64 bits signed
        > integer) and the dim statement expects an integer. VB would have to
        > implicitely convert the long to an integer (32 bits signed integer).
        >
        > Unles you have really big files, the type conversion will be safe here.
        > Make it explicit by using ctype(fs.length ,integer) instead of fs.length.
        >
        > Another note in case you don't realise: value fs.length is the upper bound
        > of the array. The array starts at index 0, so you are declaring an array
        > of
        > fs.length + 1 long.
        >
        > Renze.[/color]


        Comment

        • Vijay

          #5
          Re: Option Strict On

          But the point.. I am reading files as a stream from my app display window to
          create zip files the users can export... I am hoping nobody hits the Integer
          mark...

          VJ

          "Renze de Waal" <renze@dewaal.s peedlinq.nl> wrote in message
          news:uj99dkphze 7c.rj6wieg7oeaa $.dlg@40tude.ne t...[color=blue]
          > Op Fri, 31 Mar 2006 15:12:05 -0600 schreef Vijay:
          >[color=green]
          >> With the option strict On set.....
          >>
          >> Dim fs As FileStream = File.OpenRead(s trFile)
          >>
          >> With fs
          >> Dim buffer(fs.Lengt h) As Byte ' <--- Option Strict On disallows implicit
          >> conversions from 'Long' to 'Integer'.
          >> ' other stuff..
          >> End With
          >>
          >> been a while i did VB code... can somebody help me what I am doing wrong
          >> here...
          >>
          >> Vijay[/color]
          >
          >
          > Vijay,
          >
          > The report is about the array bound. fs.length is a long (64 bits signed
          > integer) and the dim statement expects an integer. VB would have to
          > implicitely convert the long to an integer (32 bits signed integer).
          >
          > Unles you have really big files, the type conversion will be safe here.
          > Make it explicit by using ctype(fs.length ,integer) instead of fs.length.
          >
          > Another note in case you don't realise: value fs.length is the upper bound
          > of the array. The array starts at index 0, so you are declaring an array
          > of
          > fs.length + 1 long.
          >
          > Renze.[/color]


          Comment

          • Vijay

            #6
            Re: Option Strict On

            Ok here is some MSDN documentation I found... that helps... gives peace of
            mind...

            You can convert the Integer data type to Long, Single, Double, or Decimal
            without encountering a System.Overflow Exception error.

            I guess if the above is true... VB should not complain... the C# complier
            does not .. why is VB complaining...

            Vijay

            "Renze de Waal" <renze@dewaal.s peedlinq.nl> wrote in message
            news:uj99dkphze 7c.rj6wieg7oeaa $.dlg@40tude.ne t...[color=blue]
            > Op Fri, 31 Mar 2006 15:12:05 -0600 schreef Vijay:
            >[color=green]
            >> With the option strict On set.....
            >>
            >> Dim fs As FileStream = File.OpenRead(s trFile)
            >>
            >> With fs
            >> Dim buffer(fs.Lengt h) As Byte ' <--- Option Strict On disallows implicit
            >> conversions from 'Long' to 'Integer'.
            >> ' other stuff..
            >> End With
            >>
            >> been a while i did VB code... can somebody help me what I am doing wrong
            >> here...
            >>
            >> Vijay[/color]
            >
            >
            > Vijay,
            >
            > The report is about the array bound. fs.length is a long (64 bits signed
            > integer) and the dim statement expects an integer. VB would have to
            > implicitely convert the long to an integer (32 bits signed integer).
            >
            > Unles you have really big files, the type conversion will be safe here.
            > Make it explicit by using ctype(fs.length ,integer) instead of fs.length.
            >
            > Another note in case you don't realise: value fs.length is the upper bound
            > of the array. The array starts at index 0, so you are declaring an array
            > of
            > fs.length + 1 long.
            >
            > Renze.[/color]


            Comment

            • Cor Ligthert [MVP]

              #7
              Re: Option Strict On

              Vijay,

              In addition to Renze.

              VB has very simple convert methods.

              Dim buffer(CInt(fs. Length)) As Byte '

              I hope this helps,

              Cor

              "Vijay" <vijay@msdiscus sions.com> schreef in bericht
              news:OEf2EfQVGH A.2360@TK2MSFTN GP09.phx.gbl...[color=blue]
              > With the option strict On set.....
              >
              > Dim fs As FileStream = File.OpenRead(s trFile)
              >
              > With fs
              > Dim buffer(fs.Lengt h) As Byte ' <--- Option Strict On disallows implicit
              > conversions from 'Long' to 'Integer'.
              > ' other stuff..
              > End With
              >
              > been a while i did VB code... can somebody help me what I am doing wrong
              > here...
              >
              > Vijay
              >[/color]


              Comment

              • Renze de Waal

                #8
                Re: Option Strict On

                p Fri, 31 Mar 2006 16:30:26 -0600 schreef Vijay:
                [color=blue]
                > But the point.. I am reading files as a stream from my app display window to
                > create zip files the users can export... I am hoping nobody hits the Integer
                > mark...
                >
                > VJ
                >[/color]

                Vijay,

                If your files can be that long, try to avoid having to keep the file in
                memory. I am not quite sure what you are doing, but maybe you can process
                the files in parts.

                Renze.

                Comment

                • Vijay

                  #9
                  Re: Option Strict On

                  The confusion for me here is C-Sharp does not seem to complain. VB being
                  simple enough why is there this problem? Maybe they have not changed the
                  underlying type from VB??

                  VJ

                  <zacks@construc tion-imaging.com> wrote in message
                  news:1143841803 .101695.88850@z 34g2000cwc.goog legroups.com...[color=blue]
                  > The length of an array is defined as a 32-bit integer. The Length
                  > property in the FileStream class is a Long. Convert it to an Int, but
                  > you should really check to see if the length of the file yo are trying
                  > to read isn't too large to fit in a 32-bit int.
                  >[/color]


                  Comment

                  • Larry Lard

                    #10
                    Using a constant long as array size initializer gives RTE (was Re: Option Strict On)


                    Vijay wrote:[color=blue]
                    > The confusion for me here is C-Sharp does not seem to complain. VB being
                    > simple enough why is there this problem? Maybe they have not changed the
                    > underlying type from VB??[/color]

                    It would appear this is one the very few times when VB.NET is stricter
                    than C#. To wit, and to recap for C#ers just joining us:

                    C#
                    int[] f = new int[3000000000];
                    // compiles, throws System.Overflow Exception at run time

                    VB.NET
                    Dim f(3000000000) As Integer
                    ' Will not compile: error is 'Constant expression not representable in
                    type Integer'

                    So here, the VB.NET compiler can work out at compile time that 3
                    billion is bigger than an Int32, and thus can't be used as an array
                    size (a CLS limitation I would suspect); but the C# compiler can't, or
                    doesn't care.

                    Your particular example can be examined also:

                    C#
                    long l = 3000000000;
                    int[] f = new int[l];
                    // compiles, throws System.Overflow Exception at run time

                    VB.NET
                    Dim l As Long = 3000000000
                    Dim f(l) As Integer
                    ' Will not compile under Option Strict On:
                    ' error is 'Option Strict On disallows implicit conversions from
                    '<type1>' to '<type2>''
                    ' WILL compile with Option Strict Off: throws System.Overflow Exception
                    at run time

                    So for me the question is, why does C# allow this unstated cast from
                    long to int to compile? To that end I have xposted to the C# group

                    --
                    Larry Lard
                    Replies to group please

                    Comment

                    • Larry Lard

                      #11
                      Using a constant long as array size initializer gives RTE (was Re: Option Strict On)


                      Vijay wrote:[color=blue]
                      > The confusion for me here is C-Sharp does not seem to complain. VB being
                      > simple enough why is there this problem? Maybe they have not changed the
                      > underlying type from VB??[/color]

                      It would appear this is one the very few times when VB.NET is stricter
                      than C#. To wit, and to recap for C#ers just joining us:

                      C#
                      int[] f = new int[3000000000];
                      // compiles, throws System.Overflow Exception at run time

                      VB.NET
                      Dim f(3000000000) As Integer
                      ' Will not compile: error is 'Constant expression not representable in
                      type Integer'

                      So here, the VB.NET compiler can work out at compile time that 3
                      billion is bigger than an Int32, and thus can't be used as an array
                      size (a CLS limitation I would suspect); but the C# compiler can't, or
                      doesn't care.

                      Your particular example can be examined also:

                      C#
                      long l = 3000000000;
                      int[] f = new int[l];
                      // compiles, throws System.Overflow Exception at run time

                      VB.NET
                      Dim l As Long = 3000000000
                      Dim f(l) As Integer
                      ' Will not compile under Option Strict On:
                      ' error is 'Option Strict On disallows implicit conversions from
                      '<type1>' to '<type2>''
                      ' WILL compile with Option Strict Off: throws System.Overflow Exception
                      at run time

                      So for me the question is, why does C# allow this unstated cast from
                      long to int to compile? To that end I have xposted to the C# group

                      --
                      Larry Lard
                      Replies to group please

                      Comment

                      • VJ

                        #12
                        Re: Using a constant long as array size initializer gives RTE (was Re: Option Strict On)

                        Yea seems like at some point VB.NET is smarter or strict here... I took all
                        the trouble to move from VB to C# over last 2 years.. now you have given me
                        some reason to come back.. :-) Actually speaking I should be a loyalist here
                        for VB, because my initials are VB...:-)

                        Vijay

                        "Larry Lard" <larrylard@hotm ail.com> wrote in message
                        news:1144163023 .023565.93240@i 40g2000cwc.goog legroups.com...[color=blue]
                        >
                        > Vijay wrote:[color=green]
                        >> The confusion for me here is C-Sharp does not seem to complain. VB being
                        >> simple enough why is there this problem? Maybe they have not changed the
                        >> underlying type from VB??[/color]
                        >
                        > It would appear this is one the very few times when VB.NET is stricter
                        > than C#. To wit, and to recap for C#ers just joining us:
                        >
                        > C#
                        > int[] f = new int[3000000000];
                        > // compiles, throws System.Overflow Exception at run time
                        >
                        > VB.NET
                        > Dim f(3000000000) As Integer
                        > ' Will not compile: error is 'Constant expression not representable in
                        > type Integer'
                        >
                        > So here, the VB.NET compiler can work out at compile time that 3
                        > billion is bigger than an Int32, and thus can't be used as an array
                        > size (a CLS limitation I would suspect); but the C# compiler can't, or
                        > doesn't care.
                        >
                        > Your particular example can be examined also:
                        >
                        > C#
                        > long l = 3000000000;
                        > int[] f = new int[l];
                        > // compiles, throws System.Overflow Exception at run time
                        >
                        > VB.NET
                        > Dim l As Long = 3000000000
                        > Dim f(l) As Integer
                        > ' Will not compile under Option Strict On:
                        > ' error is 'Option Strict On disallows implicit conversions from
                        > '<type1>' to '<type2>''
                        > ' WILL compile with Option Strict Off: throws System.Overflow Exception
                        > at run time
                        >
                        > So for me the question is, why does C# allow this unstated cast from
                        > long to int to compile? To that end I have xposted to the C# group
                        >
                        > --
                        > Larry Lard
                        > Replies to group please
                        >[/color]


                        Comment

                        • VJ

                          #13
                          Re: Using a constant long as array size initializer gives RTE (was Re: Option Strict On)

                          Sorry wrong post... I was another group..

                          VJ

                          "VJ" <vijaybalki@yah oo.com> wrote in message
                          news:u5g7ao$VGH A.4416@TK2MSFTN GP15.phx.gbl...[color=blue]
                          > Yea seems like at some point VB.NET is smarter or strict here... I took
                          > all the trouble to move from VB to C# over last 2 years.. now you have
                          > given me some reason to come back.. :-) Actually speaking I should be a
                          > loyalist here for VB, because my initials are VB...:-)
                          >
                          > Vijay
                          >
                          > "Larry Lard" <larrylard@hotm ail.com> wrote in message
                          > news:1144163023 .023565.93240@i 40g2000cwc.goog legroups.com...[color=green]
                          >>
                          >> Vijay wrote:[color=darkred]
                          >>> The confusion for me here is C-Sharp does not seem to complain. VB being
                          >>> simple enough why is there this problem? Maybe they have not changed the
                          >>> underlying type from VB??[/color]
                          >>
                          >> It would appear this is one the very few times when VB.NET is stricter
                          >> than C#. To wit, and to recap for C#ers just joining us:
                          >>
                          >> C#
                          >> int[] f = new int[3000000000];
                          >> // compiles, throws System.Overflow Exception at run time
                          >>
                          >> VB.NET
                          >> Dim f(3000000000) As Integer
                          >> ' Will not compile: error is 'Constant expression not representable in
                          >> type Integer'
                          >>
                          >> So here, the VB.NET compiler can work out at compile time that 3
                          >> billion is bigger than an Int32, and thus can't be used as an array
                          >> size (a CLS limitation I would suspect); but the C# compiler can't, or
                          >> doesn't care.
                          >>
                          >> Your particular example can be examined also:
                          >>
                          >> C#
                          >> long l = 3000000000;
                          >> int[] f = new int[l];
                          >> // compiles, throws System.Overflow Exception at run time
                          >>
                          >> VB.NET
                          >> Dim l As Long = 3000000000
                          >> Dim f(l) As Integer
                          >> ' Will not compile under Option Strict On:
                          >> ' error is 'Option Strict On disallows implicit conversions from
                          >> '<type1>' to '<type2>''
                          >> ' WILL compile with Option Strict Off: throws System.Overflow Exception
                          >> at run time
                          >>
                          >> So for me the question is, why does C# allow this unstated cast from
                          >> long to int to compile? To that end I have xposted to the C# group
                          >>
                          >> --
                          >> Larry Lard
                          >> Replies to group please
                          >>[/color]
                          >
                          >[/color]


                          Comment

                          • VJ

                            #14
                            Re: Option Strict On

                            Yea seems like at some point VB.NET is smarter or strict here... I took all
                            the trouble to move from VB to C# over last 2 years.. now you have given me
                            some reason to come back.. :-) Actually speaking I should be a loyalist
                            here
                            for VB, because my initials are VB...:-)

                            "Vijay" <vijay@msdiscus sions.com> wrote in message
                            news:uw3kCB$VGH A.5588@TK2MSFTN GP09.phx.gbl...[color=blue]
                            > The confusion for me here is C-Sharp does not seem to complain. VB being
                            > simple enough why is there this problem? Maybe they have not changed the
                            > underlying type from VB??
                            >
                            > VJ
                            >
                            > <zacks@construc tion-imaging.com> wrote in message
                            > news:1143841803 .101695.88850@z 34g2000cwc.goog legroups.com...[color=green]
                            >> The length of an array is defined as a 32-bit integer. The Length
                            >> property in the FileStream class is a Long. Convert it to an Int, but
                            >> you should really check to see if the length of the file yo are trying
                            >> to read isn't too large to fit in a 32-bit int.
                            >>[/color]
                            >
                            >[/color]


                            Comment

                            • Mike Schilling

                              #15
                              Re: Using a constant long as array size initializer gives RTE (was Re: Option Strict On)


                              "Larry Lard" <larrylard@hotm ail.com> wrote in message
                              news:1144163023 .023565.93240@i 40g2000cwc.goog legroups.com...[color=blue]
                              >
                              > Vijay wrote:[color=green]
                              >> The confusion for me here is C-Sharp does not seem to complain. VB being
                              >> simple enough why is there this problem? Maybe they have not changed the
                              >> underlying type from VB??[/color]
                              >
                              > It would appear this is one the very few times when VB.NET is stricter
                              > than C#. To wit, and to recap for C#ers just joining us:
                              >
                              > C#
                              > int[] f = new int[3000000000];
                              > // compiles, throws System.Overflow Exception at run time
                              >
                              > VB.NET
                              > Dim f(3000000000) As Integer
                              > ' Will not compile: error is 'Constant expression not representable in
                              > type Integer'
                              >
                              > So here, the VB.NET compiler can work out at compile time that 3
                              > billion is bigger than an Int32, and thus can't be used as an array
                              > size (a CLS limitation I would suspect); but the C# compiler can't, or
                              > doesn't care.
                              >
                              > Your particular example can be examined also:
                              >
                              > C#
                              > long l = 3000000000;
                              > int[] f = new int[l];
                              > // compiles, throws System.Overflow Exception at run time
                              >
                              > VB.NET
                              > Dim l As Long = 3000000000
                              > Dim f(l) As Integer
                              > ' Will not compile under Option Strict On:
                              > ' error is 'Option Strict On disallows implicit conversions from
                              > '<type1>' to '<type2>''
                              > ' WILL compile with Option Strict Off: throws System.Overflow Exception
                              > at run time
                              >
                              > So for me the question is, why does C# allow this unstated cast from
                              > long to int to compile? To that end I have xposted to the C# group[/color]


                              There's no cast involved; the C# spec says, regarding the sizes used in
                              creating a new array: (section 7.5.10.2
                              Array creation expressions)

                              Each expression in the expression list must be of type int, uint, long,
                              or ulong, or of a
                              type that can be implicitly converted to one or more of these types

                              It also says it's a compile-time error if a constant expression evaluates to
                              a negative, but is silent about what happens if it evaluates to a number
                              that's too large. In fact, nothing I can see in the spec refers to the
                              largest possible size of an array being limited to what fits in an int.
                              Apparently the language has no such restriction.


                              Comment

                              Working...