Casting

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
Hi,

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

Jon
 
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
 
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.
 
Back
Top