在用户输入数组时将整数分配给数组

时间:2014-04-07 22:01:11

标签: vb.net

我正在编写教科书中的问题。界面允许用户输入所售出的每种车型的数量。添加到总计按钮应使用数组来累积每种车型销售的数字。它也应该显示在lblNew和lblUsed中每个销售的总数。有没有办法使用循环向数组intNew()和intUsed()添加新的整数,而不知道用户将输入多少个数字? (我现在只关注intNew())

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

我按照Steves的想法编辑,我得到了一些错误:Expression类型为&#39; Object&#39;,它不是集合类型(第42行)。 Option strict on disallows late binding(第36行),Object strict on要求所有变量声明都有一个As子句(第25行)。我会严格关闭,但我听说你不应该这样做。任何想法如何让这个工作?下面是更新后的代码:

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

结束班

1 个答案:

答案 0 :(得分:1)

是的,您应该使用每次添加新值时都不需要重新编辑的List(Of Integer)而不是数组。

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

由于List(T)对阵列有许多优点。数组的固定大小更加明显,但您可以使用List(T)与数组相同的方式来发现大小或通过索引访问单个值

Dim countElements = intNew.Count

Dim secondValue = intNew(1)

那么IEnumerable Extensions呢?

Dim intTotal = intNew.Sum() 'bye bye explicit loop
lblNew.Text = intTotal.ToString