473,789 Members | 2,422 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
Anyway, thanks to all of you for the suggestions. This is as has been
mentioned, not an C# issue per se, but rather an reflection/CLR issue.
Perhaps I'll try in the CLR or the Interop (might surprise you, but
Interop is the closest thing I've come to finding a solution) groups.

For the project, it doesn't quite matter at this point, as a
formalization of the XML was required because of other factors. And
through that the necessary info can be extracted directly from the XML
and no multiple level base calls are necessary. I'm still very curious
of how what I described can be accomplished, but it's not a high
priority anymore.

Thanks again.

--Lucas

Nov 16 '05 #21
> I can't quite do that as the level at which you choose to deploy is
arbitrary.

Do you mean that the highest level at which you could deploy to the
Compact Framework could be one of the child classes? Well, then I guess
you have to make the "CompactFramewo rk" methods virtual, and trust the
inheriting child classes to override them only when they can, in fact,
deploy to the compact framework.
There can be several components (classes) in the same line of inheritence that need to be deployed.

OK, now I'm confused again. I thought you said that along a single line
of inheritance, there was some base class that you were trying to find,
which was the one for which you wanted to call ToXml(). Now you're
saying that you may want to call ToXml() for several of the classes up
the hierarchy? Why would you do that when the highest-level one will
include the XML for all of the lower-level ones, and supposedly you're
deploying the most sophisticated control you can to the compact device?
Obviously I don't fully understand what you're trying to achieve. :(
And in addition, it isn't an aesthetically pleasing solution,
Aesthetics is in the eye of the beholder. :) Me, I find messing around
in IL and using Reflection to play tricks aesthetically ugly. :)

I would much prefer to make the situation explicit in my interface:
this class can export to the Compact Framework / this class can't and
instead defers to its base class. In essence I see what you're trying
to do is to load two jobs into one method: Emit XML for the regular
framework and emit XML to deploy to the compact framework, and decide
which to do based on some funky Reflection / IL mashing... or am I not
getting what you're trying to do?
as the XML method would have to be implemented twice in the deployable class.

Implemented? No, not at all. More like declared. The CompactFramewor k
versions would never have any more of a body that simply a call to the
corresponding "regular framework" version. Classes that can deploy to
the CF contain an overload. Those that can't, don't. The net effect is
that calling the CompactFramewor k version of the method will "search up
the hierarchy" until it finds the first class that can deploy to the
framework, and use that class's version of the method, which will
simply call that class's ToXml(). If that's not exactly what you want
to do, then please help me understand better.
Emiting IL is more complicated, but this I can do on my side, sparing


those that write the components to write redundant code.

Nobody has to write redundant code, unless you count three lines of
code in each class that can be deployed to the framework as being
"redundant" .

One thing I certainly don't understand is how, if you're going to take
care of this "automagica lly" under the covers, are you going to figure
out which of the derived classes can be deployed to the framework (and
thus their ToXml() should be used) and which can't (and so you have to
keep searching up the hierarchy)?

Nov 16 '05 #22
lu**********@ho tmail.com <lu**********@h otmail.com> wrote:
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.


Only the classes which override the method would need to call base.Foo.
It's not that hard to do, and it would be very easy to make the top-
level base class keep a flag to check whether or not it had been done
properly (as you could check that *all* methods had been called simply
by knowing whether or not the top method had been called). You could
test the flag after calling the method.
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.


Then choose a different language, basically.

--
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 #23


using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;

namespace Testing_Samples
{
public class Form2 : System.Windows. Forms.Form
{
private System.Componen tModel.Containe r components = null;
public Form2()
{
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
private void InitializeCompo nent()
{
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 273);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHan dler(this.Form2 _Load);

}
private void Form2_Load(obje ct sender, System.EventArg s e)
{
C a = new C();
//Invoking Class C Method
MessageBox.Show (a.PrintMe());
//Casting with Class A Method
MessageBox.Show (((A)a).PrintMe ());
}
}
class A
{
public string PrintMe()
{
return "Original A";
}
}

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

class C : B
{

public string PrintMe()
{
return "Overriden C";
}
}
}
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #24
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;

namespace Testing_Samples
{
public class Form2 : System.Windows. Forms.Form
{
private System.Componen tModel.Containe r components = null;
public Form2()
{
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
private void InitializeCompo nent()
{
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 273);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHan dler(this.Form2 _Load);

}
private void Form2_Load(obje ct sender, System.EventArg s e)
{
C a = new C();
//Invoking Class C Method
MessageBox.Show (a.PrintMe());
//Casting with Class A Method
MessageBox.Show (((A)a).PrintMe ());
}
}
class A
{
public string PrintMe()
{
return "Original A";
}
}

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

class C : B
{

public string PrintMe()
{
return "Overriden C";
}
}
}
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #25
Hmmm... code with no explanation, and it's code that doesn't even
compile!

I understand the implication: that declaring PrintMe() in classes A, B,
and C will cause "Original A" to print out if you cast a reference of
type "C" to type "A". I assume that you meant to say that:

C a = new C();
//Invoking Class C Method
MessageBox.Show (a.PrintMe());
//Casting with Class A Method
MessageBox.Show (((A)a).PrintMe ());

will display two message boxes: one that says, "Overridden C" and the
next that says, "Original A". Unfortunately, if you had tried to
compile this code, you would have found that it wouldn't, because you
must make A's PrintMe "virtual", and specify either "override" or "new"
on B and C's PrintMe.

The behaviour you are claiming will happen only if you declare the
PrintMe methods in B and C to be "new". "new," however, isn't an
override, so you won't get the polymorphic behaviour that the OP wants.

If you were to specify "override," you would get the behaviour that the
OP wants, but both message boxes would display "Overridden C." That, in
a nutshell, was the OP's problem: how to have both message boxes
display "Overridden C," but then go back to call the base method
("Original A") on demand.

Unfortunately, no can do (in C#).

Nov 16 '05 #26

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
10404
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...
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...
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...
1
7525
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
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?
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
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.