I assume soda() is declared at the class level? I also assume upperbound
is too?
You can move upperbound back into form_Load because you can loop through
an array using the length of the array:
for x=0 to soda.Length-1
However, I am concerned about the double loop you have to read in the
items. In fact, I can't even follow the logic of the code.
I think you have a comma-delimited file for your drinks and shares,
similar to:
ABC,10
XYZ,30
MORE,50
Seems like you could trim off a lot of code with an arraylist:
' Global
Dim alSodas As New ArrayList
' Form Load
Dim sr As IO.StreamReader
Dim sCSVLine As String
Dim aCSVValues() As String
Dim oSoda As soda
sr = IO.File.OpenText("Soft Drinks.txt")
sCSVLine = sr.ReadLine
Do While Not sCSVLine Is Nothing
aCSVValues = sCSVLine.Split(CChar(","))
oSoda = New soda
With oSoda
.drinks = aCSVValues(0)
.shares = aCSVValues(1)
.sales = CDbl(.shares / 100 * 42)
End With
alSodas.Add(oSoda)
Loop
sr.Close()
' Bind to List
Dim oSoda As soda
Dim iCounter As Integer
Dim fmtstr As String = "{0,-15}{1,5:n1}{2,10:n1}"
lstdisplay.Items.Clear()
For iCounter = 0 To alSodas.Count - 1
oSoda = CType(alSodas(iCounter), soda)
lstdisplay.Items.Add(String.Format(fmtstr, osoda.drinks,
osoda.shares, osoda.sales))
Next
That reduces your file reads from three to one. Hopefully I'm on target
with that one.
=?Utf-8?B?ZnJlZGR5?= <fr****@discussions.microsoft.com> wrote in
news:9D**********************************@microsof t.com:
in my code I am using an array, but it only reads one like it doe not
loop Dim i As Integer
Dim sr As IO.StreamReader
Dim strline, strlines() As String
Dim fmtstr As String = "{0,-15}{1,5:n1}{2,10:n1}"
sr = IO.File.OpenText("Soft Drinks.txt")
strline = sr.ReadLine
lstdisplay.Items.Clear()
For i = 0 To upperbound
strlines = strline.Split(CChar(","))
soda(i).drinks = sr.ReadLine
soda(i).shares = CDbl(sr.ReadLine)
soda(i).sales = CDbl(soda(i).shares / 100 * 42)
lstdisplay.Items.Add(String.Format(fmtstr, soda(i).drinks,
soda(i).shares, soda(i).sales))
Next
sr.Close()
End sub
Form_load
Dim name As String
Dim i As Integer
Dim sr As StreamReader = IO.File.OpenText("Soft Drinks.txt")
upperbound = 0
Do While (sr.Peek <> -1)
name = sr.ReadLine
upperbound += 1
Loop
sr.Close()
ReDim soda(upperbound)
sr = IO.File.OpenText("Soft Drinks.txt")
For i = 1 To upperbound
soda(i).drinks = sr.ReadLine
Next
sr.Close()
End Sub
Help please