473,789 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple level base calls

I'm having some serious trouble accessing a virtual method of a base
class - that is not the immidate base class.

This is the basic situation that I have:

=============== =============== =============== ==============
class A
{
public virtual string PrintMe()
{
return "Original A";
}
}

class B : A
{
public override string PrintMe()
{
return "Overriden B";
}
}

class C : B
{

public override string PrintMe()
{
return "Overriden C";
}

}
=============== =============== =============== ==============

Now, what I'm trying to do is to from an instance of C call A's
PrintMe() method. I've tried various delegate twists, I've tried
reflection - without any luck. It insists on returning the top of the
virtual call table - C's PrintMe().

Any ideas or suggestions?

thanks
--Lucas

Nov 16 '05
25 3811
RCS
Just because you can, doesn't mean you should.

<lu**********@h otmail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Well, the bottom line is that you *can't* do what you originally

asked.
You can't call a grandparent method if it's overridden in the parent.


Well, that's not quite true either. I have no problem doing it through
IL, so as far as the runtime environment goes, it's no problem. It's c#
that's blocking me.

Nov 16 '05 #11
I can't use new, because I'm not in control of writing the children
clasess, and forgetting to use new would have nasty consequences.

To explain a bit more, suppose you have these classes:

Atom - contains basic from/to XML serialization functions
Adder:Atom - a class that adds two numbers
AdderGui: Adder - graphical version of Adder, in windows forms
AdderNiceGui: AdderGui, with surround sound, 3d blah blah - i.e a whole
bunch of things

Each class sets/gets XML to store its current state. This is done by
overriding the ToXml and FromXml methods, that have originally been
defined in Atom. The XML works by iteratively encapsulating the lower
levels. So for instance, the Adder XML might look like this (produced
by it's ToXml() ):

<adder class=Adder>
<anum>1</anum>
<bnum>2</bnum>
</adder>

The AdderGui might then look like this:

<addergui class=AdderGui>
<window size=100></>
<base class=Adder>
<adder class=Adder>
<anum>1</anum>
<bnum>2</bnum>
</adder>
</base>
</adderGui>

The AdderNiceGui might look something like this:

<addernicegui class=AdderNice Gui>
<sound mode=surround></sound>
<gfx mode=d3d></gfx>
<base class=AdderGui>
<addergui class=AdderGui>
<window size=100></>
<base class=Adder>
<adder class=Adder>
<anum>1</anum>
<bnum>2</bnum>
</adder>
</base>
</adderGui>
</base>
</addernicegui>

Ok?

Now the user will during run-time use the AdderNiceGui and AdderGui
classes. Once he is done configuring them, he wants to deploy them to
..NET compact. Only the pure Adder class is compact framework
compatible, so I need from the AdderNiceGui class, extract the Adder
class, for deployment. No problems there. Now we want the XML for just
the Adder class - which we get from Adder's ToXml(). And this is where
the problem comes in that from AdderNiceGui, there seems to be no
(civilized) way of getting through a call to Adder's ToXml.

There are a number of design restrictions - such as easy implementation
of new classes as well as that the individual class XML can be whatever
(in short, you can't trust to extract any structural info from the
XML). Each class is responsible for getting the XML from its baseclass
and incorporating it to its XML etc

That's the practical context of it.

Nov 16 '05 #12
lu**********@ho tmail.com <lu**********@h otmail.com> wrote:
Well, the bottom line is that you *can't* do what you originally

asked.
You can't call a grandparent method if it's overridden in the parent.


Well, that's not quite true either. I have no problem doing it through
IL, so as far as the runtime environment goes, it's no problem. It's c#
that's blocking me.


You can't do it in C#. Your question is in a C# newsgroup, so I think
it's reasonable to assume you wanted a C# solution.

(There's the matter of "should" as well, where I'd say that you
shouldn't do this even if you could.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13
lu**********@ho tmail.com <lu**********@h otmail.com> wrote:
Thanks, but I'm aware of the basic language limitations. Unfortunately,
as I said in the original post, I can't use new. And base doesn't cut
it as it only goes one level and I can't get to it from outside.


Base only goes one level, but if every method calls base, then you end
up getting all the way up the tree.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #14
RCS
It sounds like you just want to use classes that are already in (and named
different names) - from your base classes. Is this correct?

If so, you just call them - the runtime "figured out" that you mean call it
from a base class if another version doesn't exist higher up..

In other words, if you have BaseTest.Format It - then you inherit from that
and create BaseTestTwo.For matItBetter and you inherit from BaseTestTwo and
create BaseTestThree and and have FormatItBest..

From BaseTestThree, you have access to all of those methods:

BaseTestThree objBTT = new BaseTestThree() ;
objBTT.FormatIt ();
objBTT.FormatIt Better();
objBTT.FormatIt Best();

Those are all valid. Is that what you are trying to do??
<lu**********@h otmail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I can't use new, because I'm not in control of writing the children
clasess, and forgetting to use new would have nasty consequences.

To explain a bit more, suppose you have these classes:

Atom - contains basic from/to XML serialization functions
Adder:Atom - a class that adds two numbers
AdderGui: Adder - graphical version of Adder, in windows forms
AdderNiceGui: AdderGui, with surround sound, 3d blah blah - i.e a whole
bunch of things

Each class sets/gets XML to store its current state. This is done by
overriding the ToXml and FromXml methods, that have originally been
defined in Atom. The XML works by iteratively encapsulating the lower
levels. So for instance, the Adder XML might look like this (produced
by it's ToXml() ):

<adder class=Adder>
<anum>1</anum>
<bnum>2</bnum>
</adder>

The AdderGui might then look like this:

<addergui class=AdderGui>
<window size=100></>
<base class=Adder>
<adder class=Adder>
<anum>1</anum>
<bnum>2</bnum>
</adder>
</base>
</adderGui>

The AdderNiceGui might look something like this:

<addernicegui class=AdderNice Gui>
<sound mode=surround></sound>
<gfx mode=d3d></gfx>
<base class=AdderGui>
<addergui class=AdderGui>
<window size=100></>
<base class=Adder>
<adder class=Adder>
<anum>1</anum>
<bnum>2</bnum>
</adder>
</base>
</adderGui>
</base>
</addernicegui>

Ok?

Now the user will during run-time use the AdderNiceGui and AdderGui
classes. Once he is done configuring them, he wants to deploy them to
.NET compact. Only the pure Adder class is compact framework
compatible, so I need from the AdderNiceGui class, extract the Adder
class, for deployment. No problems there. Now we want the XML for just
the Adder class - which we get from Adder's ToXml(). And this is where
the problem comes in that from AdderNiceGui, there seems to be no
(civilized) way of getting through a call to Adder's ToXml.

There are a number of design restrictions - such as easy implementation
of new classes as well as that the individual class XML can be whatever
(in short, you can't trust to extract any structural info from the
XML). Each class is responsible for getting the XML from its baseclass
and incorporating it to its XML etc

That's the practical context of it.

Nov 16 '05 #15
Rather than doing tricky things with Reflection and IL, why not just do
this:

Adder declares not two, but _four_ public methods:

public virtual ... FromXml(...) {...}

public virtual ... ToXml(...) {...}

which is what you have (and have overridden in the various child
classes) and

public ... FromCompactComp atibleXml(...)
{
return FromXml(...);
}

public ... ToCompactCompat ibleXml(...)
{
return ToXml(...);
}

which emits XML that is compatible with the Compact Framework version
of the class, and is not virtual, so cannot be overridden.

Child classes override and embellish ToXml and FromXml, but when it's
time to deploy to the Compact Framework, you call the CompactCompatib le
methods, which are inherited directly from the base class.

In effect, you give alternate names to FromXml() and ToXml() in the
base class, and forbid inheriting classes from overriding the meaning
of those names, so that you can always get at them from any child class.

Nov 16 '05 #16
>In effect, you give alternate names to FromXml() and ToXml() in the
base class, and forbid inheriting classes from overriding the meaning
of those names, so that you can always get at them from any child

class.

I can't quite do that as the level at which you choose to deploy is
arbitrary. There can be several components (classes) in the same line
of inheritence that need to be deployed.

And in addition, it isn't an aesthetically pleasing solution, as the
XML method would have to be implemented twice in the deployable class.
Emiting IL is more complicated, but this I can do on my side, sparing
those that write the components to write redundant code.

Nov 16 '05 #17
>Base only goes one level, but if every method calls base, then you end
up getting all the way up the tree.


Nah, base is hard-wired at compile time to the actuall base class where
it is defined (not too strange). For it to work, each child-class would
have to actually implement the base call, resulting in unnecessary and
redundant code that could easily introduce errors throughout the entire
inheritance chain.

Thanks for your suggestions, they are appreciated, but I'm really not
looking for a workaround to the problem. What I need help with is how
to accomplish it, through reflection, without resorting to IL.

Nov 16 '05 #18
>Those are all valid. Is that what you are trying to do??

Nope, can't do that either. The only thing I know about the classes
comes from a simple interface (that specifies To/From Xml methods).
Their assemblies are dynamically loaded (plugins) and I have no
knowledge of them at compile time - apart from that they implement a
number of interfaces.

Nov 16 '05 #19
>You can't do it in C#. Your question is in a C# newsgroup, so I think
it's reasonable to assume you wanted a C# solution.


Fair point, I probably should have posted it in one of the more
specific .NET framework groups, since it's more about reflection than
about C#. The problem is that this quite doable from for instance
managed C++, so my problem is C# specific.

Nov 16 '05 #20

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

Similar topics

2
4339
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
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...
11
8106
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 how and why it works. I'll post a simplified version of it below and ask my questions afterwards: class Base { void *function_ptr;
32
14902
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
2
1514
by: Emmanuel | last post by:
Hi, I'm working on a c# web app and require having some code which runs in the page Load event of each page and to be reusable in other web apps. So i decided to use a Class Library which contains a class that inherits from the System.Web.UI.Page. the class contains an override of the OnLoad event in which the common code is executed.
3
1702
by: Mark P | last post by:
#include <iostream> using namespace std; struct Base { virtual int foo () {return 1;} virtual int foo (int i) {return 2;} virtual ~Base () {} };
60
4944
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 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
11
2250
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern to illustrate this. Problem: I need to expose two lists of objects from a high-level class. I would like to expose these lists as read-only, but require write access internally.
4
1883
by: junyang | last post by:
Hi all, I have one DTD fragment, base.dtd, that contains a bunch of useful element definitions (say an element named "base"), and two DTD fragments, a.dtd and b.dtd, that each build on base.dtd and defines a few more elements. I use an entity to include base.dtd in a.dtd and b.dtd. For example, in a.dtd: <!ENTITY % include.base SYSTEM "base.dtd"> %include.base
0
9663
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
9511
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
10195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
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,...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
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

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.