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;   }
    } 
Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
user2740970
  • 137
  • 3
  • 4
  • 6

3 Answers3

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;
    }
}
Mansfield
  • 14,445
  • 18
  • 76
  • 112
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");
    }
Community
  • 1
  • 1
Paolo Falabella
  • 24,914
  • 3
  • 72
  • 86
2

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.

glautrou
  • 3,140
  • 2
  • 29
  • 34
  • yes but i want to set value to it index like { p[i].Age= 30; – user2740970 Sep 10 '13 at 16:48
  • but how to set the value to that index? – user2740970 Sep 10 '13 at 16:48
  • Should work fine. Your class can refer to its own private variables, even in another instance. – Mike Christensen Sep 10 '13 at 16:48
  • @user2740970 You can because you are in the same class. Look ar [Access Modifiers](http://msdn.microsoft.com/en-us/library/ms173121.aspx) for more details. – glautrou Sep 10 '13 at 16:49
  • Oh, disregard. I just noticed the comment `// Another Class ` – Mike Christensen Sep 10 '13 at 16:50
  • 2
    The answer is you cannot do that. Tell your boss to stop asking you to do things the compiler doesn't let you do. – Mike Christensen Sep 10 '13 at 16:51
  • Agree with @MikeChristensen, your boss should be worry about the maintenance cost, your code is long, complicated, difficult to maintain and error prone. How can another developer understand how it works without reading lots of comments for a behaviour which should be simple – glautrou Sep 10 '13 at 16:53
  • Sorry to say that but you cannot. The best way for you and your boss is to use the code posted in my answer. If you absolutely want to use the property from another class you can use `internal` instead of `private` – glautrou Sep 10 '13 at 17:04
  • @user2740970 - Another way would be to make *Another Class* a nested class of `Person`. You'd then be able to access Person's private fields. – Mike Christensen Sep 10 '13 at 17:07