473,386 Members | 1,779 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,386 software developers and data experts.

Array Question, Out of Bounds

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.

Nov 27 '06 #1
5 1488
"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
Nov 27 '06 #2

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,.

Nov 27 '06 #3
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()
Nov 27 '06 #4
"RallyDSM" <eb********@gmail.comschrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 27 '06 #5

"RallyDSM" <eb********@gmail.comwrote in message
news:11**********************@h54g2000cwb.googlegr oups.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

Nov 27 '06 #6

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

Similar topics

26
by: Sterten | last post by:
when I define int R; and then later access it with x=R;C=7; .... but x happens to be <0 or >99 , then the program's behavious becomes unpredictable. Is there a way to prevent this ? Is...
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
6
by: Flip | last post by:
I'm reading the O'Reilly's Progamming C# book and I have a question about array bounds checking. On page 174, near the top, they show an example where c# does indeed to array bounds checking cause...
1
by: noleander | last post by:
Hey. I'm new to Visual C++. I've got a large application, and I need to make sure all array accesses are within bounds. Back on Unix we had a tool called Purify that detected array-out-of-bounds...
8
by: ais523 | last post by:
I've checked the FAQ for this and couldn't find the answer. Is the following code snippet portable? int a; a=6; printf("%d\n",(*a)); This prints "6" on my compiler. I've been told it's...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
26
by: Adam Warner | last post by:
Hi all, One cannot return a pointer to an array type in C because C has no first class array types. But one can return a pointer to a struct containing an incomplete array via the illegal but...
14
by: ptq2238 | last post by:
Hi, I'm getting confused with arrays and hope someone can shed light on my code. I wrote this to try and learn about files and arrays as I thought if I could grab each element and place them into...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
30
by: ggnguser | last post by:
It's part of a test and I'm stumped. There is a function void foo(char **x) The function signature is given and cannot be changed, so no passing of other values. The test case involves...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.