我正在为自己的作业创建会计应用程序。应用程序需要有一个单独的类,其中包含余额,利息,利率和交易次数的变量。余额应随每次提款和存款而变化。
我目前在余额类中有一个公共子账户,该子账户将存款添加到检查负值的变量中。在btnDeposit的主要单击中,我有标签将变量从余额类中拉出以显示。它显示值,但是下一个输入未添加到变量中,它仅显示当前存款。
Public Class Balance
Public dblBalance As Double
Public decDeposit As Decimal
Public decWithdraw As Decimal
Public dblIntrest As Double
Public dblIntRate As Double
Public intTransactions As Integer
Public Sub New()
dblBalance = 0
intTransactions = 0
decDeposit = 0
decWithdraw = 0
dblIntRate = 5
dblIntrest = 0
End Sub
Public Sub MakeDeposit()
decDeposit = InputBox("Enter the Deposit Amount", "Deposit", "0.00")
If decDeposit < 0 Then
MessageBox.Show("Enter a Positive Number")
ElseIf decDeposit >= 0 Then
dblBalance += decDeposit
intTransactions += 1
End If
End Sub
Public Class Form1
Private Sub btnDeposit_Click(sender As Object, e As EventArgs) Handles btnDeposit.Click
Dim Balance = New Balance()
Balance.MakeDeposit()
lblBalance.Text = Balance.dblBalance.ToString("C")
lblTransactions.Text = Balance.intTransactions
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblBalance.Text = 0.ToString("C")
lblInterest.Text = 0.ToString("C")
lblIntRate.Text = 0.ToString("P")
lblTransactions.Text = 0.ToString("G")
End Sub
End Class
变量应该每次都加上值。
答案 0 :(得分:1)
看起来您的存款按钮点击事件包含用于存储您的存款类实例的变量。因此,每次单击该按钮时,它将创建该类,然后在完成按钮单击事件后将其销毁。 您将需要将Dim Balance = New Balance()更改为类级变量,如下所示将其移出按钮单击过程
Public Class Form1
Dim Balance = New Balance()
Private Sub btnDeposit_Click(sender As Object, e As EventArgs) Handles btnDeposit.Click
Balance.MakeDeposit()
lblBalance.Text = Balance.dblBalance.ToString("C")
lblTransactions.Text = Balance.intTransactions
End Sub