Results 1 to 6 of 6

Thread: Index was outside the bounds of the array.

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2016
    Posts
    5

    Index was outside the bounds of the array.

    Dear All,

    I am trying to read selected columns in a text file and getting min and max values of those columns. I can read the first and second column in a sequence, but when I break the sequence i.e. reading first and third or fourth column, I get the error "Index was outside the bounds of the array" .. The selection of the column is in the textbox.
    The text file I am reading has columns separated by TAB and Spaces. So the code is replacing all the spaces and double tabs to single TAB to be consistent. Following is the example of text file

    00001111 00002222 00003333 00004444
    194987.5 195012.5 194987.5 195012.5
    195012.5 2003999 195012.5 2003999
    2003999.5 195037.5 2003999 195037.5
    195037.5 195062.5 195037.5 195062.5
    195062.5 195087.5 195062.5 195087.5
    195087.5 195112.5 195087.5 195112.5
    195112.5 195137.5 195112.5 495137.5
    195137.5 195162.5 195137.5 195162.5
    195162.5 194812.5 195162.5 19400012.5
    194837.5 194837.5
    194862.5 194862.5
    194887.5 194887.5
    194912.5 194912.5
    194937.5 194937.5
    12111 12111999.5

    Please help me in finding the error as I have to finish this as quick as possible.. Thanks in advance

    Code:
     
    Using RECfile As New System.IO.StreamReader("C:\RECEIVER.txt")
                Dim maxE As Double = 0
                Dim minE As Double = 99999999
                Dim maxN As Double = 0
                Dim minN As Double = 99999999
                    Do While RECfile.Peek() <> -1
                    Dim textline As String() = RECfile.ReadLine().Replace(CChar(vbTab) & CChar(vbTab), vbTab).Split(New Char() {CChar(vbTab)})
    
    
                    If textline(Me.eastval.Text) > maxE Then
                        maxE = textline(Me.eastval.Text)
                        Me.MaxvalE.Text = maxE.ToString()
    
                    End If
    
                    If textline(Me.eastval.Text) < minE Then
                        minE = textline(Me.eastval.Text)
                        Me.MinvalE.Text = minE.ToString()
    
                    End If
    
    
                    If textline(Me.northval.Text) > maxN Then
                        maxN = textline(Me.northval.Text)
                        Me.MaxvalN.Text = maxN.ToString()
    
                    End If
                    If textline(Me.northval.Text) < minN Then
                        minN = textline(Me.northval.Text)
                        Me.MinvalN.Text = minN.ToString()
    
                    End If 
    Loop
    End Using
    Attached Files Attached Files
    Last edited by Learn_VB; Mar 19th, 2017 at 06:17 AM. Reason: updating with available data file

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Index was outside the bounds of the array.

    Quote Originally Posted by Learn_VB View Post
    Dear All,

    I am trying to read selected columns in a text file and getting min and max values of those columns. I can read the first and second column in a sequence, but when I break the sequence i.e. reading first and third or fourth column, I get the error "Index was outside the bounds of the array" .. The selection of the column is in the textbox.
    The text file I am reading has columns separated by TAB and Spaces. So the code is replacing all the spaces and double tabs to single TAB to be consistent. Following is the example of text file

    00001111 00002222 00003333 00004444
    194987.5 195012.5 194987.5 195012.5
    195012.5 2003999 195012.5 2003999
    2003999.5 195037.5 2003999 195037.5
    195037.5 195062.5 195037.5 195062.5
    195062.5 195087.5 195062.5 195087.5
    195087.5 195112.5 195087.5 195112.5
    195112.5 195137.5 195112.5 495137.5
    195137.5 195162.5 195137.5 195162.5
    195162.5 194812.5 195162.5 19400012.5
    194837.5 194837.5
    194862.5 194862.5
    194887.5 194887.5
    194912.5 194912.5
    194937.5 194937.5
    12111 12111999.5

    Please help me in finding the error as I have to finish this as quick as possible.. Thanks in advance

    Code:
     
    Using RECfile As New System.IO.StreamReader("C:\RECEIVER.txt")
                Dim maxE As Double = 0
                Dim minE As Double = 99999999
                Dim maxN As Double = 0
                Dim minN As Double = 99999999
                    Do While RECfile.Peek() <> -1
                    Dim textline As String() = RECfile.ReadLine().Replace(CChar(vbTab) & CChar(vbTab), vbTab).Split(New Char() {CChar(vbTab)})
    
    
                    If textline(Me.eastval.Text) > maxE Then
                        maxE = textline(Me.eastval.Text)
                        Me.MaxvalE.Text = maxE.ToString()
    
                    End If
    
                    If textline(Me.eastval.Text) < minE Then
                        minE = textline(Me.eastval.Text)
                        Me.MinvalE.Text = minE.ToString()
    
                    End If
    
    
                    If textline(Me.northval.Text) > maxN Then
                        maxN = textline(Me.northval.Text)
                        Me.MaxvalN.Text = maxN.ToString()
    
                    End If
                    If textline(Me.northval.Text) < minN Then
                        minN = textline(Me.northval.Text)
                        Me.MinvalN.Text = minN.ToString()
    
                    End If 
    Loop
    End Using
    Here are some things I noticed about your code:
    1. Why store the file in the root directory? Also you need admin rights for that.
    2. If you need the highest and lowest possible values for a given data type (Double in your case) use the MinValue and MaxValue properties (Double.MinValue and Double.MaxValue).
    3. The "System" part in "Using RECfile As New System.IO.StreamReader("C:\RECEIVER.txt")" is unnecessary as that namespace is imported by default. You could use an "Imports" statement at the top of your .vb code file to import "System.IO" as well.
    4. Also you are obviously working with "Option Strict" turned off. This is usually unecessary and can lead to nasty bugs in your code. I recommend turning it on and using the Integer.Parse() and Double.Parse() functions to explicitly convert the data types.
    5. Also "Replace(CChar(vbTab) & CChar(vbTab), vbTab).Split(New Char() {CChar(vbTab)})" can simplified to "Replace(vbTab & vbTab, vbTab).Split(CChar(vbTab))".
    6. You didn't supply the interface that comes with your code, making it harder to understand and test.

    Perhaps you could explain what you program is supposed to do?
    Last edited by Peter Swinkels; Mar 19th, 2017 at 06:09 AM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2016
    Posts
    5

    Re: Index was outside the bounds of the array.

    Hi Peter,

    Thanks for your quick response.. Please find attached the example file with the thread update.. I have tried your 2, 3, 4 points .. but still getting the error..

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Index was outside the bounds of the array.

    Quote Originally Posted by Learn_VB View Post
    Hi Peter,

    Thanks for your quick response.. Please find attached the example file with the thread update.. I have tried your 2, 3, 4 points .. but still getting the error..
    Hi, I will have a look at your attachment. I should have mentioned that the points I mentioned in my previous post were meant as general improvements to your code, not to fix your specific problem.

    EDIT:
    I just viewed your attachment. You should post your project files.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2016
    Posts
    5

    Re: Index was outside the bounds of the array.

    Sorry, It is not possible to post project files..... it is just some s***y IT security thing..

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Index was outside the bounds of the array.

    You aren't allowed to attach compiled code, for obvious reasons. Source code files are just text, though, and those are fine on this end.

    The problem appears fairly obvious based on the example file you showed. Only the first two columns have values for all rows, so any attempt to check the third or fourth column along with the first or second is certain to fail with the exact error you are showing. Split() will return an array with the number of elements found. In the first several rows, that means that split will return an array with four elements. Accessing any of those four elements is fine. However, for the last several rows, Split will only result in an array with two elements, because there is nothing in the third or fourth element. Any attempt to access the third or fourth element on those lines will give you an Index Out Of Range exception because the array only has two elements.

    What the best solution is to this problem is harder to say. You can always check the length of the textLine array, and not do anything with elements beyond the end of the array. Or maybe the real problem is the missing data, in which case you can fix up the lines before doing the rest.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width