Class Functions-VBForums
Results 1 to 2 of 2

Thread: Class Functions

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    23

    Class Functions

    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:
    Name:  CR.jpg
Views: 131
Size:  13.5 KB
    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?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,578

    Re: Class Functions

    First things first, please wrap your code snippets in formatting tags. There is a button for each on the advanced editor or you can type [code][/code] or [highlight=vb.net][/highlight] yourself.

    As for the issue, you never actually create a CashRegister object and assign it to your Register variable. You can't set the Amount property of an object that doesn't exist.

    By the way, variables should start with a lower-case letter. Upper-case is used for types, methods and properties most other things too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.