1

i am using a getter/setter properties to get or set variables my code is working fine if i use the public variable for setting value as i am making a array of my class but i am just wondering how to set value of private variable . My code is

public class Person
{
    //getter and setter for each variable
    private string _Name;
    public string Name
    {
        get { return _Name;}
        set { _Name= value; }
    }

    private int _Age;
    public int Age
    {
        get {return _Age;  }
        set  {  _Age= value;   }
    }  
        .... // other properties

    // Another Class 
    public void setValues (Person[] p,int i)
    {    p[i].Age= 30;
    }

But how to set the variable if i change my set variable to private ??

    private int _Age;
    public int Age
    {
       get {return _Age;  }
       private  set  {  _Age= value;   }
    } 
6
  • sorry its Age not Title Commented Sep 10, 2013 at 16:44
  • Why would you want to do that? Commented Sep 10, 2013 at 16:45
  • my boss told me to use private for setting a variable Commented Sep 10, 2013 at 16:47
  • 3
    I'm pretty sure that either you or your boss has misunderstood how this class is supposed to be used. Commented Sep 10, 2013 at 16:49
  • 1
    Private variables can only be set within the class itself. That's their whole purpose. Commented Sep 10, 2013 at 16:50

3 Answers 3

7

If you change your set method to private, you won't be able to set the value of that property outside the class; that's the whole point of the private keyword. I'd consider the protected or internal keywords instead if you want to avoid it being public.

Or, as JNYRanger says, you could call this setter from a constructor which would allow you to effectively set that value "outside" the current class.

Example:

public class Person 
{    
    public int Age { get; private set; }

    public Person (int age) 
    {
        Age = age;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

+1, but just to add on: Unless you use an overloaded constructor to set this private variable at initialization
i did this but how to pass the index of my array ??
@user2740970 Pass the index of your array to what? You'd have to create a new person object to set the Age as I've described here.
This does not answer the question. It avoids the problem by solving it in another way.
I'm a C# newby. How can I set the value of Age from outside the class shown in this answer?
3

Are you looking for this:

public int Age { get; private set; }

It is easy to manage and you don't have to use _Age. In your case _Age is not needed.

9 Comments

yes but i want to set value to it index like { p[i].Age= 30;
but how to set the value to that index?
Should work fine. Your class can refer to its own private variables, even in another instance.
@user2740970 You can because you are in the same class. Look ar Access Modifiers for more details.
The answer is you cannot do that. Tell your boss to stop asking you to do things the compiler doesn't let you do.
|
3

I can't reverse-engineer your boss to understand why he's recommending you not to use public setters. If I had to take a guess, though, I would say he's encouraging you to make your classes immutable.

Immutability means you can't change the state of a class after creating it (i.e. no public setters or methods that change the internal state). It has various advantages (e.g. you can take a read to What's the advantage of a String being Immutable?).

So how do you change the age of a Person after creating it, if you have no setters? You can't. You create a new Person with the required age and "throw away" the other Person.

For reference you can read How to: Implement a Lightweight Class with Auto-Implemented Properties

Now, to your example:

//immutable Person
public class Person
{
    public string Name {get; private set;}

    public int Age {get; private set;}
    public Person(int Age, string Name)
    {
        this.Age=Age;
        this.Name=Name;
    }
}

you would have

 // Another Class 
    public void setValues (Person[] p,int i)
    {    
        // p[i].Age= 30; // no
           p[i]=new Person(p[i], "new name");
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.