I am working on a problem in my text book. The interface allows the user to enter the number of each car type sold. The add to total button should use an array to accumulate the numbers sold by each car type. it also should display in lblNew and lblUsed the total of each sold. Is there a way to use a loop to add new integers to the arrays intNew() and intUsed() without knowing how many numbers the user will enter? (im only concerned with intNew() right now)
Public Class frmMain
Dim total As Integer = 0
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
' fill the list box with values
lstCarType.Items.Add("New")
lstCarType.Items.Add("Used")
lstCarType.SelectedIndex = 0
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
' adds the amount sold to the appropriate total
' declare accumulator array and variables
Dim intNew() As Integer = {}
'Dim intUsed As Integer = {}
Dim index As Integer = 0
Dim intSold As Integer
Dim intTotal As Integer
' update array value
If lstCarType.Text = "New" Then
Integer.TryParse(txtSold.Text, intSold)
ReDim Preserve intNew(index + 1)
intNew(index) = intSold
index += index
End If
' display array values
For intColumn As Integer = 0 To intNew.GetUpperBound(0)
intTotal = intNew(intColumn) + intTotal
Next
lblNew.Text = intTotal.ToString
txtSold.Focus()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub txtSold_Enter(sender As Object, e As EventArgs) Handles txtSold.Enter
txtSold.SelectAll()
End Sub
Private Sub txtSold_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtSold.KeyPress
' allow numbers and the Backspace
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
I edited as per Steves idea, i'm getting a few errors: Expression is of type 'Object', which is not a collection type(line 42). Option strict on disallows late binding(line 36), Object strict on requires all variable declarations to have an As clause(line 25). I would just turn strict off but i heard your not supposed to do that normally. Any ideas how to get this to work? Heres the updated code:
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Dim total As Integer = 0
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
' fill the list box with values
lstCarType.Items.Add("New")
lstCarType.Items.Add("Used")
lstCarType.SelectedIndex = 0
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
' adds the amount sold to the appropriate total
' declare accumulator array and variables
Dim intNew = New List(Of Integer)()
'Dim intUsed As Integer = {}
Dim index As Integer = 0
Dim intSold As Integer
Dim intTotal As Integer
' update array value
If lstCarType.Text = "New" Then
If Integer.TryParse(txtSold.Text, intSold) Then
intNew.Add(intSold)
End If
End If
' display the list values
For Each intValue As Integer In intNew
intTotal += intValue
Next
lblNew.Text = intTotal.ToString
txtSold.Focus()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub txtSold_Enter(sender As Object, e As EventArgs) Handles txtSold.Enter
txtSold.SelectAll()
End Sub
Private Sub txtSold_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtSold.KeyPress
' allow numbers and the Backspace
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
End Class