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 25 3810
Why do you want to do this? Maybe there is another way to get the
effect you want, without having to call up the class hierarchy like
that.
Basically it's about deploying components on a selectable level. The
whole story is a bit complicated, but basically the story is that each
class has its own to/from XML methods that store various properties and
settings that belong to the class.
The basic idea is that there are base classes, which are .NET Compact
framework compatible while the user works with classes that inherit
them and have extensive GUI etc. When the system is deployed, we need
to get the XML of the base classes..
Anyway, the whole thing is too complicated to briefly cover here -
there's a bunch of restrictions on how it can be done. Bottom line is
that I really need to do it the way I described it.
How about using base.PrintMe from B? Have a protected method in B that
calls base.PrintMe() and call that method from C.
using System;
class A
{
public virtual void PrintMe() { Console.WriteLi ne("A"); }
}
class B : A
{
public override void PrintMe() { Console.WriteLi ne("B"); }
protected void BasePrintMe() { base.PrintMe(); }
}
class C : B
{
public override void PrintMe() { BasePrintMe(); }
}
class T
{
public static void Main()
{
A a = new C();
a.PrintMe();
}
}
Regards
Senthil
Lucas... The language does support:
1) Use new instead of override which destroys polymorphic behaviour but
allows access to the base method through a reference variable of the
base type.
2) In the subclass method you can call super.PrintMe()
class Class2 : Class1
{
public override string Test()
{
return base.Test()+"Cl ass2";
}
}
Regards,
Jeff 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().<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it! lu**********@ho tmail.com <lu**********@h otmail.com> wrote: Basically it's about deploying components on a selectable level. The whole story is a bit complicated, but basically the story is that each class has its own to/from XML methods that store various properties and settings that belong to the class.
The basic idea is that there are base classes, which are .NET Compact framework compatible while the user works with classes that inherit them and have extensive GUI etc. When the system is deployed, we need to get the XML of the base classes..
Anyway, the whole thing is too complicated to briefly cover here - there's a bunch of restrictions on how it can be done. Bottom line is that I really need to do it the way I described it.
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.
However, if you make each method call the base method, making them pick
and choose what they react to, it should work fine.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Unfortunately that's not an option as I don't know beforehand how long
the chain is. I need to do this with an arbitrary amount of inheretence
steps.
> 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.
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.
What I'm trying to do is get around it through reflection, but without
resorting to have to emit in IL what I need. I don't see why the
runtime constantly refuses me access to any level of the virtual call
table. I always end up at the top, with the last overriden class. The
furthest I have come is to get an actual function pointer (IntPtr) to
the method I want, but when I use it through
RuntimeMethodHa ndle->MethodHandle , I again end up at the top of the
virtual table, with a call to the last method in the virtual table.
--Lucas
"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.
What I'm trying to do is get around it through reflection, but without resorting to have to emit in IL what I need. I don't see why the runtime constantly refuses me access to any level of the virtual call table. I always end up at the top, with the last overriden class. The furthest I have come is to get an actual function pointer (IntPtr) to the method I want, but when I use it through RuntimeMethodHa ndle->MethodHandle , I again end up at the top of the virtual table, with a call to the last method in the virtual table.
--Lucas
it's restricted because that's just how polymorphism suppose to work.
and why can't you use new? from what you described, sounds to me like you
want to hide the base method, not override it. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Graham Banks |
last post by:
Does using multiple inheritance introduce any more performance overhead than
single inheritance?
|
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...
|
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;
|
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) ||
|
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.
| |
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 () {}
};
|
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
|
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.
|
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
|
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...
|
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...
| |
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |