Multiple Inheritance C++ & C#

  • Thread starter Thread starter Andy Meyer
  • Start date Start date
A

Andy Meyer

Hi all,

I'm converting some C++ Controls to C# and there's one big thing, that
I can't solve:

class CControlEx
{
int nDescriptionID;
CString strDescription;

public:

//works with nDescriptionID & strDescription
void DoSomething();
};

with this class I create the following

class CComboBox2 : public CComboBox, CControlEx
{
...
};

class CEdit2 : public CEdit, CControlEx
{
...
};

....

The advantage is that I have to code the logic for DoSomething in all
of my derivations only once.

In C# I use interfaces and it works fine, but is there a workaround,
so that I don't have to code the same thing for DoSomething in all of
my classes?
Attributes and Reflection aren't a solution, because the values of
nDescriptionID and strDescription may change at runtime.

Thanks in advance
Andy
 
Andy,

As you have figured out, Multiple Inheritance (MI) is not supported in
..NET (it is supported in Eiffel, but it is not true MI, but rather, an
obfuscation based on interfaces).

To get around this, I would declare a base class of CControlEx (which
should be named ControlEx, the naming guidelines for .NET indicate that you
should not use C in front of class names to identify them as classes), and
then derive CComboBox2 from CControlEx. CControlEx would declare
nDescriptionId and strDescription, which would be marked as protected (or
have protected properties wrapping the private member names). Then,
DoSomething would access those members. Also, you might want to declare
DoSomething as virtual, which would allow you to have a base implementation,
but a custom one if needed in a derived class.

Hope this helps.
 
You could do something like Aggregation if that is an acceptable design in
your context

class CControlEx {}

class CComboBox2 : CComboBox
{

// Your CComboBox2 will not implement "DoSomething". There
are no interfaces either.
// If you appropriately expose the contents of CControlEx
then you can access them in this
// CComboBox2 class

private CControlEx myInnerControl;

public CControlEx CControlEx
{ get { return myInnerControl; } }
}

Now clients of CComboBox2 will have to do the following...

ClientOfCComboBox2
{
..........
CComboBox2 comboBox;

comboBox.CControlEx.DoSomething(); // Your class did not
implement DoSomething(). They forward it to the

// contained Control.

}

--------------------
From: (e-mail address removed) (Andy Meyer)
Newsgroups: microsoft.public.dotnet.languages.csharp
Subject: Multiple Inheritance C++ & C#
Date: 12 Nov 2003 06:50:23 -0800
Organization: http://groups.google.com
Lines: 41
Message-ID: <[email protected]>
NNTP-Posting-Host: 217.2.190.98
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1068648623 7925 127.0.0.1 (12 Nov 2003 14:50:23 GMT)
X-Complaints-To: (e-mail address removed)
NNTP-Posting-Date: Wed, 12 Nov 2003 14:50:23 +0000 (UTC)
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!news-out.cwix.com!newsfeed.cwix.co
m!news.maxwell.syr.edu!postnews1.google.com!not-for-mail
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:198690
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Hi all,

I'm converting some C++ Controls to C# and there's one big thing, that
I can't solve:

class CControlEx
{
int nDescriptionID;
CString strDescription;

public:

//works with nDescriptionID & strDescription
void DoSomething();
};

with this class I create the following

class CComboBox2 : public CComboBox, CControlEx
{
...
};

class CEdit2 : public CEdit, CControlEx
{
...
};

...

The advantage is that I have to code the logic for DoSomething in all
of my derivations only once.

In C# I use interfaces and it works fine, but is there a workaround,
so that I don't have to code the same thing for DoSomething in all of
my classes?
Attributes and Reflection aren't a solution, because the values of
nDescriptionID and strDescription may change at runtime.

Thanks in advance
Andy


Rakesh, EFT.

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
Back
Top