473,761 Members | 10,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple polymorphism... How to?

Hi,

Does anybody know how Multiple polymorphism can be done in VB.NET or DOT
doesn't support it? Is there any third party extensions available like for
Java to do this?

Regards,
....Ashok
------------------------------------------------------------------------
"It is beautiful for an engineer to shape and design the same way
that an artist shapes and designs. But whether the whole process
makes any sense, whether men become happier - that I can no
longer decide." - Rudolph Diesel
Nov 21 '05 #1
6 1823

"G.Ashok" <gw******@hotma il.com> wrote in message
news:eo******** ******@TK2MSFTN GP14.phx.gbl...
Hi,

Does anybody know how Multiple polymorphism can be done in VB.NET or DOT
doesn't support it? Is there any third party extensions available like for
Java to do this?

Regards,
...Ashok


Can you describe what you want a whole lot better?
Polymorphism in itself can mean different things depending upon context.
If are are talking about simply implementing multiple interfaces on a single
object, then this is no problem.
Nov 21 '05 #2
"G.Ashok" <gw******@hotma il.com> schrieb:
Does anybody know how Multiple polymorphism can be done in VB.NET or DOT
doesn't support it? Is there any third party extensions available like for
Java to do this?


Are you referring to multiple inheritance? No, .NET doesn't support MI for
good reasons.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
G.

You can go as deep as you want to make from a cell an homo sapiens sapiens.

Or any other example.

I hope this helps,

Cor
Nov 21 '05 #4
Ashok,
As the others ask: What specifically do you mean by "Multiple polymorphism"?

Do you mean "Multiple Dispatch" (aka double dispatch) polymorphism?
Do you mean "Multiple Inheritance" as Herfried suggests? (PersonForm :
Inherits Form AndAlso Person)
Do you mean "Multiple Layer" inheritance as Cor Suggests? (MainForm :
inherits Form : ... inherits Control : ... Inherits Object)
Do you mean something else?
The "easiest" way to implement Double Dispatch in .NET is to use the Visitor
pattern.

Something like:

Public MustInherit Class C

Public Sub F(ByVal aC As C)
aC.Fc(Me)
End Sub

Public MustOverride Sub Fc(ByVal aC As C)

' NOTE we use qualified names rather then overloading here
' we could have just as easily call these all Fc...
Public MustOverride Sub Fc1(ByVal aC1 As C1)
Public MustOverride Sub Fc2(ByVal aC2 As C2)
Public MustOverride Sub Fc3(ByVal aC3 As C3)
Public MustOverride Sub Fc4(ByVal aC4 As C4)

End Class

Public Class C1
Inherits C

Public Overrides Sub Fc(ByVal aC As C)
aC.Fc1(Me)
End Sub

Public Overrides Sub Fc1(ByVal aC1 As C1)
Debug.WriteLine ("Fc1(c1)", "c1")
End Sub

Public Overrides Sub Fc2(ByVal aC2 As C2)
Debug.WriteLine ("Fc2(c2)", "c1")
End Sub

Public Overrides Sub Fc3(ByVal aC3 As C3)
Debug.WriteLine ("Fc3(c3)", "c1")
End Sub

Public Overrides Sub Fc4(ByVal aC4 As C4)
Debug.WriteLine ("Fc4(c4)", "c1")
End Sub

End Class

Public Class C2
Inherits C

Public Overrides Sub Fc(ByVal aC As C)
aC.Fc2(Me)
End Sub

Public Overrides Sub Fc1(ByVal aC1 As C1)
Debug.WriteLine ("Fc1(c1)", "c2")
End Sub

Public Overrides Sub Fc2(ByVal aC2 As C2)
Debug.WriteLine ("Fc2(c2)", "c2")
End Sub

Public Overrides Sub Fc3(ByVal aC3 As C3)
Debug.WriteLine ("Fc3(c3)", "c2")
End Sub

Public Overrides Sub Fc4(ByVal aC4 As C4)
Debug.WriteLine ("Fc4(c4)", "c2")
End Sub

End Class

Public Class C3
Inherits C

Public Overrides Sub Fc(ByVal aC As C)
aC.Fc3(Me)
End Sub

Public Overrides Sub Fc1(ByVal aC1 As C1)
Debug.WriteLine ("Fc1(c1)", "c3")
End Sub

Public Overrides Sub Fc2(ByVal aC2 As C2)
Debug.WriteLine ("Fc2(c2)", "c3")
End Sub

Public Overrides Sub Fc3(ByVal aC3 As C3)
Debug.WriteLine ("Fc3(c3)", "c3")
End Sub

Public Overrides Sub Fc4(ByVal aC4 As C4)
Debug.WriteLine ("Fc4(c4)", "c3")
End Sub

End Class

Public Class C4
Inherits C

Public Overrides Sub Fc(ByVal aC As C)
aC.Fc4(Me)
End Sub

Public Overrides Sub Fc1(ByVal aC1 As C1)
Debug.WriteLine ("Fc1(c1)", "c4")
End Sub

Public Overrides Sub Fc2(ByVal aC2 As C2)
Debug.WriteLine ("Fc2(c2)", "c4")
End Sub

Public Overrides Sub Fc3(ByVal aC3 As C3)
Debug.WriteLine ("Fc3(c3)", "c4")
End Sub

Public Overrides Sub Fc4(ByVal aC4 As C4)
Debug.WriteLine ("Fc4(c4)", "c4")
End Sub

End Class

Public Class Test

Public Shared Sub Main()
Dim xlist() As C = {New C1, New C2, New C3, New C4}
Dim ylist() As C = {New C1, New C2, New C3, New C4}

For Each x As C In xlist
For Each y As C In ylist
Debug.WriteLine (x, "x")
Debug.WriteLine (y, "y")
x.F(y)
Debug.WriteLine (Nothing)
Next
Next
End Sub

End Class
The C.F & C.Fc methods are performing the double dispatch. C.F is
dispatching to C.Fc, which is then dispatching back to one of C.Fc1, C.Fc2,
C.Fc3, C.Fc4...

I find using unique names for C.Fc1, C.Fc2, C.Fc3, C.Fc4 makes the code
easier to read, normally the "function" followed by the type name...
Hope this helps
Jay

"G.Ashok" <gw******@hotma il.com> wrote in message
news:eo******** ******@TK2MSFTN GP14.phx.gbl...
| Hi,
|
| Does anybody know how Multiple polymorphism can be done in VB.NET or DOT
| doesn't support it? Is there any third party extensions available like for
| Java to do this?
|
| Regards,
| ...Ashok
| ------------------------------------------------------------------------
| "It is beautiful for an engineer to shape and design the same way
| that an artist shapes and designs. But whether the whole process
| makes any sense, whether men become happier - that I can no
| longer decide." - Rudolph Diesel
|
|
Nov 21 '05 #5

Hi, Thank you all for the response in anyway,

Here the story...

Module Module1
Sub Main()

Dim am As New AccountManager

am.handleTransf er(New BankAccount)

End Sub

End Module

Public Interface IBankAccount

Sub deposit(ByVal currency As Currency)

'Sub deposit(ByVal currency As EuroCurrency)

End Interface

Public Class Currency

' // These Currency classes could as easily be interfaces, with no impact on
the examples.

' // members

'}

End Class

Public Class EuroCurrency : Inherits Currency

'// members

End Class

Public Class BankAccount : Implements IBankAccount

Public Sub deposit(ByVal currency As Currency) Implements
IBankAccount.de posit

'// handle standard deposit

End Sub

Public Sub deposit(ByVal currency As EuroCurrency) ' Implements
IBankAccount.de posit

'// handle Euro Currencies

End Sub

End Class

Public Class AccountManager

Public Sub handleTransfer( ByVal account As IBankAccount)

Dim currency As Currency = New EuroCurrency '// getCurrency()

'//

'// Make a deposit. In standard Java and *.NET languages,

'// this will always invoke BankAccount's first deposit method.

'// With Multiple Polymorphism, this will invoke the second deposit
method for

'// EuroCurrency (and its derived classes), otherwise the first
deposit method.

account.deposit (currency)

End Sub

End Class

Another workaround approach to the above problem is to create another
interface method, defining the following:

Public Interface IBankAccount
Sub deposit(currenc y As Currency)
Sub deposit(currenc y AS EuroCurrency)
End Inteface

But this would force all implementations to define a method for
the second case, even if none were desired. This will further forces to use
several huge amount of switch (Select Case/If TypeOf ) statements

With multiple polymorphism, if the currency returned by getCurrency is not a
EuroCurrency (inheritance is-a), the first deposit method defined in
BankAcount will be called. If the currency is a EuroCurrency, the second
(more specialized) deposit method will be called.

Double dispatch will not eliminate the switch statements.

I hope this helps to undertsand the problem in detail :o)

Regards,
....Ashok
------------------------------------------------------------------------
"It is beautiful for an engineer to shape and design the same way
that an artist shapes and designs. But whether the whole process
makes any sense, whether men become happier - that I can no
longer decide." - Rudolph Diesel
"G.Ashok" <gw******@hotma il.com> wrote in message
news:eo******** ******@TK2MSFTN GP14.phx.gbl...

| Hi,
|
| Does anybody know how Multiple polymorphism can be done in VB.NET or DOT
| doesn't support it? Is there any third party extensions available like for
| Java to do this?
|
| Regards,
| ...Ashok
| ------------------------------------------------------------------------
| "It is beautiful for an engineer to shape and design the same way
| that an artist shapes and designs. But whether the whole process
| makes any sense, whether men become happier - that I can no
| longer decide." - Rudolph Diesel
|
|


Nov 21 '05 #6
G.Ashok,
| Double dispatch will not eliminate the switch statements.
Double Dispatch is how you solve it. Without switch statements!

Look closely at my initial example & the following sample. It just happened
that my example C was both Currency & AccountManager. The sample below
separates the two types.

Something like:

Module Module1

Sub Main()

Dim manager As New AccountManager

manager.handleT ransfer(New BankAccount)

End Sub

End Module

Public Interface IBankAccount

Sub deposit(ByVal currency As Currency)

' routines to handle the double dispatch.
Sub doCurrencyDepos it(ByVal currency As Currency)
Sub doEuroCurrencyD eposit(ByVal currency As EuroCurrency)

End Interface

Public Class Currency

' routines to handle the double dispatch
Public Overridable Sub Deposit(ByVal account As IBankAccount)
account.doCurre ncyDeposit(Me)
End Sub

End Class

Public Class EuroCurrency : Inherits Currency

' routines to handle the double dispatch
Public Overrides Sub Deposit(ByVal account As IBankAccount)
account.doEuroC urrencyDeposit( Me)
End Sub

End Class

Public Class BankAccount
Implements IBankAccount

Public Sub deposit(ByVal currency As Currency) Implements
IBankAccount.de posit

' ask the specific type of currency to call me back via the
specific routine...
currency.Deposi t(Me)
End Sub

' only called by Currency objects
Public Sub doCurrencyDepos it(ByVal currency As Currency) Implements
IBankAccount.do CurrencyDeposit
' do the actual 'Currency' deposit here
End Sub

' only called by EuroCurrency objects
Public Sub doEuroCurrencyD eposit(ByVal currency As EuroCurrency)
Implements IBankAccount.do EuroCurrencyDep osit
' do the actual 'EuroCurrency' deposit here
End Sub

End Class

Public Class AccountManager

Public Sub handleTransfer( ByVal account As IBankAccount)

Dim currency As currency = New EuroCurrency '// getCurrency()

account.deposit (currency)

currency = new Currency

account.deposit (currency)

End Sub

End Class

NOTE: both deposit(Currenc y) & DoCurrencyDepos it(Currency) are needed in
IBankAccount to avoid an endless recursive loop, during the "callback" from
the double dispatch...

NOTE: Rather then have a class hierarchy for Currency I would consider
implementing a Money type as discussed by Kent Beck in "Test - Driven
Development - By Example" from Addison Wesley press.
http://www.nunit.org/samples.html

The "danger" of double dispatch is that DoCurrencyDepos it &
DoEuroCurrencyD eposit need to be public, which means they may be called
inappropriately , if all the types concerned are in the same assembly I would
consider making them Friend instead of Public...

| I hope this helps to undertsand the problem in detail :o)
I hope you understand my sample in detail! ;-)

Hope this helps
Jay
"G.Ashok" <gw******@hotma il.com> wrote in message
news:uS******** ******@TK2MSFTN GP14.phx.gbl...
|
| Hi, Thank you all for the response in anyway,
|
| Here the story...
|
| Module Module1
| Sub Main()
|
| Dim am As New AccountManager
|
| am.handleTransf er(New BankAccount)
|
| End Sub
|
| End Module
|
| Public Interface IBankAccount
|
| Sub deposit(ByVal currency As Currency)
|
| 'Sub deposit(ByVal currency As EuroCurrency)
|
| End Interface
|
| Public Class Currency
|
| ' // These Currency classes could as easily be interfaces, with no impact
on
| the examples.
|
| ' // members
|
| '}
|
| End Class
|
| Public Class EuroCurrency : Inherits Currency
|
| '// members
|
| End Class
|
| Public Class BankAccount : Implements IBankAccount
|
| Public Sub deposit(ByVal currency As Currency) Implements
| IBankAccount.de posit
|
| '// handle standard deposit
|
| End Sub
|
| Public Sub deposit(ByVal currency As EuroCurrency) ' Implements
| IBankAccount.de posit
|
| '// handle Euro Currencies
|
| End Sub
|
| End Class
|
| Public Class AccountManager
|
| Public Sub handleTransfer( ByVal account As IBankAccount)
|
| Dim currency As Currency = New EuroCurrency '// getCurrency()
|
| '//
|
| '// Make a deposit. In standard Java and *.NET languages,
|
| '// this will always invoke BankAccount's first deposit method.
|
| '// With Multiple Polymorphism, this will invoke the second deposit
| method for
|
| '// EuroCurrency (and its derived classes), otherwise the first
| deposit method.
|
| account.deposit (currency)
|
| End Sub
|
| End Class
|
| Another workaround approach to the above problem is to create another
| interface method, defining the following:
|
| Public Interface IBankAccount
| Sub deposit(currenc y As Currency)
| Sub deposit(currenc y AS EuroCurrency)
| End Inteface
|
| But this would force all implementations to define a method for
| the second case, even if none were desired. This will further forces to
use
| several huge amount of switch (Select Case/If TypeOf ) statements
|
| With multiple polymorphism, if the currency returned by getCurrency is not
a
| EuroCurrency (inheritance is-a), the first deposit method defined in
| BankAcount will be called. If the currency is a EuroCurrency, the second
| (more specialized) deposit method will be called.
|
| Double dispatch will not eliminate the switch statements.
|
| I hope this helps to undertsand the problem in detail :o)
|
| Regards,
| ...Ashok
| ------------------------------------------------------------------------
| "It is beautiful for an engineer to shape and design the same way
| that an artist shapes and designs. But whether the whole process
| makes any sense, whether men become happier - that I can no
| longer decide." - Rudolph Diesel
|
|
| "G.Ashok" <gw******@hotma il.com> wrote in message
| news:eo******** ******@TK2MSFTN GP14.phx.gbl...
|
|| Hi,
||
|| Does anybody know how Multiple polymorphism can be done in VB.NET or DOT
|| doesn't support it? Is there any third party extensions available like
for
|| Java to do this?
||
|| Regards,
|| ...Ashok
|| ------------------------------------------------------------------------
|| "It is beautiful for an engineer to shape and design the same way
|| that an artist shapes and designs. But whether the whole process
|| makes any sense, whether men become happier - that I can no
|| longer decide." - Rudolph Diesel
||
||
|
|
|
|
Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
2844
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily use the instance of this base class (or its children class). How can I ensure the instance IS-A base class instance, since Python is a fully dynamic typing language? I searched and found several different ways to do this:
18
12598
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion about whether we should allow polymorphism. Our system is not embedded and does not need to be as real-time as, say, a pacemaker. But it does involve updating displays based on radar input. So a need for something close to real-time is desired...
4
2403
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a hierarchy of algebraic matrices with the addition operation. Thus, I want to have a virtual base class class Matr;
3
7450
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming http://en.wikipedia.org/wiki/Polymorphism http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?query=polymorphism&action=Search
11
5105
by: richard pickworth | last post by:
Can anyone explain polymorphism?(very simply). thanks richard
4
7394
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition of polymorphism, but he insisted on runtime. Did I miss a new buzzword while I was sick with the flu last week or something like that?
13
14617
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and Nicolai M. Josuttis. Chapter 14.) How to implement analogue of this technique via static polymorphism? Perhaps there is special design pattern for this purpose...
11
2976
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++, that anybody who doesn't use it might as well just program in plain C. I totally disagree with this, because I think C++ has a lot of great features apart from polymorphism, such as the ability to organize code into classes, code reuse through...
17
3881
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
9521
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9333
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10107
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9945
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9900
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7324
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3863
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.