overriding array size

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

    #1

    overriding array size

    I have a program that reads a delimited text file and splits the input
    string into string array elements:

    Dim Data(41) as String

    'read semicolon delimited fields into string array
    Data = SrRead.ReadLine ().Split(";".To CharArray)

    I expect up to 40 elements to be read. But if the input data string has
    less than that and I try to access Data(40) I get a
    System.IndexOut OfRange exception. I thought that declaring the array
    size first would take care of that.

    I know I can GetLength on the array and know how big it is. But is there
    a way to statically set the array bounds so I don't need to know?

    Thanks
  • vbnetdev

    #2
    Re: overriding array size

    Don't defind the number ahead of time. Let it find it for you when it reads
    the first line.

    Dim fieldValues As String()

    'Open file and read first line to determine how many fields
    'there are.
    myReader = IO.File.OpenTex t(fileFullPath)
    fieldValues = myReader.ReadLi ne().Split(sepe rator)


    "Dave Cullen" <nospam@mail.co mwrote in message
    news:458C3963.4 77C6482@mail.co m...
    >I have a program that reads a delimited text file and splits the input
    string into string array elements:
    >
    Dim Data(41) as String
    >
    'read semicolon delimited fields into string array
    Data = SrRead.ReadLine ().Split(";".To CharArray)
    >
    I expect up to 40 elements to be read. But if the input data string has
    less than that and I try to access Data(40) I get a
    System.IndexOut OfRange exception. I thought that declaring the array
    size first would take care of that.
    >
    I know I can GetLength on the array and know how big it is. But is there
    a way to statically set the array bounds so I don't need to know?
    >
    Thanks

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: overriding array size

      "Dave Cullen" <nospam@mail.co mschrieb:
      >I have a program that reads a delimited text file and splits the input
      string into string array elements:
      >
      Dim Data(41) as String
      .... is semantically equivalent to 'Dim Data() As String = New String(41)
      {}'.
      'read semicolon delimited fields into string array
      Data = SrRead.ReadLine ().Split(";".To CharArray)
      'Split' returns a dimensioned and filled array object. Thus you do not need
      to dimension the array at its declaration:

      \\\
      Dim Data() As String = SrRead.ReadLine .Split(...)
      ///

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • Master Programmer

        #4
        Re: overriding array size

        Dim MyArr As Array
        Dim MyString Ss String
        Dim i As Integer

        ' Build string
        MyString = "What;Are;You;D oing"

        ' Put it into an array - spliting each element with delimiter
        MyArr = Split(MyString, ";")

        ' Loop through array
        For i = LBound(MyArr) to UBound(MyArr)
        Dim MyRow as String = MyArr(i)
        Next

        The Grand Master


        Dave Cullen wrote:
        I have a program that reads a delimited text file and splits the input
        string into string array elements:
        >
        Dim Data(41) as String
        >
        'read semicolon delimited fields into string array
        Data = SrRead.ReadLine ().Split(";".To CharArray)
        >
        I expect up to 40 elements to be read. But if the input data string has
        less than that and I try to access Data(40) I get a
        System.IndexOut OfRange exception. I thought that declaring the array
        size first would take care of that.
        >
        I know I can GetLength on the array and know how big it is. But is there
        a way to statically set the array bounds so I don't need to know?
        >
        Thanks

        Comment

        • Phill W.

          #5
          Re: overriding array size

          Dave Cullen wrote:
          I have a program that reads a delimited text file and splits the input
          string into string array elements:
          >
          Dim Data(41) as String
          >
          'read semicolon delimited fields into string array
          Data = SrRead.ReadLine ().Split(";".To CharArray)
          >
          I expect up to 40 elements to be read. But if the input data string has
          less than that and I try to access Data(40) I get a
          System.IndexOut OfRange exception. I thought that declaring the array
          size first would take care of that.
          >
          I know I can GetLength on the array and know how big it is. But is there
          a way to statically set the array bounds so I don't need to know?
          It's Kludgey in the extreme, but you could force the array size after
          you've loaded it, as in :

          Dim Data as String() _
          = SrRead.ReadLine ().Split(";"c)

          ReDim Preserve Data(40)

          I've yet to find a ".Net" equivalent of ReDim Preserve ...

          HTH,
          Phill W.

          Comment

          • Dave Cullen

            #6
            Re: overriding array size

            Thank you Phill, that works. The other solutions posted would require me
            to test the size of the array before attempting to read each element
            (tedious), or abort if the length was not what I expect.

            With the ReDim Preserve I get to have a valid array of the size expected
            (no crash & burn when I read element 40) and also have backward
            compatibility with data files that have less than the expected number of
            elements.

            Thanks again
            Dave


            "Phill W." wrote:
            >
            Dave Cullen wrote:
            I have a program that reads a delimited text file and splits the input
            string into string array elements:

            Dim Data(41) as String

            'read semicolon delimited fields into string array
            Data = SrRead.ReadLine ().Split(";".To CharArray)

            I expect up to 40 elements to be read. But if the input data string has
            less than that and I try to access Data(40) I get a
            System.IndexOut OfRange exception. I thought that declaring the array
            size first would take care of that.

            I know I can GetLength on the array and know how big it is. But is there
            a way to statically set the array bounds so I don't need to know?
            >
            It's Kludgey in the extreme, but you could force the array size after
            you've loaded it, as in :
            >
            Dim Data as String() _
            = SrRead.ReadLine ().Split(";"c)
            >
            ReDim Preserve Data(40)
            >
            I've yet to find a ".Net" equivalent of ReDim Preserve ...
            >
            HTH,
            Phill W.

            Comment

            Working...