0

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

0000
  • 677
  • 2
  • 8
  • 20

1 Answers1

1

Yes, instead of an array you should use a List(Of Integer) that doesn't need to be redimmed everytime you add a new value.

Dim intNew As List(Of Integer) = new List(Of Integer)()
Dim intTotal As Integer

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  In intNew
    intTotal += intValue 
Next
lblNew.Text = intTotal.ToString

As List(T) has many advantages against an array. The fixed size of the array is just the more visible, but you could use a List(T) at the same way as an array to discover the size or to access a single value by index

Dim countElements = intNew.Count

Dim secondValue = intNew(1)

And what about the IEnumerable Extensions?

Dim intTotal = intNew.Sum() 'bye bye explicit loop
lblNew.Text = intTotal.ToString
Steve
  • 213,761
  • 22
  • 232
  • 286
  • i went and tried your code, im gettin some errors, updated my code ^^ – 0000 Apr 07 '14 at 22:41
  • 1
    Not sure where are these lines, but try to change the declaration of the list to `Dim intNew As List(Of Integer) = New List(Of Integer)()` – Steve Apr 07 '14 at 22:45
  • Amazing how one change can fix everything! – 0000 Apr 07 '14 at 22:53
  • Well, good to know, I can't reproduce the problem with a simple console application. What version of Framework are you using and what version of Visual Studio? Just curious – Steve Apr 07 '14 at 22:54
  • 1
    From MSDN: _When you set Option Infer to On, you can declare local variables without explicitly stating a data type. The compiler infers the data type of a variable from the type of its initialization expression._ http://stackoverflow.com/questions/667851/best-practices-option-infer – Steve Apr 07 '14 at 22:57
  • I see, i guess turning Option Infer on might make it harder to debug, but easier to code. Im using Visual Studio 2012, with .Net FrameWork 4.5 :-) – 0000 Apr 07 '14 at 23:04