hello! I need to use this for loop to extract specific data from a text file. But it gives me an error message:syntax error.I hope you don't mind helping. Thank you for your time and effort.

Below is my codes:

OpenFileDialog1.ShowDialog()
Dim a As String
TextBox2.Text = OpenFileDialog1.FileName
a = TextBox2.Text
Dim objStreamReader As New StreamReader(TextBox2.Text)

for each i as StreamReader
objStreamReader.ReadLine()
TextBox4.Text = (objStreamReader.ReadLine())
Label2.Text = TextBox4.Text.Substring(0, 3)

Next

objStreamReader.Close()

Dani AI

Generated

— the syntax and the runtime error come from two different issues in the original posts. The For Each i As StreamReader line is incorrect (a StreamReader is not an enumerable), and the "'EndOfStream' is not a member..." message means the compiler/resolver can't find that property on the actual type in use. The BCL's System.IO.StreamReader does expose an EndOfStream property, while the string/text reader types do not, so confirm the declared type and the project references. (learn.microsoft.com)

A simple, safe pattern that avoids recreating the reader and that correctly detects EOF is to use a Using block and either Peek() or ReadLine()-null checks. Example (replace path/controls as appropriate):

Using sr As New System.IO.StreamReader(path)
    Dim line As String
    While sr.Peek() <> -1
        line = sr.ReadLine()
        If line Is Nothing Then Exit While
        If line.Length >= 3 Then
            Label2.Text = line.Substring(0, 3)
        Else
            Label2.Text = line
        End If
    End While
End Using

ReadLine() returns Nothing at end-of-stream and Peek() returns -1 when no chars remain, so either check works and both avoid the double-read bug seen in the original code (calling ReadLine() twice consumes two lines). Also guard any Substring calls for short lines. (referencesource.microsoft.com)

If EndOfStream still isn't recognized, check the exact declared type (hover or "Go To Definition" in the IDE), remove any conflicting imports or custom types named StreamReader, and ensure the intended .NET framework/assemblies are targeted. Prefer the Using pattern so readers are closed automatically and wrap IO in try/catch for robustness. (learn.microsoft.com)

hi randycena,

I am not sure of the purpose of your code as your description is brief and I assumed that you wish to read a text file and extract certain string from the file. If we just take the for loop portion of the code, there maybe some problem. You're defining the streamreader object for every loop you go through which is not so correct. The stremreader object can read a whole stream at one go. You should use the objStreamReader you defined earlier as this instance has been 'tied' to the text file defined in textbox2.text. So instead of using For..Next loop, you should you While..End While and use the EndOfStream property to check if it's end of stream.

While NOT objStreamReader.EndOfStream
'do whatever you want to do
End While

thank you but now i have this error:
'EndOfStream' is not a member of 'System.IO.StreamReader'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.