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
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
Last edited by Learn_VB; Mar 19th, 2017 at 06:17 AM.
Reason: updating with available data file
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
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.
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..
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.
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.