Home All Groups Group Topic Archive Search About

Array Question, Out of Bounds

Author
27 Nov 2006 4:17 PM
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.

Author
27 Nov 2006 4:35 PM
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
Author
27 Nov 2006 4:47 PM
RallyDSM
Robinson wrote:
> "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,.
Author
27 Nov 2006 4:56 PM
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()
Author
27 Nov 2006 7:23 PM
Herfried K. Wagner [MVP]
"RallyDSM" <ebennet***@gmail.com> schrieb:
>        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.

Show quoteHide quote
>        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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
27 Nov 2006 8:50 PM
Hal Rosser
Show quote Hide quote
"RallyDSM" <ebennet***@gmail.com> wrote in message
news:1164644221.329168.153920@h54g2000cwb.googlegroups.com...
>    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