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

R

RallyDSM

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
 
R

RallyDSM

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? :)
 
A

Andrew Morton

RallyDSM said:
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
 
M

Michael D. Ober

Take a look at your sLine value. I suspect you don't have 4 commas in one
of the lines.

Mike Ober.
 

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