Question using string reader to read into an array

badbane

Member
Joined
Apr 23, 2014
Messages
8
Programming Experience
Beginner
I have been trying to remember how to read each line in a txt file into an array on form load. But i haven't messed with vb in months and I can't remember how loop through properly.
VB.NET:
Imports SystemImports System.IO
Imports System.Windows






Public Class Form1
    Dim txtInvtoner As String = "c:\tmp\toner.txt"
    Dim srReadtoner As StreamReader = New StreamReader(txtInvtoner)
    Dim strTempReadToner As String
    
    Dim dell23 As Integer
    Dim Hp4000 As Integer
    Dim hp4100 As Integer
    Dim lex360 As Integer
    Dim lex260 As Integer
    Dim dell18 As Integer


     Dim Toneinv() As String






    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


        For Each line In srReadtoner.ReadToEnd


        Next


    End Sub
End Class
 
updated question

okay I used something different to read the text file. but now i have a new problem. I am using the form load event combine with a loop to read the text file into a public array. Then assigning the values of the array to different textboxes.
the problem is that the loop runs twice but the form loads with no values in it. when i put break points in it reads and assigns data to the array but the loops stops at toninv(2) and the boolean still reads false.

VB.NET:
Imports System
Imports System.IO
Imports System.Windows






Public Class Form1
    Dim txtInvtoner As String = "c:\tmp\toner.txt"
    Dim srReadtoner As StreamReader = New StreamReader(txtInvtoner)
    Dim strTempReadToner As String
    Dim dell23 As Integer
    Dim Hp4000 As Integer
    Dim hp4100 As Integer
    Dim lex360 As Integer
    Dim lex260 As Integer
    Dim dell18 As Integer


 


    Dim Toneinv() As String


    Private Sub txtDell330_TextChanged(sender As Object, e As EventArgs) Handles txtDell330.TextChanged


    End Sub


    Private Sub txtLex360_TextChanged(sender As Object, e As EventArgs) Handles txtLex360.TextChanged


    End Sub


    Private Sub txtLJ4000_TextChanged(sender As Object, e As EventArgs) Handles txtLJ4000.TextChanged


    End Sub


    Private Sub txtLJ4100_TextChanged(sender As Object, e As EventArgs) Handles txtLJ4100.TextChanged


    End Sub


    Private Sub txtLex260_TextChanged(sender As Object, e As EventArgs) Handles txtLex260.TextChanged


    End Sub


    Private Sub txtDell1815_TextChanged(sender As Object, e As EventArgs) Handles txtDell1815.TextChanged


    End Sub




    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim count As Integer = 0
        Dim lcount As Integer = 0
        Do Until srReadtoner.EndOfStream = True

               Redim Preserve toneinv(lcount)
            Toneinv(lcount) = srReadtoner.ReadLine(lcount)
            lcount += 1
        Loop
        count = lcount


        txtDell330.Text = Toneinv(0)
        txtLex360.Text = Toneinv(1)
        txtLJ4000.Text = Toneinv(2)
        txtLJ4100.Text = Toneinv(3)
        txtLex260.Text = Toneinv(4)
        txtDell1815.Text = Toneinv(5)


    End Sub
End Class
 
Hi,

You have got a few flaws going on there that are going to lead you into trouble:-

1) You do not close your file after reading it and you do not release resources back to the system. Use a Using Block which will do both when you reach the end of the block.

2) You use Redim Preserve with an Array which is old technology. With .NET you should be using a List(of T) which, in very basic terms, is a dynamic and easy to use array.

3) You use sReadToner.Readline(lcount) but what you probably do not realise is that specifying an index within the Readline method actually reads a single character at the specified index of that particular line. If the length of the line being read is then smaller than the specified index then an exception will be thrown.

Here is one way that this could be written better:-

Dim myFileLines As New List(Of String)
 
Using SR As New StreamReader("c:\temp\test.txt")
  Do While Not SR.EndOfStream
    myFileLines.Add(SR.ReadLine)
  Loop
End Using
 
MsgBox(myFileLines(0))
MsgBox(myFileLines(1))
'etc ...


Hope that helps.

Cheers,

Ian
 
string writer acting up

that helped me out a lot actually I am reading into the array now I am having issues with writing
 
Last edited:
that helped me out a lot actually I am reading into the array now I am having issues with writing
Given that you could follow JohnH's advice from post #2 and read the whole file into an array in a single line of code, why would you make it more complicated? Are you being forced to by the requirements of an assignment? If so then would it not be a good idea to provide us with those requirements so that we know what's allowed and what's not? If you can do it how you like then why would you not use the single-line solution?
 
Back
Top