Skip to content

C# inheritance and set get method

Yasmine Amin

Asked Jul 28, 2008, 1:53 AM EDT4 answers
Share
LinkedInX

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

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Niradhip Chakraborty
    +1

    Hey check out the following code:

    C#
    namespace ConsoleApplicationtest
    {
        class Class1
        {
            abstract class Shape
            {
                public abstract double Area
                {
                    get;
                    set;
                }
            }
    
            class Square : Shape
            {
                public double side;
    
                public Square(double s)  //constructor
                {
                    side = s;
                }
    
                public override double Area
                {

                    get

    C#
                    {
                        return side * side;
                    }
                    set
                    {
                        side = System.Math.Sqrt(value);
                    }
                }
            }
    
            class TestShapes
            {
                static void Main()
                {
                    // Input the side:
                    System.Console.Write("Enter the side: ");
                    double side = double.Parse(System.Console.ReadLine());
    
                    // Compute the areas:
                    Square s = new Square(side);
    
    
                    // Display the results:
                    System.Console.WriteLine("Area of the square = {0:F2}", s.Area);
    
                    System.Console.WriteLine();
    
    
                }
            }
        }
    }
    +1
  • +1

    Hi
    Don't have any problem in your code just you need to call set stament then will excute set properties. i've chaged in code give below and in bold.

    {
     //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()
          {

             height=15;// you need to call set then will excute.
             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);
          }
       }
    }

    +1
  • +1
    thank you both for your replies. they ahve been great help.

    thanks
    yasmine
    +1
  • +1
    Welcome Anytime........................
    +1