-1

My person class:

 class Person
{
    public string FirstName { get; private set; }
    public string LastName { get; private set; }
    public int Age { get; private set; }

    public Person(string firstName,string LastName,int age)
    {
        this.FirstName = firstName;
        this.LastName = LastName;
        this.Age = age;


    }

    public override string ToString()
    {
        return this.FirstName + " " + this.LastName + " " + this.Age;

    }



}

Main:

 class Program
{
    static void Main(string[] args)
    {
        Person sallyPerson = new Person("Sally", "Solomon",23);

    }
}

Lets say I want to change the Firstname and Age of the person, how would I go about doing so? The FirstName and Age properties are set privately.

Charles Whitfield
  • 477
  • 1
  • 4
  • 7
  • 2
    Why have a private setter if you need to change them from outside of the class? – MAV May 02 '13 at 22:17
  • That makes no sense but you can make a public method and call set inside it. – Francisco Afonso May 02 '13 at 22:18
  • Is there a *reason* the sets are private? If so, then you should probably honor those reasons and not try to set them outside the class (but probably there should be a comment saying why not.) Otherwise, yes, just make them public, if you want to be able to set them and there isn't any reason not to... – neminem May 02 '13 at 22:19

5 Answers5

3

You could make the properties public -

class Person
{
    // changed private set; to set;
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}
Brandon Cuff
  • 1,438
  • 9
  • 24
1
class Program
{
    static void Main(string[] args)
    {
        Person sallyPerson = new Person("Sally", "Solomon",23);
sallyPerson = new Person("Newname", "Newvalue", 23);

    }
}

This would be one way, another way would be to add methods in the Person object:

public void UpdateFirstName(string newName)
{
this.FirstName = newName;
}

and do sallyPerson.UpdateFirstName("newName");

Or make the properties public with public set and get

AD.Net
  • 13,352
  • 2
  • 28
  • 47
1

The class has been set up as Immutable meaning that once an instance of this class has been constructed it's properties cannot be modified. If you want to be able to construct this class and them modify it's properties you need to make the classes set properties to be public, like so:

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}
Paul Welbourne
  • 1,063
  • 10
  • 16
1

I'm hoping this is a homework question, otherwise @Brandon Cuff's answer is the obvious one.

Alternatively, you could expose methods to set the fields:

class Person
{
    public void SetFirstName(string value)
    {
       this.FirstName = value;
    }
}
RPM1984
  • 72,246
  • 58
  • 225
  • 350
1

So I just realized, all of the answers are assuming that the question you're asking is literally the question you're asking, rather than just an example. But perhaps you were just using it as an example, and you actually need to set a private property of an object that you don't control the source of? In which case, I would say, that's generally a bad idea, but if you really need to do it anyway, there is a way. That way is called "reflection" (this being just one of many kind-of-sketchy things you can do with reflection if you really want). This is a question that you might want to look at, if that is actually what you're after.

More likely, though, the correct response is just don't have the properties be private if you control the source for that class, and are going to want to change them.

Community
  • 1
  • 1
neminem
  • 2,658
  • 5
  • 27
  • 36