|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Array Question, Out of BoundsDim 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. "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 Robinson wrote:
> "row" is initialised to zero - so you have exactly one "StockData" item. OK What code changes do I do?> 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 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,. 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() "RallyDSM" <ebennet***@gmail.com> schrieb: 'row' is 0, thus the array contains one item only.> Dim arr() As String > Dim row As Integer > Dim sLine As String > Dim StockData(row) As Stocks Show quoteHide quote > Dim fstream As IO.StreamReader = New IO.StreamReader(New I suggest to use 'System.Collections.Generic.List(Of Stocks)' instead of the > 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 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/>
Show quote
Hide quote
"RallyDSM" <ebennet***@gmail.com> wrote in message Do your first read (primer read) before the loop.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. 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 |
|||||||||||||||||||||||