Multi Inherence

  • Thread starter Thread starter Bernard Bourée
  • Start date Start date
B

Bernard Bourée

Is there a way to overpass the impossibility of VN.NET to accept the multi
heritage, that is to allow a class to inherit from TWO mother classes ?
 
Bernard Bourée said:
Is there a way to overpass the impossibility of VN.NET to accept the multi
heritage, that is to allow a class to inherit from TWO mother classes ?

Not directly, but you can go the delegation way that was used in VB6 to
archieve implementation inheritance.
 
No.

You will have to achieve what you want with inheriting from 1 and using
interfaces. Or having your class contain an instance of another as a
private member, and forwarding method calls to that object (if that can work
for your situation).
 
Is there a way to overpass the impossibility of VN.NET to accept the
Not directly, but you can go the delegation way that was used in VB6 to
archieve implementation inheritance.

Here's a bizarre idea from left field... C++ can do multiple inheritance.
Could he do it via C++ / Managed Extensions and then use it from VB?
 
Herfried

Can you tell me more about delegation or some links ?

Thanks
 
Bernard Bourée said:
Is there a way to overpass the impossibility of VN.NET to accept the multi
heritage, that is to allow a class to inherit from TWO mother classes ?

If there was a often used need for multiple inheritance, then MS would have
probably found a way to include it. Evidently they decided there wasn't
an absolute need that would be common enough for the functionality.

Perhaps if you describe what you want to do, someone can explain how
to do that within the confines of the language you are using....

LFS
 
Bernard,


While Microsoft's .NET programming languages do not provide
multiple-inheritence they do provide ways to implement multiple-inheritance
like behavior.

There are other .NET programming languages that do support
multiple-inheritance:

Haskell.NET (HUGS98) is Haskell as a .NET programming language that allows
multiple-inheritance.

Eifel for .NET is another .NET programming language that allows
multiple-inhieritance:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/pdc_eiffel.asp


--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
 
Bernard Bourée said:
Can you tell me more about delegation or some links ?

Sample (untested):

\\\
Public Interface IB
Sub Goo()
End Interface

Public Interface IC
Sub Foo()
End Interface

Public Class A
Implements IB, IC

Private m_IB As IB
Private m_IC As IC

Public Sub New(ByVal IBImpl As IB, ByVal ICImpl As IC)
m_IB = IBImpl
m_IC = ICImpl
End Sub

Public Sub Goo() Implements IB.Goo
m_IB.Goo()
End Sub

Public Sub Foo() Implements IC.Foo
m_IC.Foo()
End Sub
End Class

Public Class B
Implements IB

...
End Class

Public Class C
Implements IC

...
End Class
..
..
..
Dim f As New A(New B(), New C())
///
 
Well if I try to make it simple

I have one class named FLUID which contains various properties like
Pressure
Temp
FlowRate

An other class named DROP which contains properties like
Diameter
Temp
Pressure

And a last one called SECTION which should contains the properties of FLUID,
DROP plus some others.
So the solution I have now is to make a copy of the FLUID's properties
inside SECTION

But If I have to change the properties of FLUID I have to remind to do it in
both places.

Thank for your help
 
Bernard,
In addition to the other comments, you could use one or more Interfaces:

Something like:
Public Interface IFluid
Property Pressure() As Integer
Property Temp() As Integer
Property FlowRate() As Integer
End Interface

Public Class Fluid
Implements IFluid

Private m_flowRate As Integer
Private m_pressure As Integer
Private m_temp As Integer

Public Property FlowRate() As Integer Implements IFluid.FlowRate
Get
Return m_flowRate
End Get
Set(ByVal value As Integer)
m_flowRate = value
End Set
End Property

Public Property Pressure() As Integer Implements IFluid.Pressure
Get
Return m_pressure
End Get
Set(ByVal value As Integer)
m_pressure = value
End Set
End Property

Public Property Temp() As Integer Implements IFluid.Temp
Get
Return m_temp
End Get
Set(ByVal value As Integer)
m_temp = value
End Set
End Property

End Class

Public Interface IDrop
Property Diameter() As Integer
Property Temp() As Integer
Property Pressure() As Integer
End Interface

Public Class Drop
Implements IDrop

Private m_diameter As Integer
Private m_pressure As Integer
Private m_temp As Integer

Public Property Diameter() As Integer Implements IDrop.Diameter
Get
Return m_diameter
End Get
Set(ByVal value As Integer)
m_diameter = value
End Set
End Property

Public Property Pressure() As Integer Implements IDrop.Pressure
Get
Return m_pressure
End Get
Set(ByVal value As Integer)
m_pressure = value
End Set
End Property

Public Property Temp() As Integer Implements IDrop.Temp
Get
Return m_temp
End Get
Set(ByVal value As Integer)
m_temp = value
End Set
End Property

End Class

Public Interface ISection
Inherits IFluid
Inherits IDrop
End Interface

Public Class Section
Implements ISection

Private m_flowRate As Integer
Private m_pressure As Integer
Private m_temp As Integer
Private m_diameter As Integer

Public Property FlowRate() As Integer Implements IFluid.FlowRate
Get
Return m_flowRate
End Get
Set(ByVal value As Integer)
m_flowRate = value
End Set
End Property

Public Property Pressure() As Integer Implements IFluid.Pressure,
IDrop.Pressure
Get
Return m_pressure
End Get
Set(ByVal value As Integer)
m_pressure = value
End Set
End Property

Public Property Temp() As Integer Implements IFluid.Temp, IDrop.Temp
Get
Return m_temp
End Get
Set(ByVal value As Integer)
m_temp = value
End Set
End Property

Public Property Diameter() As Integer Implements IDrop.Diameter
Get
Return m_diameter
End Get
Set(ByVal value As Integer)
m_diameter = value
End Set
End Property

End Class

Then rather then define your parameters as the class, you would define them
as the Interface.

For Example:

Public Shared Sub ProcessFluid(ByVal aFluid As IFluid)
Debug.WriteLine(CObj(aFluid).GetType().Name)
Debug.Indent()
Debug.WriteLine(aFluid.Pressure, "Fluid.Pressure")
Debug.WriteLine(aFluid.Temp, "Fluid.Temp")
Debug.WriteLine(aFluid.FlowRate, "Fluid.FlowRate")
Debug.Unindent()
End Sub

Public Shared Sub ProcessDrop(ByVal aDrop As IDrop)
Debug.WriteLine(CObj(aFluid).GetType().Name)
Debug.Indent()
Debug.WriteLine(aDrop.Diameter, "Drop.Diameter")
Debug.WriteLine(aDrop.Temp, "Drop.Diameter")
Debug.WriteLine(aDrop.Pressure, "Drop.Pressure")
Debug.Unindent()
End Sub


Public Shared Sub Main()
Dim aFluid As New Fluid
Dim aDrop As New Drop
Dim aSection As New Section
ProcessFluid(aFluid)
ProcessFluid(aSection)

ProcessDrop(aDrop)
ProcessDrop(aSection)
End Sub

Note you don't need to use three actual interface, you can get by with only
defining Fluid or Drop in terms on an interface, then Section could inherit
from the other & implement the interface. The key is to use the Interface
instead of the class when you want to accept a Section in addition to the
class... Section could also implement ISection by using delegation to actual
Fluid & Drop objects...

Hope this helps
Jay
 
C++ can do multiple inheritance, Managed C++ cannot.

Interesting --- the single inheritance thing is a "feature" of the CLR then?
 
Just curious but if you can inheirit two interfaces and both have a property
or method that has the same name, how can you tell which one to use?
 
Bernard Bourée said:
Well if I try to make it simple

I have one class named FLUID which contains various properties like
Pressure
Temp
FlowRate

An other class named DROP which contains properties like
Diameter
Temp
Pressure

And a last one called SECTION which should contains the properties of FLUID,
DROP plus some others.
So the solution I have now is to make a copy of the FLUID's properties
inside SECTION

But If I have to change the properties of FLUID I have to remind to do it in
both places.

What you list does not appear to be inheritance, but rather polymorphism.
Inheritance is involved when class Drop "is a" class Fluid, which may or
may not be inherited, that part wasn't clear (cute pun, eh?). But then you
have a class Section that has the properties of Fluid, and the properties of
Drop, and the properties of some others. That involves polymorphism.

If that is what you want then you need to use interfaces, like Jay suggests.

HTH
LFS
 
Dennis said:
Just curious but if you can inheirit two interfaces and both have a property
or method that has the same name, how can you tell which one to use?

How can _who_ tell which one to use? The user or the class developer?

When you implement a member of an interface, the member is marked:

Private Sub SomeMethod() Implements MyInterface.MyMethod

Because both the interface and method are named, the CLR won't get confused....

LFS
 
consider this aircode:

Public Interface IFoo
Sub ThisMethod() 'same signature
End Interface

Public Interface IFee
Sub ThisMethod() 'same signature
End Interface

Public Class Fum Implements IFoo Implements IFee
Public Sub ThisMethod() Implements IFoo.ThisMethod
'stuff
End Sub
Public Sub ThisMethod() Implements IFee.ThisMethod
'other stuff
End Sub
End Class

I didn't try to compile it (it's late) but each method implementation is
clearly marked as to whose interface it's implementing.
 
Dennis,
My earlier sample shows one way of implementing two interfaces with the same
method.

Another way is to explicitly name the methods in the implementing class,
when I do this, I make the methods private:

Something like:

Public Class Section
Implements ISection

Private m_flowRate As Integer
Private m_pressure As Integer
Private m_temp As Integer
Private m_diameter As Integer

Public Property FlowRate() As Integer Implements IFluid.FlowRate
Get
Return m_flowRate
End Get
Set(ByVal value As Integer)
m_flowRate = value
End Set
End Property

Public Property Pressure() As Integer Implements IFluid.Pressure,
IDrop.Pressure
Get
Return m_pressure
End Get
Set(ByVal value As Integer)
m_pressure = value
End Set
End Property

Public Property Temp() As Integer
Get
Return m_temp
End Get
Set(ByVal value As Integer)
m_temp = value
End Set
End Property

Private Property IFluid_Temp() As Integer Implements IDrop.Temp
Get
' do something unique for IFluid
Return m_temp
End Get
Set(ByVal value As Integer)
' do something unique for IFluid
m_temp = value
End Set
End Property

Private Property IDrop_Temp() As Integer IDrop.Temp
Get
' do something unique for IDrop
Return m_temp
End Get
Set(ByVal value As Integer)
' do something unique for IDrop
m_temp = value
End Set
End Property

Public Property Diameter() As Integer Implements IDrop.Diameter
Get
Return m_diameter
End Get
Set(ByVal value As Integer)
m_diameter = value
End Set
End Property

End Class
 
Jay

Thank you so much for your help !!!

--
Bernard Bourée
(e-mail address removed)
Jay B. Harlow said:
Bernard,
In addition to the other comments, you could use one or more Interfaces:

Something like:
Public Interface IFluid
Property Pressure() As Integer
Property Temp() As Integer
Property FlowRate() As Integer
End Interface

Public Class Fluid
Implements IFluid

Private m_flowRate As Integer
Private m_pressure As Integer
Private m_temp As Integer

Public Property FlowRate() As Integer Implements IFluid.FlowRate
Get
Return m_flowRate
End Get
Set(ByVal value As Integer)
m_flowRate = value
End Set
End Property

Public Property Pressure() As Integer Implements IFluid.Pressure
Get
Return m_pressure
End Get
Set(ByVal value As Integer)
m_pressure = value
End Set
End Property

Public Property Temp() As Integer Implements IFluid.Temp
Get
Return m_temp
End Get
Set(ByVal value As Integer)
m_temp = value
End Set
End Property

End Class

Public Interface IDrop
Property Diameter() As Integer
Property Temp() As Integer
Property Pressure() As Integer
End Interface

Public Class Drop
Implements IDrop

Private m_diameter As Integer
Private m_pressure As Integer
Private m_temp As Integer

Public Property Diameter() As Integer Implements IDrop.Diameter
Get
Return m_diameter
End Get
Set(ByVal value As Integer)
m_diameter = value
End Set
End Property

Public Property Pressure() As Integer Implements IDrop.Pressure
Get
Return m_pressure
End Get
Set(ByVal value As Integer)
m_pressure = value
End Set
End Property

Public Property Temp() As Integer Implements IDrop.Temp
Get
Return m_temp
End Get
Set(ByVal value As Integer)
m_temp = value
End Set
End Property

End Class

Public Interface ISection
Inherits IFluid
Inherits IDrop
End Interface

Public Class Section
Implements ISection

Private m_flowRate As Integer
Private m_pressure As Integer
Private m_temp As Integer
Private m_diameter As Integer

Public Property FlowRate() As Integer Implements IFluid.FlowRate
Get
Return m_flowRate
End Get
Set(ByVal value As Integer)
m_flowRate = value
End Set
End Property

Public Property Pressure() As Integer Implements IFluid.Pressure,
IDrop.Pressure
Get
Return m_pressure
End Get
Set(ByVal value As Integer)
m_pressure = value
End Set
End Property

Public Property Temp() As Integer Implements IFluid.Temp, IDrop.Temp
Get
Return m_temp
End Get
Set(ByVal value As Integer)
m_temp = value
End Set
End Property

Public Property Diameter() As Integer Implements IDrop.Diameter
Get
Return m_diameter
End Get
Set(ByVal value As Integer)
m_diameter = value
End Set
End Property

End Class

Then rather then define your parameters as the class, you would define them
as the Interface.

For Example:

Public Shared Sub ProcessFluid(ByVal aFluid As IFluid)
Debug.WriteLine(CObj(aFluid).GetType().Name)
Debug.Indent()
Debug.WriteLine(aFluid.Pressure, "Fluid.Pressure")
Debug.WriteLine(aFluid.Temp, "Fluid.Temp")
Debug.WriteLine(aFluid.FlowRate, "Fluid.FlowRate")
Debug.Unindent()
End Sub

Public Shared Sub ProcessDrop(ByVal aDrop As IDrop)
Debug.WriteLine(CObj(aFluid).GetType().Name)
Debug.Indent()
Debug.WriteLine(aDrop.Diameter, "Drop.Diameter")
Debug.WriteLine(aDrop.Temp, "Drop.Diameter")
Debug.WriteLine(aDrop.Pressure, "Drop.Pressure")
Debug.Unindent()
End Sub


Public Shared Sub Main()
Dim aFluid As New Fluid
Dim aDrop As New Drop
Dim aSection As New Section
ProcessFluid(aFluid)
ProcessFluid(aSection)

ProcessDrop(aDrop)
ProcessDrop(aSection)
End Sub

Note you don't need to use three actual interface, you can get by with only
defining Fluid or Drop in terms on an interface, then Section could inherit
from the other & implement the interface. The key is to use the Interface
instead of the class when you want to accept a Section in addition to the
class... Section could also implement ISection by using delegation to actual
Fluid & Drop objects...

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top