user3598246
user3598246

Reputation: 43

My rectangle area and perimeter comes out zero in C#

why the result in the area of the rectangle and it's perimeter comes out by zero?

in using C#

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            rec r = new rec();
            r.L = Convert.ToInt32(Console.ReadLine());
            r.wi = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(r.area());
            Console.WriteLine(r.per());
        }
    }
    class rec
    {
        int l;
        int w;
        public int L
            {
                set
                {
                    l = L;
                }
                get
                {
                    return l;

                }
            }
            public int wi
            {
                set
                {
                    w = wi;
                }
                get
                {
                    return w;
                }
            }
            public rec()
            {
            }
            public rec(int x, int y)
            {
                l = x;
                w = y;
            }
            public double area()
            {
                return l * w;
            }
            public int per()
            {
                return 2 * l + 2 * w;
            }
    }

}

Upvotes: 2

Views: 126

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

set should be using implicit value parameter. Your code sets properties to they current value instead:

 private int width;
 public int Width
 {
    get { return width; }
    set { width = value; }        
 }

Note that since you don't use any logic in get/set you can use auto-implemented properties instead:

 public int Width {get;set;} // no need to define private backing field.

Upvotes: 5

Related Questions