473,769 Members | 7,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple base classes in .NET

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 support multiple inheritance. In
C++ for instance I noticed that the compiler flags an error if you use the
"ref" keyword on a class with multiple base classes. This supports the above
quote. However, under the "CodeClass2.Bas es" property (part the VS
extensibility model), it states that:

"Bases are super types of CodeElements. For Visual Basic and Visual C#
there is always only one element in the collection except when the code type
is a CodeInterface".

This is true of course since these languages only support single (class)
inheritance. However, it should be true for all .NET classes based on the
first quote above. My issue is therefore this. I want to retrieve the base
class of an arbitrary class in an arbitrary code file by invoking
"CodeClass2.Bas es.Item(1)". This works in my testing but will it always work
for all languages in theory, assuming the class I'm targetting is always a
..NET class of course. Thanks in advance.
Jul 13 '07
47 4034
Am Sat, 14 Jul 2007 09:59:39 -0700 schrieb Peter Duniho:
IMHO, C# already does make an improvement over multiple inheritance vs
C++. That is, a class can in fact inherit multiple interfaces.
A class can implement multiple interfaces - roughly the same as in C++
(minor differences ignored). But a class in C# cannot use implementation
inheritance, which can be very useful. You have to duplicate the code, use
delegation etc.
>This
allows for the same basic behavior as multiple inheritance, while forcing
the programmer to be explicit about how the class is arranged (ambiguity
in behavior of base classes shared by multiply inherited classes being one
of the bigger stumbling blocks for multiple inheritance in C++, IMHO).
No, disagree. The same ambiguity problems can arise when you implement
multiple interfaces in a C#-class. The solution is the same, though:
Compiler gives notice, and you have to manually resolve the amiguity,
usually by qualifying the name. No problem.

Paule
Jul 18 '07 #11
[Please do not mail me a copy of your followup]

Paul Werkowitz <ne********@pri maprogramm.desp ake the secret code
<1f************ *************** ***@40tude.nett husly:
>Am Sat, 14 Jul 2007 16:37:06 +0100 schrieb Mark Rae [MVP]:
>>
Indeed. I've been using C# since the latter half of 2002 and have never had
any need for multiple inheritance...

Hello, how then can you advise me to implement the following:

I have derivations from the standard WinForm Controls that implement
certain protocols for loading, storing, verification etc. Code example:

//-----------------------------------------------------------------
// boBindName
//
[
Bindable (true)
, Category ("QFC")
, Description ("Feld/Property, an das gebunden werden soll")
]
public string boBindName
{
get { return mBoBindName; }
set { mBoBindName = value; }
}

I have many of those.

At the moment, I need to have the exact same code in all of my derived
controls. Any change in that code must be manually repeated for all
controls - a perfect situation for implementation inheritance with the help
of MI.
Personally, I'd question any design that is encouraging you to
subclass every .NET WinForms Control.

MI is only one way to aggregate behaviors, not the only way. In .NET
you can implement multiple interfaces to aggregate multiple behaviors
into a single class. In .NET 2 you also have generics, so you can do
template type tricks:

class BindableControl <T: T
{
[Bindable(true)]
[Category("QFC")]
[Description(".. .")]
public string boBindName
{
get { return mBoBindName; }
set { mBoBindName = value; }
}
private string mBoBindName;
}

This eliminates the duplication, but it still is requiring you to
subclass every .NET control. I'd consider other design alternatives
that wouldn't require you to do this.
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://www.xmission.co m/~legalize/book/download/index.html>

Legalize Adulthood! <http://blogs.xmission. com/legalize/>
Jul 18 '07 #12
On Wed, 18 Jul 2007 01:41:56 -0700, Paul Werkowitz
<ne********@pri maprogramm.dewr ote:
A class can implement multiple interfaces - roughly the same as in C++
(minor differences ignored). But a class in C# cannot use implementation
inheritance, which can be very useful. You have to duplicate the code,
use
delegation etc.
I prefer to use the term "compositio n". But whatever. Whether you call
it "delegation " or "compositio n", it works fine.
>This
allows for the same basic behavior as multiple inheritance, while
forcing
the programmer to be explicit about how the class is arranged (ambiguity
in behavior of base classes shared by multiply inherited classes being
one
of the bigger stumbling blocks for multiple inheritance in C++, IMHO).

No, disagree.
Then you do so incorrectly.
The same ambiguity problems can arise when you implement
multiple interfaces in a C#-class.
If you think "the same ambiguity problems can arise", then you don't
understand which ambiguity problems I'm talking about.

I am specifically speaking about the problems that arise when a single
class implicitly inherits the same implementation from the same base class
multiple times. This simply does not happen in .NET, because you can only
inherit implementation from any given class once.

In any case, I do not intend to rehash to ridiculous thread that already
occurred in the C# newsgroup about MI. If you think that the lack of MI
in .NET and/or C# is a fundamental flaw, then don't use .NET and/or C#.
Simple as that. If you don't believe it's a fundamental flaw, then get
over it and quit wasting time complaining about the lack of it. There are
workarounds that work fine for people who can get over their love affair
with MI.

The basic behaviors that MI allow still exist in .NET. They just require
writing a little more code, while at the same time avoiding some of the
pitfalls that true MI creates.

Pete
Jul 18 '07 #13
"Mark Rae [MVP]" <ma**@markNOSPA Mrae.netwrote
"Larry Smith" <no_spam@_nospa m.comwrote in message
[Multiple Inheritence]
>Moreover, it's rarely even used in the C++ world.
That's just... not right.

The entire STL is full of multiple inheritence. Every C++ programmer's first
program uses it, even if they're not aware of it (IOStream).

It's very frequently used, and is a very powerfull tool to have access to.
>From my own (long) experience in that arena, it makes sense conceptually
but in practice it's mechanically very difficult to work with. I doubt
significant improvements can be made on this front.
My experience is otherwise. I've used it alot, loved it, had great success
with it, and really miss it.
Indeed. I've been using C# since the latter half of 2002 and have never
had any need for multiple inheritance...
I've been using the C# since 2001 (neh! heheh. ) and have had many, many
cases where multiple inheritence would have been the correct answer. Without
MI, I've been stuck jumping through crazy Interface hoops, duplicating all
sorts of implementation logic, and generally been forced to take a very
powerfull tool out of my bag of tricks.

I know there are no changes planned on this front, and as a result, I'm
beating a dead horse, but I find it quite frustrating. I continually hear
people say, "I don't use it, have never used it, and will never use it -
therefore it's not needed.".

--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
Jul 18 '07 #14
My experience is otherwise. I've used it alot, loved it, had great success
with it, and really miss it.
I agree (cannot say how much!)
Jul 18 '07 #15
>>Moreover, it's rarely even used in the C++ world.
>
That's just... not right.
Actually it is. I've worked for many different companies both large and
small and almost nobody uses it. I find it very hard to believe this isn't
the norm.
The entire STL is full of multiple inheritence. Every C++ programmer's
first program uses it, even if they're not aware of it (IOStream).
Actually it's not full of MI. There's precious little of it in fact. The
streams library is really all that there is and its MI model is very simple.
If something else exists that uses MI then it escapes me right now as I've
been immersed in C# for the last 18 months now (after many years in C/C++).
There's nevertheless a big difference between using the STL classes and
applying MI in your own code. You normally don't get your hands dirty with
it at this level. In fact, from a programmer's perspective it really
functions as SI for all intents and purposes. That is, you normally don't
deal with the complicatd issues that can make MI difficult to deal with.
It's very frequently used, and is a very powerfull tool to have access to.
I didn't say it wasn't powerful. The mechanics get progressively more
difficult to work with however as class hiearchies become non-trivial.
>
>>From my own (long) experience in that arena, it makes sense conceptually
but in practice it's mechanically very difficult to work with. I doubt
significant improvements can be made on this front.

My experience is otherwise. I've used it alot, loved it, had great success
with it, and really miss it.
You must have precious little experience dealing with large class
hierarchies then.. When you factor in issues like virtual base classes, the
calling of their constructors from the most dervied class (which refutes
your claim that MI is widely used since very few C++ programmers even know
about this), the headaches involved with ambiguity (a major pain), etc.,
then it becomes a major deterrent very quickly unless your class hierarchies
remain trivial.
>Indeed. I've been using C# since the latter half of 2002 and have never
had any need for multiple inheritance...

I've been using the C# since 2001 (neh! heheh. ) and have had many, many
cases where multiple inheritence would have been the correct answer.
Without MI, I've been stuck jumping through crazy Interface hoops,
duplicating all sorts of implementation logic, and generally been forced
to take a very powerfull tool out of my bag of tricks.

I know there are no changes planned on this front, and as a result, I'm
beating a dead horse, but I find it quite frustrating. I continually hear
people say, "I don't use it, have never used it, and will never use it -
therefore it's not needed.".
The lack of MI has caused problems for me as well on occasion. You don't
need to duplicate interface logic however but that's another story. In fact.
I firmly believe that MI is conceptually superior to C# interfaces since
it's a more natural model of objects in the real world. It's mechanically
difficult (even challenging) to work with however once you start
encountering the problems in my previous point. This is my only real beef
with it.
Jul 19 '07 #16
[Please do not mail me a copy of your followup]

"Larry Smith" <no_spam@_nospa m.comspake the secret code
<uQ************ *@TK2MSFTNGP06. phx.gblthusly:
>Actually it's not full of MI. There's precious little of it in fact. The
streams library is really all that there is and its MI model is very simple.
One library I've seen that uses MI all over the place is ATL, but in
that case the MI is used to aggregate various bits of COM behavior.
>You must have precious little experience dealing with large class
hierarchies then.. When you factor in issues like virtual base classes, the
calling of their constructors from the most dervied class (which refutes
your claim that MI is widely used since very few C++ programmers even know
about this), the headaches involved with ambiguity (a major pain), etc.,
then it becomes a major deterrent very quickly unless your class hierarchies
remain trivial.
On the other hand, ATL does all its business with template tricks and
doesn't use the virtual function call mechanism very much, if at all.
(I can't recall right now if it uses any virtual functions or not, but
my recollection is that I don't recall any virtual function business
going on when I last studied the ATL Internals book.)
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://www.xmission.co m/~legalize/book/download/index.html>

Legalize Adulthood! <http://blogs.xmission. com/legalize/>
Jul 19 '07 #17
"Larry Smith" <no_spam@_nospa m.comspake the secret code
<uQ************ *@TK2MSFTNGP06. phx.gblthusly:
>>Actually it's not full of MI. There's precious little of it in fact. The
streams library is really all that there is and its MI model is very
simple.

One library I've seen that uses MI all over the place is ATL, but in
that case the MI is used to aggregate various bits of COM behavior.
>>You must have precious little experience dealing with large class
hierarchies then.. When you factor in issues like virtual base classes,
the
calling of their constructors from the most dervied class (which refutes
your claim that MI is widely used since very few C++ programmers even know
about this), the headaches involved with ambiguity (a major pain), etc.,
then it becomes a major deterrent very quickly unless your class
hierarchies
remain trivial.

On the other hand, ATL does all its business with template tricks and
doesn't use the virtual function call mechanism very much, if at all.
(I can't recall right now if it uses any virtual functions or not, but
my recollection is that I don't recall any virtual function business
going on when I last studied the ATL Internals book.)
I'm very familiar with this book having read it in nauseating detail once
upon a time (it's really the bible of ATL). ATL is brutally difficult
however. It took me a long time to master it and I have many years of C++
experience. For the less experienced it's downright dangerous. Hardly a good
example of MI nor many other techniques (for C++ experts only).
Jul 19 '07 #18
[Please do not mail me a copy of your followup]

"Larry Smith" <no_spam@_nospa m.comspake the secret code
<e9************ **@TK2MSFTNGP04 .phx.gblthusly:
>[...] Hardly a good
example of MI nor many other techniques (for C++ experts only).
Its just the only library I can think of that uses multiple
inheritance heavily.

Other than that, I can't think of a typical Windows library that uses
multiple inheritance heavily like that.
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://www.xmission.co m/~legalize/book/download/index.html>

Legalize Adulthood! <http://blogs.xmission. com/legalize/>
Jul 19 '07 #19
MI is only one way to aggregate behaviors, not the only way. In .NET
you can implement multiple interfaces to aggregate multiple behaviors
into a single class. In .NET 2 you also have generics, so you can do
template type tricks:
Sorry, but generics aren't templates, and you can't "do template type
tricks".
>
class BindableControl <T: T
For instance, that line won't work with generics.
{
[Bindable(true)]
[Category("QFC")]
[Description(".. .")]
public string boBindName
{
get { return mBoBindName; }
set { mBoBindName = value; }
}
private string mBoBindName;
}

This eliminates the duplication, but it still is requiring you to

Except it doesn't work.
Jul 19 '07 #20

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

Similar topics

6
6487
by: Stuart Golodetz | last post by:
Hi, I've got a minor casting issue which I need to check about (marked // <--). I was trying to do a static_cast on it (which didn't work, though I'm not sure why this should be the case?) I also tried reinterpret_cast (which is clearly an exceedingly dodgy thing to do; it worked, but I'm not sure whether it should have worked, or whether (the more likely scenario) it was just coincidence?) Finally, after a bit of trawling through the...
6
2840
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base { };
20
10084
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in advance for enlightment ... here's the snippet #!/usr/bin/python
22
23384
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 examples?
5
3454
by: Scott | last post by:
Hi All, Am I correct in assuming that there is no way to have a base pointer to an object that uses multiple inheritance? For example, class A { /* ... */ }; class B { /* ... */ };
2
1869
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my scenario... I have 5 Derived Classes I have 3 Base Classes
3
2554
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is used, then "the responsibility for initializing a virtual base is borne by the most derived class in the hierarchy". What does it mean? Initializing base class is usually done automatically by the compiler, but a derived class can invoke the base...
13
2502
by: stephenpas | last post by:
We are trying to monkey-patch a third-party library that mixes new and old-style classes with multiple inheritance. In so doing we have uncovered some unexpected behaviour: <quote> class Foo: pass class Bar(object): pass
2
2099
by: Immortal Nephi | last post by:
You may have heard diamond shape. You create one base class. One base class has member functions and member variables. You create two derived classes. All member functions and member variables from one base class are inherited into two derived classes. You want both derived classes to share member variables of the one base class. You can do this way so you don't need keyword -- friend. You can add virtual public One_Base_Class on...
0
9589
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
9423
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
10216
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...
1
9997
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
7413
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
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
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.