So my assignment this week seems fairly easy. We have to create a cash register that adds and subtract from the balance and doesn't permit negative amounts. We aso have to use a class called CashRegister.
Here is what it looks like:
Attachment 99689
Here is my code:
Public Class frmRegister
Dim Register As CashRegister
Structure Total
Dim Amount As Double
Dim Balance As Double
End Structure
Class CashRegister
Private crAmount As Integer
Private crBalance As Integer
Public Property Amount As Double
Get
Return crAmount
End Get
Set(ByVal value As Double)
crAmount = value
End Set
End Property
Public Property Balance As Double
Get
Return crBalance
End Get
Set(ByVal value As Double)
crBalance = value
End Set
End Property
Function amtAdd() As Double
Dim amt As Double
amt = crBalance + crAmount
Return amt
End Function
Function amtSub() As Double
Dim asub As Double
asub = crBalance - crAmount
Return asub
End Function
End Class
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Register.Amount = CDbl(txtAmount.Text)
txtBalance.Text = Register.amtAdd
End Sub
Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
Register.Amount = CDbl(txtAmount.Text)
txtBalance.Text = Register.amtSub
If txtBalance.Text < 0 Then
MessageBox.Show("Transaction Cannot Result in Negative Balance!")
End If
End Sub
End Class
Now everything seems fine and dandy until I actually run it. Then for the both the buttons the line "Register.Amount = CDbl(txtAmount.Text)" gives me the message "Null Reference Exception was Unhandled." What am I doing wrong? Also, would I start the txtBalance.Text on zero?
Here is what it looks like:
Attachment 99689
Here is my code:
Public Class frmRegister
Dim Register As CashRegister
Structure Total
Dim Amount As Double
Dim Balance As Double
End Structure
Class CashRegister
Private crAmount As Integer
Private crBalance As Integer
Public Property Amount As Double
Get
Return crAmount
End Get
Set(ByVal value As Double)
crAmount = value
End Set
End Property
Public Property Balance As Double
Get
Return crBalance
End Get
Set(ByVal value As Double)
crBalance = value
End Set
End Property
Function amtAdd() As Double
Dim amt As Double
amt = crBalance + crAmount
Return amt
End Function
Function amtSub() As Double
Dim asub As Double
asub = crBalance - crAmount
Return asub
End Function
End Class
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Register.Amount = CDbl(txtAmount.Text)
txtBalance.Text = Register.amtAdd
End Sub
Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
Register.Amount = CDbl(txtAmount.Text)
txtBalance.Text = Register.amtSub
If txtBalance.Text < 0 Then
MessageBox.Show("Transaction Cannot Result in Negative Balance!")
End If
End Sub
End Class
Now everything seems fine and dandy until I actually run it. Then for the both the buttons the line "Register.Amount = CDbl(txtAmount.Text)" gives me the message "Null Reference Exception was Unhandled." What am I doing wrong? Also, would I start the txtBalance.Text on zero?