473,703 Members | 2,970 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with virtual method

If I have a class with a virtual method, and a child class that overrides
the virtual method, and then I create an instance of the child class AS A
base class...

BaseClass bc = new ChildClass();

.... and then call the virtual method, why is it that the base class's method
is called instead of the overridden method? How do I fix this if I don't
know at runtime what the child class is? I'm using
Activator.Creat eInstance() to load the child class from a satellite DLL, a
third-party plug-in that inherits my base class, so I therefore cannot
declare the child class type in the code or at compile time.

Thanks,
Jon
Nov 15 '05 #1
28 5215
But did you try it with a satellite DLL instantiated with
Activator.Creat eInstance()?

Otherwise we're just proving the difference between an overridden virtual
method and the "new" keyword, that overriding works.

Honest to God, though, my child class in an another DLL is overriding my
virtual method, and the base virtual method is being called. I'm stepping
through the code, and the base class comes up in the code window on that
method. It is definitely marked as "virtual". The child class is definitely
a child (public class MyChild : MyBase), and the method definitely has the
"override" keyword.

This has had me stumped for at least a week, and I CANNOT afford to lose
another day on this project!! I MUST move on! But this plug-in feature is
one of the core highlights of the product.

What the hell is going on?? :( :(

Jon
"Alien" <al***@sympatic o.ca> wrote in message
news:ug******** ********@news02 .bloor.is.net.c able.rogers.com ...
That is odd
I've replicated what (I think) you're doing, and in my case the overriden
method is called. This is what I have:

public class Test {
public virtual void testVoid() {
Console.WriteLi ne("base void!");
}
}

public class TestChild : Test {
public override void testVoid() {
Console.WriteLi ne("child void!");
}
}

In another class, I have:

Test t = new TestChild();
t.testVoid();

And the output I get is:
child void!

"Jon Davis" <jo*@REMOVE.ME. PLEASE.jondavis .net> wrote in message
news:uF******** ******@TK2MSFTN GP11.phx.gbl...
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class...

BaseClass bc = new ChildClass();

... and then call the virtual method, why is it that the base class's

method
is called instead of the overridden method? How do I fix this if I don't
know at runtime what the child class is? I'm using
Activator.Creat eInstance() to load the child class from a satellite DLL, a third-party plug-in that inherits my base class, so I therefore cannot
declare the child class type in the code or at compile time.

Thanks,
Jon


Nov 15 '05 #2
Jon Davis <jo*@REMOVE.ME. PLEASE.jondavis .net> wrote:
If I have a class with a virtual method, and a child class that overrides
the virtual method, and then I create an instance of the child class AS A
base class...

BaseClass bc = new ChildClass();

... and then call the virtual method, why is it that the base class's method
is called instead of the overridden method? How do I fix this if I don't
know at runtime what the child class is? I'm using
Activator.Creat eInstance() to load the child class from a satellite DLL, a
third-party plug-in that inherits my base class, so I therefore cannot
declare the child class type in the code or at compile time.


I suspect you haven't overridden the method properly. Could you post a
short but complete example which demonstrates the problem? See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
"Jon Skeet" <sk***@pobox.co m> wrote in message news:MP******** *************** *@news.microsof t.com...

I suspect you haven't overridden the method properly. Could you post a
short but complete example which demonstrates the problem? See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean.


You already said this and gave that link in a prior thread. No I can't because I don't know how to reproduce the problem without displaying all of my code.

Here's exactly what I have, with class, instance, and object names changed for the group:

IN ASSEMBLY A:

// name changed for group
public abstract class MyType {

...

// THIS IS WHERE THE DEBUGGER GOES WHEN STEPPING THROUGH THE CODE
// name, return type name changed for group
public virtual MyResultType MyMethod( ... ) {

.. default output ..

}

}

// name changed for group
public class MyCallingType {

// name, return type name changed for group
public MyResultType[] MyActions( ... ) {

...

// type names, names changed for group
MyTypeInstanceL ist myTypeInstances = Thing.MyLoadedE xternalPlugIns;

...

for (int i=0; i<myTypeInstanc es.Count; i++) {

// type name, name changed for group
MyType myTypeInstance = myTypeInstances[i];

// NOTE THAT AT THIS POINT THE DEBUGGER
// SAYS THAT THIS IS A MyNewType (see below)

...

// type name changed for group
MyResultType pr;

...

pr = myType.MyMethod ( ... );

...


}

}
}
IN ASSEMBLY B:

// names changed for group
public class MyNewType : MyType {

...

// THIS IS WHERE THE DEBUGGER SHOULD BE GOING
// WHEN STEPPING THROUGH THE CODE
// name, return type name changed for group
public override MyResultType MyMethod( ... ) {

...

}

}

Jon
Nov 15 '05 #4
This is how I'm instantiating the customized type ("MyNewType" ) from Assembly B...

assem = System.Reflecti on.Assembly.Loa dFile(fn);

....

Type t = assem.GetType(c lrName);

....

MyTypeDescripto r ptd = new MyTypeDescripto r(t, vers, name, desc, longDesc);

....

typeInstance = (MyType)Activat or.CreateInstan ce(
ptd.Type, new object[] {thing, myTypeNode});
Jon
"Jon Davis" <jo*@REMOVE.ME. PLEASE.jondavis .net> wrote in message news:el******** ******@TK2MSFTN GP12.phx.gbl...
(Ignore previous [cancelled] reply...)
"Jon Skeet" <sk***@pobox.co m> wrote in message news:MP******** *************** *@news.microsof t.com...
I suspect you haven't overridden the method properly. Could you post a
short but complete example which demonstrates the problem? See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean.

You already said this and gave that link in a prior thread. No I can't because I don't know how to reproduce the problem without displaying all of my code.

Here's exactly what I have, with class, instance, and object names changed for the group:

IN ASSEMBLY A:

// name changed for group
public abstract class MyType {

...

// THIS IS WHERE THE DEBUGGER GOES WHEN STEPPING THROUGH THE CODE
// name, return type name changed for group
public virtual MyResultType MyMethod( ... ) {

.. default output ..

}

}

// name changed for group
public class MyCallingType {

// name, return type name changed for group
public MyResultType[] MyActions( ... ) {

...

// type names, names changed for group
MyTypeInstanceL ist myTypeInstances = Thing.MyLoadedE xternalPlugIns;

...

for (int i=0; i<myTypeInstanc es.Count; i++) {

// type name, name changed for group
MyType myTypeInstance = myTypeInstances[i];

// NOTE THAT AT THIS POINT THE DEBUGGER
// SAYS THAT THIS IS A MyNewType (see below)

...

// type name changed for group
MyResultType pr;

...

pr = myTypeInstance. MyMethod( ... );

...


}

}
}
IN ASSEMBLY B:

// names changed for group
public class MyNewType : MyType {

...

// THIS IS WHERE THE DEBUGGER SHOULD BE GOING
// WHEN STEPPING THROUGH THE CODE
// name, return type name changed for group
public override MyResultType MyMethod( ... ) {

...

}

}

Jon
Nov 15 '05 #5
Jon Davis <jo*@REMOVE.ME. PLEASE.jondavis .net> wrote:
(Ignore previous [cancelled] reply...)
Too late - but see my reply to it.
// THIS IS WHERE THE DEBUGGER GOES WHEN STEPPING THROUGH THE CODE


Have you tried running the code in release mode? Maybe it's just a
debugger bug.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #6
Jon Davis <jo*@REMOVE.ME. PLEASE.jondavis .net> wrote:
This is how I'm instantiating the customized type ("MyNewType" )
from Assembly B...


That doesn't really help me until I can run the code. I've managed to
whip up a version which *does* work (see another post) - find out the
differences between that and your code. There's really nothing else I
can do without actually having some of your code in a form I can
compile and run directly.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #7
Jon Davis <jo*@REMOVE.ME. PLEASE.jondavis .net> wrote:
"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Have you tried running the code in release mode? Maybe it's just a
debugger bug.


Ah, a helpful, insightful answer that doesn't involve your copy-pasting into
the debugger (which I can do)!

Actually, this answer made me realize that files were not being properly
propogated to the proper directories upon compile. Hopefully that's my
problem, still testing ...

(As I said, there was no more source code to demonstrate my problem. ;)


But there was - if you'd posted a short but complete example which
*wasn't* working on your box, I could have tried it and said whether or
not it worked on *my* box. We'd then have seen that the code worked on
one machine and not another, and that therefore it probably wasn't the
code that was the problem, but the manner of running it.

It's absolutely *crucial* that everyone ends up looking at the same
code in this kind of thing.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #8

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Jon Davis <jo*@REMOVE.ME. PLEASE.jondavis .net> wrote:
"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Have you tried running the code in release mode? Maybe it's just a
debugger bug.


Ah, a helpful, insightful answer that doesn't involve your copy-pasting into the debugger (which I can do)!

Actually, this answer made me realize that files were not being properly
propogated to the proper directories upon compile. Hopefully that's my
problem, still testing ...

(As I said, there was no more source code to demonstrate my problem. ;)


But there was - if you'd posted a short but complete example which
*wasn't* working on your box,


But see I couldn't do that without giving you everything. I have ten
projects all outputting to the same directory, and I didn't realize that
this was the problem.

There was absolutely no way to help you help me by reproducing sample code
in new project code because I wouldn't have repeated the same error (unless
per chance I actually caught myself along the way).

Thanks again for your insightful question (re your previous post, "Release
mode"), it actually really helped me drill down to the problem, and frankly
I don't know how else I would have found my error.

Jon
Nov 15 '05 #9
Jon Davis <jo*@REMOVE.ME. PLEASE.jondavis .net> wrote:
But there was - if you'd posted a short but complete example which
*wasn't* working on your box,
But see I couldn't do that without giving you everything.


But you could. You could have reduced the code gradually (a copy of the
code, rather) until it worked. That would actually have shown you the
error very quickly (see below) due to what the problem turned out to
be. If the problem *had* been in the code, however, you would then have
found it when it went from "not working" to "working". Either way,
you'd have found the problem in the end.
There was absolutely no way to help you help me by reproducing sample code
in new project code because I wouldn't have repeated the same error (unless
per chance I actually caught myself along the way).
Which you should have done, because immediately after copying the full
code, the first thing to do would be to test that it still didn't work.
Thanks again for your insightful question (re your previous post, "Release
mode"), it actually really helped me drill down to the problem, and frankly
I don't know how else I would have found my error.


See above.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #10

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

Similar topics

9
2250
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application which makes calls to the service. We are using tcp channel which is using binaryformatter by default. The problem is that after a certain number of remoting calls the calls dont get through to the server. The client application makes the call and...
4
2399
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a hierarchy of algebraic matrices with the addition operation. Thus, I want to have a virtual base class class Matr;
2
1777
by: Bonj | last post by:
Hello Can anyone assist with the following class hierarcy problem? I have a series of window classes, the object model currently being as such: Window / | \ / | \ MDIClientWindow | TreeViewWindow WndProcWindow / \
10
1506
by: ct | last post by:
Hi, Here is my problem: I need to be able to call a virtual method when an object is deleted. class A { public: virtual ~A(); void virtual DestroyRespond();
32
4512
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
2
2514
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a pointer or reference to a derived type of the base class's return type. In .NET the overridden virtual function is similar, but an actual parameter of the function can be a derived reference from the base class's reference also. This dichotomy...
0
3937
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
10
2350
by: Markus Svilans | last post by:
Hi, I have a weird problem in a virtual method. The original method code raises an access violation when it is run. The solution to the problem is to declare a dummy integer inside the virtual method. Then the access violation no longer occurs! The following class (condensed from the NetCDF C++ interface): class NcVar : public NcTypedComponent
3
2187
by: Gregory | last post by:
I recently reviewed the decorator pattern in the GOF book and noticed a problem. Let look at the example given in the book. For simplicity I removed intermediate Decorator class. // Interface class class VisualComponent { public: VisualComponent();
0
8744
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
8659
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
9240
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
9103
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
8996
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
7846
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6585
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...
2
2426
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2055
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.