473,385 Members | 2,044 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,385 software developers and data experts.

Multiple Inheritance C++ & C#

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
Nov 15 '05 #1
2 1735
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andy Meyer" <am****@yahoo.de> wrote in message
news:6e*************************@posting.google.co m...
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

Nov 15 '05 #2
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: am****@yahoo.de (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: <6e*************************@posting.google.com>
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: gr**********@google.com
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-mailXref: 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

Nov 15 '05 #3

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

Similar topics

2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
8
by: Shawn Casey | last post by:
Consider the following code: interface IBase { virtual void BaseFunction() = 0; }; interface IDerived : public IBase { virtual void DerivedFunction() = 0;
11
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know...
5
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
7
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...

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.