我的累加器无法在VB中运行(Visual Studio 2010)

时间:2012-04-11 17:03:48

标签: vb.net

我的累加器(decTotalCredits)无法正常工作,并且每个条目都重置为零。我需要累加器添加每个用户输入,直到它达到125.通常情况下,我确定它是小的东西。在此先感谢您的帮助!

    ' Declaring variable for Credit Entered by User
    Dim decCredit As Decimal = CDec(txtCredit.Text)
    ' Declaring the accumulator
    Dim decTotalCredits As Decimal

    If IsNumeric(txtCredit.Text) Then
        decCredit = Convert.ToDecimal(txtCredit.Text)
        Select Case decCredit
            Case Is = 5
            Case Is = 10
            Case Is = 25
            Case Is = 100
            Case Else
                MsgBox("Please enter a valid coin amount", , "Invalid Amount Entered")
        End Select

        If decTotalCredits < 125 Then

            decTotalCredits += decCredit
            lblTotal.Text = CStr(decTotalCredits)
            lblTotal.Visible = True

            txtCredit.Clear()
            txtCredit.Focus()

        Else

            ' Once the credits are reached the prompt to make selection is visible. 
            lblMakeSelection.Visible = True

            ' Once the credits are reached, the buttons for selection become enabled.
            btnDietPepsi.Enabled = True
            btnPepsi.Enabled = True
            btnSierraMist.Enabled = True
            btnLemonade.Enabled = True
            btnDrPepper.Enabled = True
            btnWater.Enabled = True
        End If
    Else
        MsgBox("Please enter a valid Coin amount", , "Input Error")


    End If

1 个答案:

答案 0 :(得分:3)

它被重新宣布..

  • 如果这是循环,则需要在循环外声明DecTotalCredits。
  • 如果这是在一个事件处理程序中,你需要在代码的顶部声明DecTotalCredits(在类的声明下面)这样每次处理程序运行时它都不会被重新声明