473,545 Members | 1,310 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 5176
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
2226
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...
4
2389
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
1769
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
1489
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
4471
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
2
2500
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...
0
3915
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,...
10
2333
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...
3
2178
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
7391
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...
0
7651
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. ...
1
7410
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...
0
7746
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...
0
4941
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...
0
3443
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...
1
1869
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
1
1010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
693
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...

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.