4
Reply

C# inheritance and set get method

Yasmine Amin

Yasmine Amin

Jul 28 2008 5:53 AM
8.5k

Hi
I am trying to learn C# and i am trying to learn how to use inheritance but i'm now  stuck on a set get question. what i am trying to do is get the area of a square. below is my main program
namespace simpleInheritance
{
 //Main program to call my shapes class (parent class)
    public class program
    {
      [STAThread]
      static void Main()
      {
         //create an object to access shapes class
         shapes sa = new shapes();
         squar sq = new squar();
         sq.squareArea(); //To call the squareArea method in the square class
         Console.WriteLine("The area of a square is ");
         Console.Read();
      }
    }
}


This is the code for the shape and square class
namespace simpleInheritance
{
    public class shapes
    {
        private int h;
      public int height
      {
         get{ return h;}
        set{h=10;}
      }

  
      public int getHeight()
      {
         int theHeight = height; //to access the height GET SET method
         return theHeight;
      }
    }//end class shapes

   public class squar : shapes
   {
      public void squareArea()
      {
        
         int side = getHeight(); //to call the getHeight method in shapes class
         int area = side * side;
         Console.WriteLine("area is " + area);
      }
   }
}
my problem is, my set method (in red) doesnt get excuted and so the thavalue of h is not assigned to 10 which means the results of the area of the square is always 0.

can anyone tell me why the set method doesnt get excuted

thanks in advance


Answers (4)