|
-
Feb 23rd, 2008, 06:18 PM
#1
Thread Starter
New Member
[RESOLVED] Still stuck !
Posted the other day.......
There is prbably a better way to read form a text file using stream reader and compare to the text in a text box.... this is what I have..... It produces an out of bounds error but I still cant see why !
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
intOccur = 0
Dim intSeekStart As Integer = 0
Dim intStrLen As Integer = TextBox1.Text.Length
If TextBox1.Text <> "" Then
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim fsStream As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim srReader As New StreamReader(fsStream)
Do
Dim chrCheckString(TextBox1.Text.Length - 1) As Char
srReader.Read(chrCheckString, intSeekStart, intStrLen)
Dim strCompare As New String(chrCheckString)
If strCompare.ToUpper = TextBox1.Text.ToUpper Then
intOccur += 1
Label1.Text = "Occurences = " & CStr(intOccur)
chrCheckString = Nothing
End If
intSeekStart += 1
Loop While (intSeekStart <= (srReader.BaseStream.Length - intSeekStart))
srReader.Close()
End If
End If
End Sub
-
Feb 23rd, 2008, 06:23 PM
#2
Re: Still stuck !
Where does the OutOfBounds exception occur?
-
Feb 23rd, 2008, 06:26 PM
#3
Re: Still stuck !
its something like:
vb Code:
Do while srReader.peek <> -1
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 23rd, 2008, 06:29 PM
#4
Thread Starter
New Member
Re: Still stuck !
But I need to read x No. of characters into the array and compare them to a known string !
-
Feb 23rd, 2008, 06:31 PM
#5
Re: Still stuck !
if you want to post an example of the file you're parsing, i'll try it for you
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 23rd, 2008, 06:33 PM
#6
Re: Still stuck !
Shouldnt the loop condition be this?
vb Code:
Loop While (intSeekStart <= (srReader.BaseStream.Length - intStrLen))
-
Feb 23rd, 2008, 06:35 PM
#7
Thread Starter
New Member
Re: Still stuck !
What i dont understand is that chrCheckstring contains the correct value and the indexes are all 4 which should be ok... but the loop throws the out of bounds exception after only one iteration ??
-
Feb 23rd, 2008, 06:37 PM
#8
Re: Still stuck !
 Originally Posted by Paul the Rookie
What i dont understand is that chrCheckstring contains the correct value and the indexes are all 4 which should be ok... but the loop throws the out of bounds exception after only one iteration ??
What line?
-
Feb 23rd, 2008, 06:39 PM
#9
Thread Starter
New Member
Re: Still stuck !
srReader.Read(chrCheckString, intSeekStart, intStrLen)
this line causes the error but the 2 values are both 4
-
Feb 23rd, 2008, 06:45 PM
#10
Re: Still stuck !
The second argument of the Read method specifies on what index in the char array that you will start writing too. You should keep it to 0 at all times.
On the second iteration, what it will try to do is to write X number of chars to an array that can hold X number of chars, but start writing at index 1. It will obviously go out of bounds.
For each call to the Read method, the StreamReader advances in the stream, so your intSeekStart variable really isnt needed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|