Hello

I have 2 classes clsParty and clsAccount
clsParty has a function named GetParty()
clsAccount has a function named GetAccount()

I have a 3rd class named clsDoTran
What i want to do is i want to use GetParty() and GetAccount() functions from both clsParty and clsAccount

But there is a problem and the problem is i cannot inherit both the class at a time

Can u tell me how to do it?

Thanks and Regards

Dani AI

Generated

— VB.NET does not support inheriting more than one class. was right to suggest interfaces or composition: implement as many interfaces as you need (VB lets a class implement multiple interfaces) or give clsDoTran references to clsParty and clsAccount and delegate calls. That keeps responsibilities clear and avoids the multiple-inheritance problem documented by Microsoft . For how to define and implement interfaces see Interfaces (Visual Basic).

Example pattern (interface + constructor injection) — this makes testing and swapping implementations easy:

Interface IParty
    Function GetParty() As String
End Interface

Interface IAccount
    Function GetAccount() As String
End Interface

Class clsDoTran
    Private ReadOnly _party As IParty
    Private ReadOnly _account As IAccount

    Public Sub New(party As IParty, account As IAccount)
        _party = party
        _account = account
    End Sub

    Public Sub Execute()
        Dim p = _party.GetParty()
        Dim a = _account.GetAccount()
        ' do transaction work
    End Sub
End Class

If you prefer not to define interfaces, composition alone (private clsParty and clsAccount members) is fine for small projects, but interfaces give better decoupling. Prefer "has-a" (composition/delegation) over "is-a" (inheritance) when clsDoTran uses party/account behavior rather than being a specialized party/account.

Recommended Answers

All 3 Replies

This is simple, this is either you properly design your classes using Interfaces or if you're really on a hurry , create your clsDoTran with member variables of clsParty and clsAccount.

For this must be simple but as i've said i have just started working in vb.net its not so simple for me. The second option u gave is under my consideration but i dont want to got with that. Can u be more helpful if u give some example? Thanks

For this must be simple but as i've said i have just started working in vb.net its not so simple for me. The second option u gave is under my consideration but i dont want to got with that. Can u be more helpful if u give some example? Thanks

Article - Can you use multiple inheritance with VB.NET?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.