views:

245

answers:

9

I am trying to execute the following code

    class A
    {
        protected int a;
        protected char b;
        public void Show()
        {
            a=5;
            MessageBox.Show(""+a);
        }
    }
        class B:A
        {
            public void Show()
            {
                b='z';
                MessageBox.Show(""+a+ ""+b);
            }
        }

i am getting 5 (value of a) as the output when i do aa.show () where aa is the instance of A but when i do bb.show() ,where bb is the instance of B, the output comes as 0 (value of a) z (value of b) can someone please explain why the derived class is unable to display the current value of a even though it has been declared as protected whereas it is able to display the correct value of b?

+9  A: 

Your B method is hiding the A method, you should be getting a compiler warning about this.

Better is as follows - note the virtual and override keywords:

class A
{
    protected int a;
    protected char b;
    public virtual void Show()
    {
        a=5;
        MessageBox.Show(""+a);
    }
}
class B:A
{
    public override void Show()
    {
        base.Show();
        b='z';
        MessageBox.Show(""+a+ ""+b);
    }
}

But here you'll have two message boxes pop up. You need to separate the setting of the variable in A with the message box function, for example set 'a' in the constructor.

Paolo
+1 for mentioning the compiler warning
JeffH
don't understand people that ask questions even not compiling the code... and tells that obtains output...
serhio
+1  A: 

If you set the value of a outside of the Show() method in class A, you will get the result that you expect. The implementation of B.Show hides A.Show, so it never executes.

Adam Crossland
+2  A: 

Try the following:

class A 
{ 
    protected int a; 
    protected char b; 
    public virtual void Show() 
    { 
        a=5; 
        MessageBox.Show(""+a); 
    } 
} 

class B:A 
{ 
     public override void Show() 
     { 
         b='z'; 
         MessageBox.Show(""+a+ ""+b); 
     } 
} 
Webleeuw
A: 

Current value of a in bb.show() is 0, so the output is correct. You set it to 5 only in the Show method in the A class.

Grzenio
A: 

Your must initialize value of a in class constructor or other method.
Show() declared in B class hides Show() declared in A class, therefore method of A class not invoked.

Read more about polymorphism and inheritance in C++ and C#.

E.g. you can be surprised with results of such piece of code:

A aa;
B bb;
//... init vars ...
aa = bb;
bb.Show();
aa.Show();
bb.Show();

;-)

ThinkJet
A: 

a=5; is set in your A.Show method.

B class will never set a=5 because it will use the B.Show method.

The question is not quite correct: why the derived class is unable to display the current value of a?

The derived class is able to display the a value, but this a value never changed from default 0 in the B class.

try also to initialize a outside of Show() like:

class A {
    protected int a = 5; // a = 5 will be visible in B too
serhio
+1  A: 

You probably have a build warning like this, don't you?

Warning 1 'ConsoleApplication7.B.Show()' hides inherited member 'ConsoleApplication7.A.Show()'. Use the new keyword if hiding was intended. C:\Documents and Settings\jhoover.ANDT\My Documents\Visual Studio 2005\Projects\ConsoleApplication7\ConsoleApplication7\Program.cs 25 21 ConsoleApplication7

As Paolo mentions, B.Show is hiding A.Show(). Webleeuw's code shows how to do what you want, by making A.Show() virtual and marking B.Show() with the override keyword.

P.S. Don't use ""+a to convert to string, use a.ToString()

JeffH
A: 

Use a Template Method pattern http://en.wikipedia.org/wiki/Template_method_pattern

class A
{
 protected int a;

 protected virtual string GetForShow()
 {
  a = 5;
  return a.ToString();
 }
 public void Show()
 {
  var forShow = GetForShow();
  MessageBox.Show(forShow);
 }
}
class B
{
 protected char b;
 protected override string GetForShow()
 {
  b = 'z';
  return base.GetForShow() + b;
 }
}
George Polevoy
A: 

thx for the reply guys...i'll try the solutions suggested and let you know. regarding the compiling error, there was none and i was able to get the output as mentioned in the question.

Pratik Gandhi