所以我正在为我的班级做一个项目,我们必须编写一个班级来制作收银机。它是一个带有两个按钮的简单寄存器:加法和减法。您在第一个文本框中输入您想要的金额,结果显示在第二个文本框中。我从来没有在课堂上工作过,也不懂大多数与之相关的术语,所以简单的答案会很棒!这是我到目前为止的代码:
Public Class frmRegister
Dim reg As CashRegister
Structure Equations
Dim Amount As Double
Dim Balance As Double
End Structure
Class CashRegister
Private xAmount As Double
Private xBalance As Double
Public Property Amount As Double
Get
Return xAmount
End Get
Set(ByVal value As Double)
xAmount = value
End Set
End Property
Public Property Balance As Double
Get
Return xBalance
End Get
Set(ByVal value As Double)
xBalance = value
End Set
End Property
Function calc_sum() As Double
Dim AddMethod As Double
AddMethod = xBalance + xAmount
Return AddMethod
End Function
Function calc_sub() As Double
Dim SubtractMethod As Double
SubtractMethod = xBalance - xAmount
Return SubtractMethod
End Function
End Class
Private Sub frmRegister_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If txtBalance.Text = "" Then
txtBalance.Text = "0"
End If
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
reg.Amount = txtAmount.Text
reg.Balance = txtBalance.Text
txtBalance.Text = FormatCurrency(reg.calc_sum)
End Sub
Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
reg.Amount = txtAmount.Text
reg.Balance = txtBalance.Text
txtBalance.Text = FormatCurrency(reg.calc_sub)
If txtBalance.Text < 0 Then
MessageBox.Show("Transaction Cannot Result in Negative Balance!")
txtBalance.Text = "0"
End If
End Sub
End Class
当我到达按钮并引用 txtboxes 时,就会出现问题。他们在 txtAmount.text 下划线并说未处理空引用异常。我究竟做错了什么。怎么是空的?我尝试在前面添加 cdbl 但这并没有什么不同