Casting

G

Guest

Hello all,

I have an object of type B that inherits from an object of type A.

I create a new instace of B, but I need to cast/convert it to an object of
type A in order to call a particular method. I've tried all sorts of ways to
cast it as A but it always stays as an object of type B. The below is an
example of the code I've tried:

A a = B.Create();
A a = b as A;

No matter what I do, it always stays as B!!

Can anyone offer any help?

Thanks,

Jon
 
G

Guest

Hi,

the method in A is virtual and overridden in B, but i need to call the A
method. How?

Jon
 
G

Guest

If you want to call method present in A,then dont make it virtual instead
make it protected then it will be available in B class also
 
G

Guest

When calling a virtual method you are always calling the method of the
actual class. If you don't want it that way, then you shouldn't make it
virtual.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top