read code problem

hitechoutlaw

Well-known member
Joined
Mar 22, 2003
Messages
77
Location
USA
ive seen this being used before but for some reason i cant get it to work. could some1 tell me what is wrong with my code:

Code:
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            Dim Read As New System.IO.StreamReader(OpenFileDialog1.FileName)
            Dim Line() As Integer
            Dim i As Integer = 0
            For Each i In Line
                Line(i) = Read.ReadLine
            Next  Error occurs here
            TextBox1.Text = Line(0)
            TextBox2.Text = Line(1)
            TextBox3.Text = Line(2)
        End If
thanks
 
You did not specify how big the array is.
I dont know but to me this code doesnt make any sense... Where did you find this?
If you want only 3 integers do this:

Code:
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
Dim Read As New System.IO.StreamReader(OpenFileDialog1.FileName)
Dim Line(2) As Integer You need to say how big the array is 
if you are not going to redim it
Dim i As Integer = 0
For Each i In Line
   Line(i) = Read.ReadLine
Next
TextBox1.Text = Line(0)
TextBox2.Text = Line(1)
TextBox3.Text = Line(2)
End If
 
ive seen bits and peices all around and tried to but them together. i think i know where u are talking about: Line(i) = Read.ReadLine right?
now that is the place i dont know, i was trying to find an easy way to read a file cuz all the other examples ive seen are very long and compicated. i think this is my only error now.
how can i make this read each line and put each line in the text boxes? can it be done with my current code or will i have to chage all of it?
 
This will read 3 lines just like you wanted and put them in textboxes:

Code:
        Dim line(2) As String
        Dim x As Integer
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
            For x = 0 To 2
                line(x) = LineInput(1)
            Next x
            TextBox1.Text = line(0)
            TextBox2.Text = line(1)
            TextBox3.Text = line(2)
        End If
 
You should declare Line and i as Strings, as that is what is stored
and retrieved from text files. Other than that, it looks like it should
work.
 
:eek:

mutant! No! Never ever ever use FileOpen and related functions.

They are provided for backward compatability (so that the program
migration wizard doesnt have to convert the old VB6 way to the new
.NET way), and should be avoided like the plague (at least, in my
opinion). Awful coding practice, those functions.

The System.IO namespace is what you use.
 
Well, its there so it can be used, there is a choice, he can use that or a streamreader, his choice.
 
True, but if you want to practice good coding standards, then you
use System.IO. You can use Goto and On Error and FileOpen
and all the rest of the Vb6 compatability functions, but you shouldnt.
They are not really supported by the .NET framework.

As a general rule, I dont use anything that cant be used in both
C# and VB.NET.
 
well in my way it only reads the last line and it puts it in the first text box and in mutants way it works great, i like my way cuz its easier for me to rember for later use and i will use it if i can find out why it is doing that, but if i cant ill use mutants way.

thanks guys
 
This is the same thing but with streamreader:
Hope it helps.
Code:
            Dim Read As New System.IO.StreamReader(OpenFileDialog1.FileName)
            For x = 0 To 2
                line(x) = Read.ReadLine
            Next x
            TextBox1.Text = line(0)
            TextBox2.Text = line(1)
            TextBox3.Text = line(2)
 
I used that code for reading a 15 lines text file, but I want to make a split in each line to have a 2D array.

Each line contains 4-6 elements separated by "vbtab"

its possible ?

thanks
 
Back
Top