Results 1 to 10 of 10

Thread: [RESOLVED] Still stuck !

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    15

    Resolved [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

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Still stuck !

    Where does the OutOfBounds exception occur?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Still stuck !

    its something like:

    vb Code:
    1. Do while srReader.peek <> -1

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    15

    Re: Still stuck !

    But I need to read x No. of characters into the array and compare them to a known string !

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Still stuck !

    if you want to post an example of the file you're parsing, i'll try it for you

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Still stuck !

    Shouldnt the loop condition be this?
    vb Code:
    1. Loop While (intSeekStart <= (srReader.BaseStream.Length - intStrLen))
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    15

    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 ??

  8. #8
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Still stuck !

    Quote 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?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    15

    Re: Still stuck !

    srReader.Read(chrCheckString, intSeekStart, intStrLen)
    this line causes the error but the 2 values are both 4

  10. #10
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

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