473,395 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Array

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
Nov 21 '05 #1
1 1700
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


Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.