Array Question, Out of Bounds

R

RallyDSM

Private Sub IntializeData()
Dim arr() As String
Dim row As Integer
Dim sLine As String
Dim StockData(row) As Stocks
Dim fstream As IO.StreamReader = New IO.StreamReader(New
IO.FileStream("csvstocks.txt", IO.FileMode.Open))
row = 0
Do
sLine = fstream.ReadLine
arr = sLine.Split(","c)
StockData(row).stockName = arr(0)
StockData(row).numShares = arr(1)
StockData(row).datePurchased = arr(2)
StockData(row).purchasePrice = arr(3)
StockData(row).currentPrice = arr(4)
row = row + 1
Loop Until sLine Is Nothing
fstream.Close()
End Sub


There is my code, when it is ran it comes up with an Out of Bounds on
the line StockData(row).stockName = arr(0)

I've been looking at this for awhile now I can't figure out what I need
to change.
 
R

Robinson

"row" is initialised to zero - so you have exactly one "StockData" item.
You need to set "row" to however many records you are expecting, or even
better, use a "List(Of Stocks)" and append each row if you aren't sure how
many there are. You can always convert it to an array later (List.ToArray
()).


Robin
 
R

RallyDSM

Robinson said:
"row" is initialised to zero - so you have exactly one "StockData" item.
You need to set "row" to however many records you are expecting, or even
better, use a "List(Of Stocks)" and append each row if you aren't sure how
many there are. You can always convert it to an array later (List.ToArray
()).


Robin

OK What code changes do I do?

I tried row = 5 (5 rows of data in the CSV) and it comes up with the
same error. I know I'm missing something simple, I just can't figure
it out right now,.
 
R

Robinson

Imports System.Collections.Generic

Dim arr() As String
Dim sLine As String
Dim StockData As New List(Of Stocks)
Dim fstream As IO.StreamReader = New IO.StreamReader(New
IO.FileStream("csvstocks.txt", IO.FileMode.Open))

Do

Dim theStock as new Stocks


sLine = fstream.ReadLine

arr = sLine.Split(","c)

theStock.stockName = arr(0)
theStock.numShares = arr(1)
theStock.datePurchased = arr(2)
theStock.purchasePrice = arr(3)
theStock.currentPrice = arr(4)

StockData.Add ( theStock )


Loop Until sLine Is Nothing

fstream.Close()
 
H

Herfried K. Wagner [MVP]

RallyDSM said:
Dim arr() As String
Dim row As Integer
Dim sLine As String
Dim StockData(row) As Stocks

'row' is 0, thus the array contains one item only.
Dim fstream As IO.StreamReader = New IO.StreamReader(New
IO.FileStream("csvstocks.txt", IO.FileMode.Open))
row = 0
Do
sLine = fstream.ReadLine
arr = sLine.Split(","c)
StockData(row).stockName = arr(0)
StockData(row).numShares = arr(1)
StockData(row).datePurchased = arr(2)
StockData(row).purchasePrice = arr(3)
StockData(row).currentPrice = arr(4)
row = row + 1
Loop Until sLine Is Nothing
fstream.Close()
End Sub

I suggest to use 'System.Collections.Generic.List(Of Stocks)' instead of the
array because it is more dynamic than an array.
 
H

Hal Rosser

RallyDSM said:
Private Sub IntializeData()
Dim arr() As String
Dim row As Integer
Dim sLine As String
Dim StockData(row) As Stocks
Dim fstream As IO.StreamReader = New IO.StreamReader(New
IO.FileStream("csvstocks.txt", IO.FileMode.Open))
row = 0
Do
sLine = fstream.ReadLine
arr = sLine.Split(","c)
StockData(row).stockName = arr(0)
StockData(row).numShares = arr(1)
StockData(row).datePurchased = arr(2)
StockData(row).purchasePrice = arr(3)
StockData(row).currentPrice = arr(4)
row = row + 1
Loop Until sLine Is Nothing
fstream.Close()
End Sub


There is my code, when it is ran it comes up with an Out of Bounds on
the line StockData(row).stockName = arr(0)

I've been looking at this for awhile now I can't figure out what I need
to change.

Do your first read (primer read) before the loop.
and
Then change the loop to check the condition at the top of the loop
ie:
Do Until SLine is Nothing

Then Read the file as the last thing in the loop.
...
In your code, the test for "SLine is nothing" is done after you have already
tried to split SLine, and that's where its crashing.

Read before the loop
Test at the top of the loop
Read at bottom of the loop.
Hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top