471,854 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,854 software developers and data experts.

How to call the subclass's method?

Hi,

I created a class "myClass" and another class "mySubclass" which inherited
from "myClass". In the "mySubclass", there is a public method named
"methodOfSubclass(...)" which is not defined in "myClass".

In a client program I tried to call the "methodOfSubclass(...)", like

.......
myClass mc;
mc = new mySubclass();
mc.methodOfSubclass(...);
.......

but I got an error message when I tried to compile it:

'...myClass' does not contain a definition for 'methodOfSubclass'

What is the problem? Do I have to create a virtual method
"methodOfSubclass(...)" in "myClass" and override it in "mwSubclass"? Any
other solutions?

Thanks

Q.

Nov 16 '05 #1
4 6778
but I got an error message when I tried to compile it:

'...myClass' does not contain a definition for 'methodOfSubclass'

What is the problem?
Exactly what the error message says.

Do I have to create a virtual method
"methodOfSubclass(...)" in "myClass" and override it in "mwSubclass"?
That's one way to do it. If it makes sense for the base class to have
such a method.

Any other solutions?


((mySubclass)mc).methodOfSubclass(...);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2

"Quentin Huo" ...
......
myClass mc;
mc = new mySubclass();
mc.methodOfSubclass(...);
......

but I got an error message when I tried to compile it:

'...myClass' does not contain a definition
for 'methodOfSubclass'

What is the problem? Do I have to create a virtual method
"methodOfSubclass(...)" in "myClass" and override it in
"mwSubclass"?
That's one way...
Any other solutions?


The type of the variable controls what you can access through it. The *only*
things you can access is what is defined in the variable's type, which can
be a class or an interface.

The simplest is to let the variable be of the subclass type from the
beginning.

mySubclass mc;
mc = new mySubclass();
mc.methodOfSubclass(...);

Another way is to cast to another variable of the subclass type:

myclass mc;
mc = new mySubclass();

mySubclass msc = (mySubclass) mc;
mc.methodOfSubclass(...);

Or you can use the "virtual/override" thingy...

// Bjorn A
Nov 16 '05 #3
If the method is unique to the subclass you may not want it virtual as other
subclasses may not need it. Have you tried to cast the object to the
subclass type like this:

mySubclass msc = (mySubclass)mc;
msc.methodOfSubclass();

--
William Stacey, MVP

"Quentin Huo" <q.***@manyworlds.com> wrote in message
news:uC**************@tk2msftngp13.phx.gbl...
Hi,

I created a class "myClass" and another class "mySubclass" which inherited
from "myClass". In the "mySubclass", there is a public method named
"methodOfSubclass(...)" which is not defined in "myClass".

In a client program I tried to call the "methodOfSubclass(...)", like

......
myClass mc;
mc = new mySubclass();
mc.methodOfSubclass(...);
......

but I got an error message when I tried to compile it:

'...myClass' does not contain a definition for 'methodOfSubclass'

What is the problem? Do I have to create a virtual method
"methodOfSubclass(...)" in "myClass" and override it in "mwSubclass"? Any
other solutions?

Thanks

Q.


Nov 16 '05 #4
Try something like

if(mc is mySubclass)
{
(mc as mySubclass).methodOfSubclass(...);
}

paul
"Quentin Huo" <q.***@manyworlds.com> wrote in message
news:uC****************@tk2msftngp13.phx.gbl...
Hi,

I created a class "myClass" and another class "mySubclass" which inherited
from "myClass". In the "mySubclass", there is a public method named
"methodOfSubclass(...)" which is not defined in "myClass".

In a client program I tried to call the "methodOfSubclass(...)", like

......
myClass mc;
mc = new mySubclass();
mc.methodOfSubclass(...);
......

but I got an error message when I tried to compile it:

'...myClass' does not contain a definition for 'methodOfSubclass'

What is the problem? Do I have to create a virtual method
"methodOfSubclass(...)" in "myClass" and override it in "mwSubclass"? Any
other solutions?

Thanks

Q.

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by ToChina | last post: by
44 posts views Thread by Steven D'Aprano | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
aboka
reply views Thread by aboka | last post: by

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.