473,325 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 1794

"G.Ashok" <gw******@hotmail.com> wrote in message
news:eo**************@TK2MSFTNGP14.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******@hotmail.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******@hotmail.com> wrote in message
news:eo**************@TK2MSFTNGP14.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.handleTransfer(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.deposit

'// handle standard deposit

End Sub

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

'// 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(currency As Currency)
Sub deposit(currency 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******@hotmail.com> wrote in message
news:eo**************@TK2MSFTNGP14.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.handleTransfer(New BankAccount)

End Sub

End Module

Public Interface IBankAccount

Sub deposit(ByVal currency As Currency)

' routines to handle the double dispatch.
Sub doCurrencyDeposit(ByVal currency As Currency)
Sub doEuroCurrencyDeposit(ByVal currency As EuroCurrency)

End Interface

Public Class Currency

' routines to handle the double dispatch
Public Overridable Sub Deposit(ByVal account As IBankAccount)
account.doCurrencyDeposit(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.doEuroCurrencyDeposit(Me)
End Sub

End Class

Public Class BankAccount
Implements IBankAccount

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

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

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

' only called by EuroCurrency objects
Public Sub doEuroCurrencyDeposit(ByVal currency As EuroCurrency)
Implements IBankAccount.doEuroCurrencyDeposit
' 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(Currency) & DoCurrencyDeposit(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 DoCurrencyDeposit &
DoEuroCurrencyDeposit 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******@hotmail.com> wrote in message
news:uS**************@TK2MSFTNGP14.phx.gbl...
|
| Hi, Thank you all for the response in anyway,
|
| Here the story...
|
| Module Module1
| Sub Main()
|
| Dim am As New AccountManager
|
| am.handleTransfer(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.deposit
|
| '// handle standard deposit
|
| End Sub
|
| Public Sub deposit(ByVal currency As EuroCurrency) ' Implements
| IBankAccount.deposit
|
| '// 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(currency As Currency)
| Sub deposit(currency 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******@hotmail.com> wrote in message
| news:eo**************@TK2MSFTNGP14.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
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...
18
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...
4
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...
3
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 ...
11
by: richard pickworth | last post by:
Can anyone explain polymorphism?(very simply). thanks richard
4
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...
13
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...
11
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++,...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.