Question Issue with FileReader.ReadLine

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
167
Programming Experience
Beginner
I'm currently using VB Express 2010 and I've been having problems with reading one specific line out of about 5 lines of text..Here is a piece of the code I'm using:

VB.NET:
Dim reader As New StreamReader(datastream)
Dim line As String = ""

                    While Not reader.EndOfStream
                        line = FileReader.ReadLine()
                        If (line.Contains("status") = True) Then
                            Name = (line)
                            Exit While
                        End If
                    End While

                    txtLog.AppendText(singledomain & "  " & Name & "  " & Now & vbCrLf)

I am wanting look at the text result and find the line that has "status" in it..When I find that line I want to take it and display it in the txtLog.

Any ideas where I'm going wrong here?
 
Hi,

You don't actually say what your problem is so cannot help you with your specific problem but two things for you to begin with:-

1) You are calling FileReader.ReadLine but FileReader does not exist. This should be Reader.ReadLine in the context of the code presented.

2) You set Name = (Line) - do you realise that in the context you have this you are actually changing the name of the parent container. i.e if this is coded within a Form then you are changing the name of the Form.

Hope that helps.

Cheers,

Ian
 
Hey Ian thanks for your response. What I want to do is get the response (datastream) which has 5 lines of text in it
text line 1
text line 2
text line 3
text line 4
text line 5

I want to find the line that contains "status" in it and display only that line in the textbox (txtLog)

Thanks for the reader.ReadLine tip..I made that change.

I didn't realize that on the form part..been up a little too long..but what I was trying to do was set a string which I named "Name" and put the line that contains "Status" in there..then call it in the textbox so it's displayed.

I changed the "Name" to "result" and now get an error in the txtLog (Variable 'result' is used before it has been assigned a value)

This is my code..

VB.NET:
                    Dim reader As New StreamReader(datastream)
                    Dim line As String = ""
                    Dim result As String

                    While Not reader.EndOfStream
                        line = reader.ReadLine()
                        If (line.Contains("status") = True) Then
                            result = (line)
                            Exit While
                        End If
                    End While

                    txtLog.AppendText(singledomain & "  " & result & "  " & Now & vbCrLf)
 
Hi,

Ok, another two things for you here:-

1) Please remember when posting to be specific about what you are experiencing. You say that you receive an error with "Variable used before Being assigned a value". In this case this is a WARNING and not an Error and is caused due to the fact that you declare a variable called Result which then may or may not be assigned a value within a While Loop. When the complier see's this it displays this warning when you try to use / display a variable that may not contain an initial value. The way to get round this is to declare the variable as Dim Result as String = String.Empty.

2) I have ran your code with a text file of my own looking for a string called "WhateverItDoesNotMatter" with your Line.Contains Method and it works fine so one thing to remember is that the Contains method will take Case Sensitivity into account so you may want to try line.ToLower.Contains("status") = True

Hope that helps again.

Cheers,

Ian
 
Ian, thanks for your response and clearing that up for me. Sorry for the confusion between the Warning and Error.

Not setting the string to empty is exactly where the issue was! Once I did that it worked like a charm although I also add the ToLower in there which seams to work fine as well.

Thanks again for your help on this one! I'd rep you some more but I need to rep some more others first :)
 
Another approach...

VB.NET:
Dim lines() As String = IO.File.ReadAllLines("c:\test.txt")
Me.TextBox1.AppendText(lines(Array.FindIndex(lines, Function(s) s.Contains("status"))))
 
Back
Top