10 Replies - 3816 Views - Last Post: 10 November 2011 - 07:20 PM Rate Topic: -----

#1 fsavage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 11

Is it possible to create a list of mulitple classes?

Posted 05 November 2011 - 05:39 PM

Hi all, I am working on an assignment that requires several classes, two of which inherit a base class.

Base Class:
Public Class Account
    Private m_balance As Double
    Private m_accountNumber As String


Child Class 1:
Public Class Checking
    Inherits Account
    Private m_accountType As Char


Child Class 2:
Public Class Savings
    Inherits Account
    Private m_accountType As Char


I am given the data to load for the above classes in the following format:

[Account #, Checking Initial Balance, Savings Initial Balance]

Is it possible to create a list that would contain both the Checking balance and the Savings balance since they are separate classes? Would it be a list of the base class only?

Is This A Good Question/Topic? 0
  • +

Replies To: Is it possible to create a list of mulitple classes?

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1029
  • View blog
  • Posts: 4,007
  • Joined: 02-July 08

Re: Is it possible to create a list of mulitple classes?

Posted 05 November 2011 - 05:44 PM

Dim clsList As New List(Of Account)


Since all your forms inherit from Account they can be added to the clsList.

Is that what you mean?
Was This Post Helpful? 1
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,845
  • Joined: 18-April 07

Re: Is it possible to create a list of mulitple classes?

Posted 05 November 2011 - 07:56 PM

As Hawk states, yes, it would be a list of the base class. Checking and Savings accounts (types of class Account) can be put into it. Since checking and savings "are a" type of Account, they share all the functionality of the base.

But keep in mind that your list will be limited to the functionality of the base class. C# can't initially tell if the class is a savings, checking or a regular account class instance. So it must treat them all as the base class since all classes are guaranteed to have at LEAST that minimum functionality.

You can certainly inspect the class and figure out its underlying type and then convert it back to a Savings/Checking account instance. Just remember this when you try to pull out the objects from your list.

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

#4 fsavage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 11

Re: Is it possible to create a list of mulitple classes?

Posted 06 November 2011 - 10:56 AM

I receive the error "Too many arguments to "Public Sub Add(item as T)'" when I try to build my list of Account, which makes sense, since the Account class only has two members and I am attempting to add 3 arguments.

Below is part of the class where I need to create my list, as you can see in line two, we are supposed to declare the list as a class members

Public Class Banking_System
    Public Property accounts As List(Of Account)
    Public Property customers As List(Of Customer)
    Public Property bankName As String

    Public Sub Main()
        accounts = New List(Of Account)
        accounts.Add("10000", 500, 1500)

    End Sub


Since the Account class only has the following properties:
Public Class Account
    Private m_balance As Double
    Private m_accountNumber As String 


It does not want to accept a balance for both the Checking and Savings accounts as arguments. Would I be better off just creating separate lists for checking and savings?
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1029
  • View blog
  • Posts: 4,007
  • Joined: 02-July 08

Re: Is it possible to create a list of mulitple classes?

Posted 06 November 2011 - 11:29 AM

The .Add is for adding a new Account class object so if those are passed to a constructor it would look like this:

accounts.Add(New Account("10000", 500))
or even'
accounts.Add(New Saving("10000", 500))


The List(Of T) has no idea what members you have and cannot randomly set them - you have to set them explicitly. You can't access Private members when you create your new account object unless you use a constructor.

Public Class Account
    Private m_balance As Double
    Private m_accountNumber As String 
Sub New(acctNumber as String, balance As Double)
  Me.m_balance = balance
  Me.m_accountNumber = acctNumber
End Sub
...
End Class


This post has been edited by _HAWK_: 06 November 2011 - 01:34 PM

Was This Post Helpful? 0
  • +
  • -

#6 fsavage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 11

Re: Is it possible to create a list of mulitple classes?

Posted 08 November 2011 - 08:46 PM

Thank you Martyr2 and Hawk, I finally figured out creating the list of Account with the derived classes Checking and Savings as:
    Private m_accounts As List(Of Account)

    Public Sub New()
        m_accounts = New List(Of Account)
    End Sub

    Public Sub AddAccount(ByVal acct As String, ByVal cBalance As Double, ByVal sBalance As Double)
        m_accounts.Add(New Checking(acct, cBalance))
        m_accounts.Add(New Savings(acct, sBalance))
    End Sub

So as you can probably imagine, when I look at the list that is generated it looks something like:
-Accounts
(0) = {Account Number: 10000 Amount: 500Account Type C}
Checking = {Account Number: 10000 Amount: 500Account Type C}

(1) = {Account Number: 10000 Amount: 1500Account Type S}
Savings = {Account Number: 10000 Amount: 1500Account Type S}


So I am trying to work through Martyr2's message about:

Quote

You can certainly inspect the class and figure out its underlying type and then convert it back to a Savings/Checking account instance. Just remember this when you try to pull out the objects from your list.


I am attempted to loop through using a For/Next loop:
    Protected Sub lbtnChecking_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnChecking.Click
        Dim type As Char
        Dim balance As Double
        Dim acctNum As String

        type = "C"
        For Each cust As Customer In tran
            For Each acct As Checking In bank.Accounts
                If cust.AccountNumber = acct.AcctNum Then
                    acctNum = cust.AccountNumber
                    balance = acct.Balance
                End If
            Next
        Next
        AddToSession(acctNum, type, balance)
        Response.Redirect("TranType.aspx")
    End Sub

However I receive a runtime error: Unable to cast object of type 'Savings' to type 'Checking'.
on line:
 For Each acct As Checking In bank.Accounts

Did I not create my list correctly?
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,351
  • Joined: 29-May 08

Re: Is it possible to create a list of mulitple classes?

Posted 08 November 2011 - 08:57 PM

Is Saving a Checking ?

Remember this saying A Banana is a Fruit, not every Fruit is a Banana.

Class Banana
   Inherits Fruit


Was This Post Helpful? 0
  • +
  • -

#8 fsavage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 11

Re: Is it possible to create a list of mulitple classes?

Posted 09 November 2011 - 04:49 PM

I think I actually got this figured out late last night. :bananaman: I am not sure that this would be the proper way to go about identifying the data needed, but it seems to be working:

        Dim type As Char
        Dim balance As Double
        Dim acctNum As String

        'retrieve balance of Checking account from bank.Accounts list
        For Each cust As Customer In tran
            For Each item In bank.Accounts
                If cust.AccountNumber = item.AcctNum Then
                    If type = "C" And balance = 0 Then    
                        acctNum = cust.AccountNumber
                        balance = item.Balance
                    End If
                End If
            Next

I have to check that the balance is zero on line 11 otherwise it triples the balance by seeing it at each level inside the list:

-Accounts
(0) = {Account Number: 10000 Amount: 500Account Type C}
Checking = {Account Number: 10000 Amount: 500Account Type C}

Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,351
  • Joined: 29-May 08

Re: Is it possible to create a list of mulitple classes?

Posted 09 November 2011 - 06:01 PM

I think you are still having trouble with the concept of inheritance.
Let's go back to my analogy of fruit.
Spoiler



So now onto your Inheritance Tree
  • Account
    • Checking
    • Savings


How can we test to see which specific type of Account your got?

This post has been edited by AdamSpeight2008: 09 November 2011 - 06:03 PM

Was This Post Helpful? 1
  • +
  • -

#10 fsavage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 11

Re: Is it possible to create a list of mulitple classes?

Posted 09 November 2011 - 07:53 PM

You are correct Adam, when I ran through testing again tonight I realized it did not work as I expected. I guess what I do not understand about inheritance is how to "inspect that class" and "convert it to the derived class". Can you suggest any good tutorials on this subject?

I located more information on using a list of base class. One article talks about using OfType to filter the derived class you need. I am still trying to get this to work since when I enter code such as:
bank.Accounts.OfType(Checking)()


I get an error saying that "Checking is a type & cannot be used as an expression"

Any good tutorials that expand beyond just creating derived classes are welcome...none of my text books go any further than creation of derived classes.
Was This Post Helpful? 0
  • +
  • -

#11 fsavage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 11

Re: Is it possible to create a list of mulitple classes?

Posted 10 November 2011 - 07:20 PM

Hi Adam, my apologies, I did not see the spoiler in your post last night. Thank you.

I am trying to apply your lesson by doing the following:

Dim bank As New Banking_System
bank = DirectCast(Session("customer"), Banking_System)

'//where Accounts is a List(Of Account) in the Banking_System class
        If TypeOf bank.Accounts Is Checking Then
            '//do something
        End If


I receive the error: System.Collections.Generic.List(Of Account) can never be of type Checking
I also tried placing the following in my Bank_System class and then call it in my Main class. I do not receive any errors, but it returns all accounts, both Savings and Checking.

Public Function Check() As IEnumerable(Of Checking)
        Return Me.m_accounts.OfType(Of Checking)()
    End Function

This post has been edited by fsavage: 10 November 2011 - 07:21 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1