By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Whats wrong with this code

P: 45
Hi all,

Expand|Select|Wrap|Line Numbers
  1. namespace ConsoleApplication1
  2. {
  3.     class I
  4.     {
  5.         public void PrintIt()
  6.         {
  7.             PrintOne();
  8.  
  9.             ((I)this).PrintOne();
  10.         }
  11.  
  12.         public virtual void PrintOne()
  13.         {
  14.             Console.WriteLine("I am from base.\n");
  15.         }
  16.     }
  17.  
  18.     class G:I
  19.     {
  20.         public override void PrintOne()
  21.         {
  22.             Console.WriteLine("I am from sub.\n");
  23.         }
  24.     }
  25.  
  26.     class Program
  27.     {
  28.         static void Main(string[] args)
  29.         {
  30.             G g = new G();
  31.             g.PrintIt();
  32.             Console.ReadLine();
  33.         }
  34.     }
  35. }
  36.  
I am expecting the output to be

"I am from sub"

"I am from base"

but its not coming.... where am i making mistake?

I know I could achieve this by calling base.PrintOne in subclass... but i dont want it that way.

I want it to be called from base itself.

Please comment.

Thanks in advance.
Zacksoniar
Apr 12 '12 #1

✓ answered by Monomachus

It is not possible. What I meant is that after casting your I object is still instantiated as a G, so he'll get the PrintOne() method from the G because the I one is virtual, meaning what is said here http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx

"When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member."

So your G even casted will still point to a G and when the I virtual method is invoked, the run-time type of your object is G. And so it will call PrintOne() from G.

The bottom line is when you instantiate your object as a G, using new G(); it won't change its run-time type.

Hope this helps.

Share this Question
Share on Google+
7 Replies


Monomachus
Expert 100+
P: 127
That is the way polymorphism works. You override the method. And you declare the
Expand|Select|Wrap|Line Numbers
  1. G g = new G();
so you basically after cast you will still have something like
Expand|Select|Wrap|Line Numbers
  1. I i = new G();
More answers here http://stackoverflow.com/questions/4...n-method-c-net
Apr 13 '12 #2

P: 45
thanks Monomachus...

but even in this case.. I am not getting what i want i.e.
"I am from sub"
"I am from base"


I know I should have called base.PrintOne() from overridden method.
But I dont want to do that way. I want to achieve calling of base class method & sub class method from base class function only.

My code compiles .. doesn't it mean it is possible?

with ur approach too .. i get
"I am from sub"
"I am from sub"


It is just to understand polymorphism & upcasting..

Thnaks
Apr 13 '12 #3

Monomachus
Expert 100+
P: 127
It is not possible. What I meant is that after casting your I object is still instantiated as a G, so he'll get the PrintOne() method from the G because the I one is virtual, meaning what is said here http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx

"When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member."

So your G even casted will still point to a G and when the I virtual method is invoked, the run-time type of your object is G. And so it will call PrintOne() from G.

The bottom line is when you instantiate your object as a G, using new G(); it won't change its run-time type.

Hope this helps.
Apr 13 '12 #4

P: 45
then what is the significance of this line
Expand|Select|Wrap|Line Numbers
  1. I i = new G();
  2.  
does it mean nothing in C#, cause in java above statement mean a lot of things..

thanks in advance
Apr 14 '12 #5

P: 51
class I contains the virtual void PrintOne()

then you override the virtual void PrintOne() in class G

you need to call the class that contains your virtual void PrintOne() then assign the class that contains override..

I i = new G();
i.PrintOne();
//you get the override value of the virtual method

I i = new I();
i.PrintOne();
//you get the value of the virtual method

G g = new G();
g.PrintOne();
//you get nothing, because you wish to call for override without any virtual method.
Apr 18 '12 #6

P: 51
let say in real life..

.

class I, virtual void PrintOne() - is a delivery man that has delivery box.

.

class G, override void PrintOne() - is another delivery delivery box.

.

delivery man can deliver the delivery box.

.

delivery man can deliver another delivery box.

.

but another delivery box cant deliver itself, it needs a delivery man. :))
Apr 18 '12 #7

P: 45
Thanks everybody .. :-)
Apr 21 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.