944,155 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 14830
  • VB.NET RSS
May 29th, 2006
0

using a "for" loop to read a text file

Expand Post »
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()
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
randycena is offline Offline
4 posts
since May 2006
May 29th, 2006
0

Re: using a "for" loop to read a text file

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zmind is offline Offline
12 posts
since Mar 2006
May 29th, 2006
0

Re: using a "for" loop to read a text file

thank you but now i have this error:
'EndOfStream' is not a member of 'System.IO.StreamReader'.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
randycena is offline Offline
4 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: VB.net code problem
Next Thread in VB.NET Forum Timeline: using VB2003 to accept excel file and convert it into a graph





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC