Hi everyone.....

class Program
    {
       private int price = 0;
        static void Main(string[] args)
        
        {
            Program instance = new Program();
            Console.WriteLine("ENTER PRICE");
            instance.price = Convert.ToInt32(Console.ReadLine());
        }

Ok so im learning...as you may have guessed...

My question is..I know we should use getters and setters so we can shield the public from having any access to fields such as "price".

But here I am assigning price the value straight from the command prompt that is entered...

Why does it allow me to do this if I have declared "price" private?

thanks for your help...

Recommended Answers

All 4 Replies

Becuase the private field (price) is created inside a Program class.
Even you have called it from a static method, which only can access to the parameters inside of it, you created a new instance of a Program class, and used this instance to connect to the private field.

Pritave modifier are still accessed from inside of the class. If you create a new class, and create a new instance of a Program inside of it, then "price" field will not be accessed any longer.

Mitja

An example:

public class MyClass1
    {
        private int price1;
        public int price2;

        public MyClass1()
        {
            int newPrice1 = price1;
            int newPrice2 = price2;
        }
    }

    public class MyClass2
    {
        public MyClass2()
        {
            MyClass1 class1 = new MyClass1();
            class1.price2;
            //class1.price1 IS NOT ACCESSIBLE FROM HERE!
        }
    }

Thanks very much...!! great help...

One more question..

class program
            
    {
        private int hello = 5;
        
        static void Main()
        {
            Console.WriteLine("Enter val:");
            int val = Convert.ToInt32(Console.ReadLine());
            program instance = new program();
           Console.WriteLine("VALUE OF HELLO IS {0}", instance.getPrice());
           instance.hello2(val);
           Console.WriteLine("HELLO IS NOW: {0}", instance.hello);

        }
        public int getPrice()
        {
            return this.hello;
        }
        
        private void hello2(int n)
        {
           this.hello = n;
        }

Is this a good way of programming or would you rather use properties to get and set values?

Also when I assign the value of val (n) to "hello", why does it not lose its value and return to 5? I would have thought i would have to use the ref keyword (ref val) in order to get hello to stay at (val)?? Hope that makes sense and thanks for your help....

Depends what you want to do/use. It this case you dont even need to use get,set accessors, but actually you dont need to use get, set at all. You can read some explanation HERE.

Regarding your question, you can try this way:

private int hello = 5;

        static void Main()
        {
            Console.WriteLine("Enter val:");
            int val = Convert.ToInt32(Console.ReadLine());
            Program instance = new Program();
            Console.WriteLine("VALUE OF HELLO IS {0}", instance.getPrice());
            instance.hello2(val);
            Console.WriteLine("HELLO IS NOW: {0}", instance.hello);
            Console.ReadLine();
        }
        public int getPrice()
        {
            return this.hello;
        }

        private int hello2(int n)
        {
           return this.hello = n;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.