按钮单击时增加全局数组的索引值

时间:2015-08-11 13:48:54

标签: arrays indexing global

这是我的问题,我想增加全局数组的索引以返回不同的值。

但每次点击按钮都会重置为旧值。

这是我的代码......

Public Class Form1

Inherits System.Windows.Forms.Form
Dim a(5) As Decimal
Dim i As Integer
Dim j As Integer=0




Private Sub btnCalculate_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnCalculate.Click

    For i = j To j
        Dim dOrderTotal As Decimal
        Dim dDiscountPct As Decimal
        Dim dDiscountAmount As Decimal
        Dim dInvoiceTotal As Decimal

        dOrderTotal = txtOrderTotal.Text
        If dOrderTotal >= 100 Then
            dDiscountPct = 0.2
        Else
            dDiscountPct = 0
        End If
        dDiscountAmount = dOrderTotal * dDiscountPct
        dInvoiceTotal = dOrderTotal - dDiscountAmount

        lblDiscountAmount.Text = dDiscountAmount
        lblInvoiceTotal.Text = dInvoiceTotal
        txtOrderTotal.Focus()
        a(i) = dInvoiceTotal

    Next
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnExit.Click
    MessageBox.Show(a(0) & ControlChars.CrLf & a(1) & ControlChars.CrLf & a(2)& ControlChars.CrLf & a(3) & ControlChars.CrLf & a(4))



    Me.Close()
End Sub

Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub[![enter image description here][1]][1]

1 个答案:

答案 0 :(得分:1)

我确信您的代码执行for循环。 你从零开始到零,这可能就是你没有看到预期结果的原因

For i = j To j

j始终为零,并且未在您显示的代码中更新。 用于更新数组值的子例程的代码应为:

Private Sub btnCalculate_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnCalculate.Click
    Dim dOrderTotal As Decimal
    Dim dDiscountPct As Decimal
    Dim dDiscountAmount As Decimal
    Dim dInvoiceTotal As Decimal

    dOrderTotal = txtOrderTotal.Text
    If dOrderTotal >= 100 Then
        dDiscountPct = 0.2
    Else
        dDiscountPct = 0
    End If
    dDiscountAmount = dOrderTotal * dDiscountPct
    dInvoiceTotal = dOrderTotal - dDiscountAmount

    lblDiscountAmount.Text = dDiscountAmount
    lblInvoiceTotal.Text = dInvoiceTotal
    txtOrderTotal.Focus()
    a(j) = dInvoiceTotal
    j = j + 1
    if j = 5 Then j = 0

End Sub