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

Can't Figure Out why this array is out of bounds now!

Pre STory - I've had a lot of problems with this program, and I just
added the last part of it (the add item code) and now an older part of
the program crashes.

Public Structure Stocks
Public stockName As String
Public numShares As Double
Public datePurchased As String
Public purchasePrice As Double
Public currentPrice As Double
End Structure

Public Class frmMain
Inherits System.Windows.Forms.Form
Dim StockData(50) As Stocks
Dim ProfitRow As Integer
#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents lblStock As System.Windows.Forms.Label
Friend WithEvents txtStock As System.Windows.Forms.TextBox
Friend WithEvents lblCurrentPrice As System.Windows.Forms.Label
Friend WithEvents txtCurrentPrice As System.Windows.Forms.TextBox
Friend WithEvents lblDatePurchased As System.Windows.Forms.Label
Friend WithEvents lblNumberOfShares As System.Windows.Forms.Label
Friend WithEvents txtDatePurchased As System.Windows.Forms.TextBox
Friend WithEvents txtNumberOfShares As System.Windows.Forms.TextBox
Friend WithEvents btnAddStock As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents btnDisplay As System.Windows.Forms.Button
Friend WithEvents btnShowProfitLoss As System.Windows.Forms.Button
Friend WithEvents btnQuit As System.Windows.Forms.Button
Friend WithEvents lblPurchasePrice As System.Windows.Forms.Label
Friend WithEvents txtPurchasePrice As System.Windows.Forms.TextBox
Friend WithEvents lstProfitLoss As System.Windows.Forms.ListBox
Friend WithEvents lblProfitLoss As System.Windows.Forms.Label
Friend WithEvents lstOutput As System.Windows.Forms.ListBox
<System.Diagnostics.DebuggerStepThrough()Private Sub
InitializeComponent()
Me.lblStock = New System.Windows.Forms.Label
Me.txtStock = New System.Windows.Forms.TextBox
Me.lblCurrentPrice = New System.Windows.Forms.Label
Me.txtCurrentPrice = New System.Windows.Forms.TextBox
Me.lblDatePurchased = New System.Windows.Forms.Label
Me.lblNumberOfShares = New System.Windows.Forms.Label
Me.txtDatePurchased = New System.Windows.Forms.TextBox
Me.txtNumberOfShares = New System.Windows.Forms.TextBox
Me.btnAddStock = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.btnDisplay = New System.Windows.Forms.Button
Me.btnShowProfitLoss = New System.Windows.Forms.Button
Me.btnQuit = New System.Windows.Forms.Button
Me.lblPurchasePrice = New System.Windows.Forms.Label
Me.txtPurchasePrice = New System.Windows.Forms.TextBox
Me.lstOutput = New System.Windows.Forms.ListBox
Me.lstProfitLoss = New System.Windows.Forms.ListBox
Me.lblProfitLoss = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'lblStock
'
Me.lblStock.Location = New System.Drawing.Point(16, 8)
Me.lblStock.Name = "lblStock"
Me.lblStock.Size = New System.Drawing.Size(40, 23)
Me.lblStock.TabIndex = 0
Me.lblStock.Text = "Stock"
'
'txtStock
'
Me.txtStock.Location = New System.Drawing.Point(64, 8)
Me.txtStock.Name = "txtStock"
Me.txtStock.Size = New System.Drawing.Size(100, 20)
Me.txtStock.TabIndex = 1
'
'lblCurrentPrice
'
Me.lblCurrentPrice.Location = New System.Drawing.Point(184, 8)
Me.lblCurrentPrice.Name = "lblCurrentPrice"
Me.lblCurrentPrice.Size = New System.Drawing.Size(72, 23)
Me.lblCurrentPrice.TabIndex = 2
Me.lblCurrentPrice.Text = "Current Price"
'
'txtCurrentPrice
'
Me.txtCurrentPrice.Location = New System.Drawing.Point(264, 8)
Me.txtCurrentPrice.Name = "txtCurrentPrice"
Me.txtCurrentPrice.Size = New System.Drawing.Size(72, 20)
Me.txtCurrentPrice.TabIndex = 3
'
'lblDatePurchased
'
Me.lblDatePurchased.Location = New System.Drawing.Point(400, 8)
Me.lblDatePurchased.Name = "lblDatePurchased"
Me.lblDatePurchased.Size = New System.Drawing.Size(100, 32)
Me.lblDatePurchased.TabIndex = 4
Me.lblDatePurchased.Text = "Date Purchased (mm/dd/yy)"
'
'lblNumberOfShares
'
Me.lblNumberOfShares.Location = New System.Drawing.Point(544,
8)
Me.lblNumberOfShares.Name = "lblNumberOfShares"
Me.lblNumberOfShares.Size = New System.Drawing.Size(88, 32)
Me.lblNumberOfShares.TabIndex = 5
Me.lblNumberOfShares.Text = "Number of Shares"
'
'txtDatePurchased
'
Me.txtDatePurchased.Location = New System.Drawing.Point(400,
48)
Me.txtDatePurchased.Name = "txtDatePurchased"
Me.txtDatePurchased.Size = New System.Drawing.Size(100, 20)
Me.txtDatePurchased.TabIndex = 6
'
'txtNumberOfShares
'
Me.txtNumberOfShares.Location = New System.Drawing.Point(544,
48)
Me.txtNumberOfShares.Name = "txtNumberOfShares"
Me.txtNumberOfShares.Size = New System.Drawing.Size(104, 20)
Me.txtNumberOfShares.TabIndex = 7
'
'btnAddStock
'
Me.btnAddStock.Location = New System.Drawing.Point(72, 48)
Me.btnAddStock.Name = "btnAddStock"
Me.btnAddStock.Size = New System.Drawing.Size(75, 23)
Me.btnAddStock.TabIndex = 8
Me.btnAddStock.Text = "Add Stock"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(160, 48)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(104, 23)
Me.Button2.TabIndex = 9
Me.Button2.Text = "Update Stock"
'
'btnDisplay
'
Me.btnDisplay.Location = New System.Drawing.Point(72, 80)
Me.btnDisplay.Name = "btnDisplay"
Me.btnDisplay.Size = New System.Drawing.Size(75, 23)
Me.btnDisplay.TabIndex = 10
Me.btnDisplay.Text = "Display"
'
'btnShowProfitLoss
'
Me.btnShowProfitLoss.Location = New System.Drawing.Point(160,
80)
Me.btnShowProfitLoss.Name = "btnShowProfitLoss"
Me.btnShowProfitLoss.Size = New System.Drawing.Size(104, 23)
Me.btnShowProfitLoss.TabIndex = 11
Me.btnShowProfitLoss.Text = "Show Profit/Loss"
'
'btnQuit
'
Me.btnQuit.Location = New System.Drawing.Point(280, 64)
Me.btnQuit.Name = "btnQuit"
Me.btnQuit.Size = New System.Drawing.Size(75, 23)
Me.btnQuit.TabIndex = 12
Me.btnQuit.Text = "Quit"
'
'lblPurchasePrice
'
Me.lblPurchasePrice.Location = New System.Drawing.Point(400,
88)
Me.lblPurchasePrice.Name = "lblPurchasePrice"
Me.lblPurchasePrice.Size = New System.Drawing.Size(100, 23)
Me.lblPurchasePrice.TabIndex = 13
Me.lblPurchasePrice.Text = "Purchase Price"
'
'txtPurchasePrice
'
Me.txtPurchasePrice.Location = New System.Drawing.Point(512,
88)
Me.txtPurchasePrice.Name = "txtPurchasePrice"
Me.txtPurchasePrice.Size = New System.Drawing.Size(136, 20)
Me.txtPurchasePrice.TabIndex = 14
'
'lstOutput
'
Me.lstOutput.Location = New System.Drawing.Point(24, 112)
Me.lstOutput.Name = "lstOutput"
Me.lstOutput.Size = New System.Drawing.Size(624, 134)
Me.lstOutput.TabIndex = 15
'
'lstProfitLoss
'
Me.lstProfitLoss.FormattingEnabled = True
Me.lstProfitLoss.Location = New System.Drawing.Point(24, 275)
Me.lstProfitLoss.Name = "lstProfitLoss"
Me.lstProfitLoss.Size = New System.Drawing.Size(620, 186)
Me.lstProfitLoss.TabIndex = 16
Me.lstProfitLoss.Visible = False
'
'lblProfitLoss
'
Me.lblProfitLoss.AutoSize = True
Me.lblProfitLoss.Font = New System.Drawing.Font("Verdana",
14.25!, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblProfitLoss.Location = New System.Drawing.Point(20, 249)
Me.lblProfitLoss.Name = "lblProfitLoss"
Me.lblProfitLoss.Size = New System.Drawing.Size(196, 23)
Me.lblProfitLoss.TabIndex = 17
Me.lblProfitLoss.Text = "Profit/Loss Table" &
Global.Microsoft.VisualBasic.ChrW(13) &
Global.Microsoft.VisualBasic.ChrW(10)
Me.lblProfitLoss.Visible = False
'
'frmMain
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(656, 473)
Me.Controls.Add(Me.lblProfitLoss)
Me.Controls.Add(Me.lstProfitLoss)
Me.Controls.Add(Me.lstOutput)
Me.Controls.Add(Me.txtPurchasePrice)
Me.Controls.Add(Me.lblPurchasePrice)
Me.Controls.Add(Me.btnQuit)
Me.Controls.Add(Me.btnShowProfitLoss)
Me.Controls.Add(Me.btnDisplay)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.btnAddStock)
Me.Controls.Add(Me.txtNumberOfShares)
Me.Controls.Add(Me.txtDatePurchased)
Me.Controls.Add(Me.lblNumberOfShares)
Me.Controls.Add(Me.lblDatePurchased)
Me.Controls.Add(Me.txtCurrentPrice)
Me.Controls.Add(Me.lblCurrentPrice)
Me.Controls.Add(Me.txtStock)
Me.Controls.Add(Me.lblStock)
Me.Name = "frmMain"
Me.Text = "Stock Analysis"
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub

#End Region

Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnQuit.Click
End
End Sub
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnDisplay.Click

Dim indexPicked As Integer

indexPicked = lstOutput.SelectedIndex

Select Case indexPicked '' maybe there is a better way to do
this? maybe not
Case indexPicked
txtStock.Text = StockData((indexPicked)).stockName
txtPurchasePrice.Text =
StockData((indexPicked)).purchasePrice
txtDatePurchased.Text =
StockData((indexPicked)).datePurchased
txtCurrentPrice.Text =
StockData((indexPicked)).currentPrice
txtPurchasePrice.Text =
StockData((indexPicked)).purchasePrice
txtNumberOfShares.Text =
StockData((indexPicked)).numShares
End Select

End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
IntializeData() ' Loading data from CSV routine
End Sub

Sub IntializeData()
Dim arr() As String
Dim row, i, displayStop As Integer
Dim sLine As String

Dim fstream As IO.StreamReader = New IO.StreamReader(New
IO.FileStream("csvstocks.txt", IO.FileMode.Open))
Dim fmtStr As String = "{0,-35}{1,-15}{2,25}{3,15} {4,40}"

row = 0

Do While fstream.Peek <-1 '' Load everything up!
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

ProfitRow = row '' asigns the max value to use later in the
Profit/Loss routine

fstream.Close()

i = 0

displayStop = row

Do Until i = displayStop '' Tricky nice way of doing that huh?
lstOutput.Items.Add(String.Format(fmtStr,
StockData((i)).stockName, StockData((i)).numShares,
StockData((i)).datePurchased, StockData((i)).purchasePrice,
StockData((i)).currentPrice))
row = row - 1
i = i + 1
Loop

End Sub

Private Sub btnShowProfitLoss_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnShowProfitLoss.Click

lstProfitLoss.Visible = True
lblProfitLoss.Visible = True
lstProfitLoss.Items.Clear()

Dim fmtStrProfit As String = "{0,55}{1,55}{2,45} {3,25}"

Dim tmpArray(50, 4) As String '' 50 rows should be enough for
this program
Dim j, xi As Integer

xi = ProfitRow
j = 0

' First write the 0 places (stock name)
Do Until j = ProfitRow
tmpArray(j, 0) = StockData((j)).stockName
j = j + 1
Loop

j = 0 ' reset this back to zero for each of the loops to do its
magic

Dim x As Integer ' need another variable for the next stages
x = 0

' Now lets do some massive math
Do Until j = ProfitRow
tmpArray(j, 1) = StockData((j)).numShares *
StockData((j)).purchasePrice
j = j + 1
Loop

j = 0 ' zero back out

Do Until j = ProfitRow
tmpArray(j, 2) = StockData((j)).numShares *
StockData((j)).currentPrice
j = j + 1
Loop

j = 0 ' zero out again!

Do Until j = ProfitRow
tmpArray(j, 3) = tmpArray(j, 2) - tmpArray(j, 1)
j = j + 1
Loop

j = 0

Do Until j = ProfitRow ' finally list them
lstProfitLoss.Items.Add(String.Format(fmtStrProfit ,
tmpArray(j, 0), tmpArray(j, 1), tmpArray(j, 2), tmpArray(j, 3)))
j = j + 1
Loop

End Sub

Private Sub btnAddStock_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnAddStock.Click

Dim sw As IO.StreamWriter = IO.File.AppendText("csvstocks.txt")
Dim tmpName, tmpDate As String
Dim tmpNum, tmpPurchase, tmpCurrent As Double

tmpName = txtStock.Text
tmpNum = txtNumberOfShares.Text
tmpDate = txtDatePurchased.Text
tmpPurchase = txtPurchasePrice.Text
tmpCurrent = txtCurrentPrice.Text
Dim tmpString() As String = {"tmpName", "tmpNum", "tmpDate",
"tmpPurchase", "tmpCurrent"}

sw.WriteLine(String.Join(","c, tmpString))

sw.Close()

End Sub
End Class

This is the error code that always shows up
Error 1 'frmMain' is a type in 'StockAnalysis' and cannot be used as an
expression. F:\Programming
Projects\StockAnalysis\Form1.vb 0 0 StockAnalysis

That's all the code, it crashes when you run it on
StockData(row).numShares = arr(1) saying its out of bounds.

This was working just a little bit before I put the Add stock button
code, even when I take out the code the error is still there and the
program crashes. I've probably deleted some part of code on accident
and can't figure it out. Thanks for the help

Nov 29 '06 #1
3 1257
OK Well I fixed the problem! Turns out my CSV file had some extra
spaces at the end.

Now a new question.

How can I fix that problem? :)

Nov 29 '06 #2
RallyDSM wrote:
OK Well I fixed the problem! Turns out my CSV file had some extra
spaces at the end.

Now a new question.

How can I fix that problem? :)
Do While fstream.Peek <-1 '' Load everything up!
sLine = fstream.ReadLine
arr = sLine.Split(","c)
' now check the value of UBound(arr)
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

HTH

Andrew
Nov 30 '06 #3
Take a look at your sLine value. I suspect you don't have 4 commas in one
of the lines.

Mike Ober.

"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:Ow**************@TK2MSFTNGP02.phx.gbl...
RallyDSM wrote:
>OK Well I fixed the problem! Turns out my CSV file had some extra
spaces at the end.

Now a new question.

How can I fix that problem? :)

Do While fstream.Peek <-1 '' Load everything up!
sLine = fstream.ReadLine
arr = sLine.Split(","c)
' now check the value of UBound(arr)
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

HTH

Andrew

Nov 30 '06 #4

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...
7
by: Andrew Joros (ezze16 | last post by:
I have no idea what the problem is... It is giving me this error: invalid use of array with unspecified bounds How can I fix it. Thanks. #include <stdio.h> // Prototype
8
by: ulyses | last post by:
I'm trying to put pointer to flexible array of structures in other structure. I want to have pointer to array of pixels in screen structure. Here is mine code, but I think it isn't quite all right:...
7
by: Paul M. Cook | last post by:
Let's say you have a CSV file and you load it into a variant array using the split function on VBCrLF. Then you load a variable with the line count and loop through the array for 0 to line count. ...
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...
52
by: Jim Langston | last post by:
I wanted to do an operator override for but couldnt' figure out the syntax. I tried this (code that doesn't compile commented out with //: class CMyBitmap { public: CMyBitmap( int Rows, int...
19
by: Sharath A.V | last post by:
I had an argument with someone on wheather this piece of code can invoke undefined bahaviour. I think it does not invoke any undefined behaviour since there is sufficient memory space of 9...
4
by: Joao | last post by:
Hi all, I have 2 listboxes with SelectionMode = MultiExtended and I just cannot figure out why I get the error below: System.IndexOutOfRangeException was unhandled Message="Index was outside...
8
by: aaragon | last post by:
Hi, just a very simple question. I was wondering what is the most efficient way of initializing an array. I have a very simple class that wraps an array to provide bound checking, something like...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.