6 Replies - 1333 Views - Last Post: 13 February 2013 - 07:10 PM Rate Topic: -----

#1 Jas0791  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-September 11

How to call method base class in program passing sub class objects.

Posted 22 April 2012 - 08:33 AM

Base class has one field to hold numeric balance value. With 2 methods that accept arguments for adding and subtracting the new input calculating new balance.
Sub class has four fields dates, transaction, memo and amount.
I have a deposit form, and withdraw form. Each time one transaction is entered it creates an object with sub class fields, then adds to the account collection. My problem is not understanding how to call the deposit/withdraw method and pass the current transaction amount back to the base class to calculate the new balance. Does anyone have any links to information/tutorials on how to perform something like this? As you can see with my code I have tried various different approaches without any success.
 'Base Class Deposit procedure accepts the current deposit amount adding to the balance.
    Public Sub AddDeposit(ByVal deposit As Double)
        dblAccountBalance += deposit
    End Sub

'Deposit form to enter new transaction infomation.
        Dim dates As String
        Dim transaction As String
        Dim memo As String
        Dim deposit As Double
        Dim newBalance As Double

       
            dates = txtDateDeposit.Text
            transaction = lblTransaction.Text
            memo = txtMemo.Text
            deposit = CDbl(txtAmountDeposit.Text)
            newBalance = AddDeposit(deposit)
 'Add new object to the collection.
g_accountCollection.Add(New CheckingAcct(dates, transaction, memo, deposit, newBalance))





Is This A Good Question/Topic? 0
  • +

Replies To: How to call method base class in program passing sub class objects.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: How to call method base class in program passing sub class objects.

Posted 22 April 2012 - 10:43 AM

By using the keyword "MyBase". Using this keyword you can call methods of the base class and pass it whatever value.

So lets say you have a base class called "Account". From your derived class you can then call the deposit method in the base like so...

MyBase.AddDeposit(someAmount)



Now you have to ask yourself, why are you needing to call the base class for deposit/withdraw? These are methods that your derived class itself should have inherited from the base. Makes sense for the CheckingAcct to have a deposit method itself, without needing to call the base. CheckingAcct would inherit AddDeposit from the base class and thus you would go...

Dim chkAcct as New CheckingAcct(dates, transaction, memo, deposit, newBalance)

' Add 100 to the account. 
chkAcct.AddDeposit(100.00)



To do this you simply need to override the AddDeposit() method. You will want to do this for other reasons too. Suppose that your CheckingAcct adds a fee to each deposit. This is something you can implement in the AddDeposit method of the CheckingAcct class, but would not be part of all accounts so not found in the base.

I suggest you review this. But the answer to your question is the "MyBase" keyword.

:)
Was This Post Helpful? 1
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: How to call method base class in program passing sub class objects.

Posted 22 April 2012 - 10:57 AM

What are the visibility and access modifier you have used on the base class method?
Maybe they need changing.
The only case you need to use MyBase is when want to access the base method rather than the Overriden one in the derived class.
Was This Post Helpful? 0
  • +
  • -

#4 Jas0791  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-September 11

Re: How to call method base class in program passing sub class objects.

Posted 22 April 2012 - 11:57 AM

Thanks for the tips, the balance is not keeping a running balance total with the different transactions. It is just adding the current amount for each transaction. When I create a new object it resets the balance to zero I believe. So how do I carry over the balance to maintain a running total? I have 2 ways I am working with adding the transaction to the collection, which of these makes more sense in your opinion?

 Private Sub btnRecordDeposit_Click(sender As System.Object, e As System.EventArgs) Handles btnRecordDeposit.Click
        'Dim objDeposit As New CheckingAcct
        Dim dates As String
        Dim transaction As String
        Dim memo As String
        Dim depositAmt As Double
        Dim newBalance As Double

        Try
            dates = txtDateDeposit.Text
            transaction = lblTransaction.Text
            memo = txtMemo.Text
            depositAmt = CDbl(txtAmountDeposit.Text)
            'newBalance = MyBase.AddDeposit(depositAmt)

        Catch ex As Exception
            'Error message.
            MessageBox.Show("Deposit amount must be numeric.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            'For check amount error for non-numeric input.
            txtAmountDeposit.Selectionstart = 0
            txtAmountDeposit.SelectionLength = txtAmountDeposit.Text.Length
            txtAmountDeposit.Focus()
        End Try
        Dim deposit As New CheckingAcct(dates, transaction, memo, depositAmt, newBalance)
        'Call AddDeposit procedure to add current deposit amount to balance.
        deposit.AddDeposit(depositAmt)
        'Add new object to the collection.
        g_accountCollection.Add(deposit)

        MessageBox.Show("Deposit Saved.", "Confirmation")
        Clearform()

    End Sub



Private Sub btnRecord_Click(sender As System.Object, e As System.EventArgs) Handles btnRecord.Click
        'Create new Checking Account object to hold current transaction data.
        Dim check As New CheckingAcct
        'Test for negative input by user.
        If txtAmtCheck.Text < 0 Then
            MessageBox.Show("Check amount must be positive.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtAmtCheck.Selectionstart = 0
            txtAmtCheck.SelectionLength = txtAmtCheck.Text.Length
            txtAmtCheck.Focus()
        Else
            'Add form data to the Check object.
            WriteCheck(check)
            'Call SubtractCheck procedure to calculate new balance subtracting the check amount entered by user.
            check.SubtractCheck(check.Amount)
            'Add Check object to the collection.
            g_accountCollection.Add(check)
            'MessageBox.Show(check.ToString())
        End If
        MessageBox.Show("Check recorded", "Confirmation")
        Clearform()

    End Sub
    'Copy Write Check form data to the Checking Account object.
    Private Sub WriteCheck(ByVal c As CheckingAcct)

        'Test for numeric input only.
        Try
            c.Dates = txtDateCheck.Text
            c.Transaction = CInt(txtNumCheck.Text)
            c.Memo = txtPayee.Text
            c.Amount = CDbl(txtAmtCheck.Text)

        Catch ex As Exception
            MessageBox.Show("Please enter numeric values only.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            'For check amount error for non-numeric input.
            txtAmtCheck.Selectionstart = 0
            txtAmtCheck.SelectionLength = txtAmtCheck.Text.Length
            txtAmtCheck.Focus()
            'For check number error for non-numeric input.
            txtNumCheck.Selectionstart = 0
            txtNumCheck.SelectionLength = txtAmtCheck.Text.Length
            txtNumCheck.Focus()
        End Try

    End Sub


Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: How to call method base class in program passing sub class objects.

Posted 22 April 2012 - 12:04 PM

It's easier to start with the design of the classes, rather than the GUI.
Which why I tend to prefer starting with console applications first.

A Previous similar topic

Can you show use your base class, a derived classes?

This post has been edited by AdamSpeight2008: 22 April 2012 - 12:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 Jas0791  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-September 11

Re: How to call method base class in program passing sub class objects.

Posted 22 April 2012 - 02:32 PM

View PostAdamSpeight2008, on 22 April 2012 - 07:04 PM, said:

It's easier to start with the design of the classes, rather than the GUI.
Which why I tend to prefer starting with console applications first.

A Previous similar topic

Can you show use your base class, a derived classes?

Here are my classes, now I am having trouble with keeping a running account balance total, when I add new object (transaction) the account balance equals the current amount only.

'BankAccount is  my Base Class.

Public Class BankAccount
    Private strAcctName As String
    Protected dblAccountBalance As Double = 0.0
    Protected dblCreditsTotal As Double = 0.0
    Protected dblDebitsTotal As Double = 0.0
    Protected dblChecksTotal As Double = 0.0
    Protected dblDepositTotal As Double = 0.0
    Protected dblWithdrawTotal As Double = 0.0
    'Default constructor.
    Public Sub New()
        dblAccountBalance = 0.0
    End Sub
    'Constructor for creating with arguments.
    Public Sub New(ByVal balance As Double)
        dblAccountBalance = balance
    End Sub
    'Account Balance property.
    Public Property AccountBalance() As Double
        Get
            Return dblAccountBalance
        End Get
        Set(value As Double)
            dblAccountBalance = value
        End Set
    End Property
   
    'The UpdateBalance procedure updates the account balance.
    Public Sub UpdateBalance()
        dblAccountBalance = dblCreditsTotal + dblDebitsTotal
    End Sub
    'Overridden ToString method.
    Public Overrides Function ToString() As String
        Dim str As String
        str = "Balance:  " & dblAccountBalance.ToString("c")
        Return str
    End Function
    'Deposit procedure accepts the current deposit amount adding to the balance.
    Public Sub AddDeposit(ByVal deposit As Double)
        'dblAccountBalance += deposit
        dblCreditsTotal += deposit
    End Sub
    'SubtractCheck procedure accepts the current check amount subtracting from the balance.
    Public Sub SubtractCheck(ByVal checkAmount As Double)
        'dblAccountBalance += checkAmount
        dblDebitsTotal += checkAmount
    End Sub
    'SubtractWithdraw procedure accepts the current check amount subtracting from the balance.
    Public Sub SubtractWithdraw(ByVal withdrawAmount As Double)
        dblAccountBalance += withdrawAmount
    End Sub
End Class


'CheckingAcct is Sub Class of BankAccount class.

Public Class CheckingAcct
    Inherits BankAccount

    Protected strDate As String
    Protected strTransaction As String
    Protected strMemo As String
    Protected dblAmount As Double = 0.0

    'Default Constructor to initialize member variables.
    Public Sub New()
        strDate = String.Empty
        strTransaction = String.Empty
        strMemo = String.Empty
        dblAmount = 0.0

    End Sub
    'Constructor for creating objects with arguments.
    Public Sub New(ByVal d As String, ByVal type As String, ByVal memo As String, ByVal amount As Double, ByVal balance As Double)
        strDate = d
        strTransaction = type
        strMemo = memo
        dblAmount = amount
        dblAccountBalance = balance
    End Sub
    'Dates property to hold the date of transaction.
    Public Property Dates() As String
        Get
            Return strDate
        End Get
        Set(value As String)
            strDate = value
        End Set
    End Property
    'Transaction property to hold the type of transaction (deposit, check number, withdraw).
    Public Property Transaction() As String
        Get
            Return strTransaction
        End Get
        Set(value As String)
            strTransaction = value
        End Set
    End Property
    'Memo property to hold notes or payee for check.
    Public Property Memo() As String
        Get
            Return strMemo
        End Get
        Set(value As String)
            strMemo = value
        End Set
    End Property
    'Amount property to hold transaction amount (deposit, check amount, withdraw)
    Public Property Amount() As Double
        Get
            Return dblAmount
        End Get
        Set(value As Double)
            dblAmount = value
            UpdateBalance()
        End Set
    End Property

    'Public Function CalculateBalance(ByRef bal As Double, ByVal amt As Double)
    'Dim dblNewBalance As Double
    '   dblNewBalance = bal + amt
    '  Return dblNewBalance
    'End Function

    'Public Overridable Sub UpdateDebits()
    '   dblDebitsTotal = dblWithdrawTotal + dblChecksTotal
    'End Sub

    'Public Function DisplayBalance() As String
    'Dim strAcctBalance As String
    '   strAcctBalance = dblAccountBalance.ToString("c")
    '  Return strAcctBalance
    'End Function

    'Overridden ToString method.
    Public Overrides Function ToString() As String
        Dim str As String
        str = "Date:  " & strDate & vbTab & vbTab & "Transaction:  " & strTransaction & vbTab &
            "Memo:  " & strMemo & vbTab & "Amount:  " & dblAmount.ToString("c") & vbTab & vbTab &
            MyBase.ToString()
        Return str
    End Function
End Class


Was This Post Helpful? 0
  • +
  • -

#7 tycos  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 12-February 13

Re: How to call method base class in program passing sub class objects.

Posted 13 February 2013 - 07:10 PM

When you call new you are setting it to 0, so a new BankAccount will always start at Zero.

Public Sub New()
 dblAccountBalance = 0.0
End Sub



My question here is how are you saving the BankAccount information?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1