Click here to Skip to main content
15,898,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CSS
namespace ConsoleAccessPrivateMembers
{
    class MyPrivateClassMem
    {
       private int a = 10, b = 20;

       public int aa
       {
           get
           {
               return aa;
           }
           set
           {
               aa = a;

           }
       }
       public int bb
       {
           get
           {
               return bb ;
           }
           set
           {
               bb = b;

           }
       }
        public int add()
        {

            return aa+bb ;
        }




    }
    class MyInput
    {

        static void Main(string[] args)
        {
            MyPrivateClassMem obj = new MyPrivateClassMem();
            obj.aa = 1;
            obj.bb = 2;


            Console.WriteLine(obj.add());


        }
    }
}


I am trying to change private variables values using properties but at executing time "Process is terminated due to stackoverflow exception " error encountered
So ,Kindly help here.
Posted

Beginning with C# 3.0, you now have auto-implemented Properties where what used to be the standard use of a "backing-field" variable is no longer necessary. If you write this code:
public int a { get; set; }
The compiler will generate a hidden private variable for you.

Here's what your code might look like taking advantage of this:
C#
using System;

namespace ConsoleAccessPrivateMembers
{
    internal class MyPrivateClassMem
    {
        // parameterless ctor
        public MyPrivateClassMem()
        {
            a = 10;
            b = 20;
        }

        // ctor with supplied parameters
        public MyPrivateClassMem(int parametera, int parameterb)
        {
            a = parametera;
            b = parameterb;
        }

        // auto-implemented public properties
        public int a { get; set; }
        public int b { get; set; }

        public int add()
        {
            return a + b;
        }
    }

    internal class MyInput
    {
        private static void Main(string[] args)
        {
            var obj1 = new MyPrivateClassMem();
            obj1.a = 1;
            obj1.b = 2;
            Console.WriteLine("obj1 add result: " + obj1.add());

            var obj2 = new MyPrivateClassMem(100, 200);
            Console.WriteLine("obj2 add result: " + obj2.add());

            // keep the Console window open
            Console.ReadLine();
        }
    }
}
Note the use of a parameterless Constructor that allows for the initialization of 'a and 'b to default values.

Also note a second, optional, Constructor with parameters supplied, so you can initialize 'a and 'b, if you wish, when you create an instance of the Class 'MyPrivateClassMem.
 
Share this answer
 
Comments
AVINCODE 26-Dec-13 3:51am    
Thanks... :-)
The getters should return the value in the local variable and the setters should use the keyword 'value' to se the values of the private variables.

C#
public int aa
       {
           get
           {
               return a;
           }
           set
           {
               a = value;

           }
       }
       public int bb
       {
           get
           {
               return b ;
           }
           set
           {
               b = value;

           }
       }
 
Share this answer
 
Comments
AVINCODE 26-Dec-13 3:50am    
Thanks...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900