-6

Say I have a class like this:

class public Person
{
    public string firstName;
    public string lastName;
    public string address;
    public string city;
    public string state;
    public string zip;

    public Person(string firstName, string lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

And let's further say I create a List of type Person like this:

List<Person> pList = new List<Person>;
pList.Add(new Person("Joe", "Smith");

Now, I want to set the address, city, state, and zip for Joe Smith, but I have already added the object to the list. So, how do I set these member variables, after the object has been added to the list?

Thank you.

user717236
  • 4,959
  • 19
  • 66
  • 102
  • 4
    You really shouldn't be exposing the fields publicly; it would be more appropriate to use properties. Thanks to auto implemented properties it's very little additional effort. – Servy Feb 01 '13 at 16:15
  • I appreciate the advice. Can you please elaborate further with an example? – user717236 Feb 01 '13 at 16:16
  • 2
    Google is there for a reason. Use it. – Servy Feb 01 '13 at 16:16
  • @user717236 http://stackoverflow.com/questions/40730/how-do-you-give-a-c-sharp-auto-property-a-default-value – Dom Feb 01 '13 at 16:18
  • @user717236: I edited my answer to show a bit the use of such automatic properties. – Jesse Emond Feb 01 '13 at 16:21

5 Answers5

4

You get the item back out of the list and then set it:

pList[0].address = "123 Main St.";
Servy
  • 202,030
  • 26
  • 332
  • 449
3

You can keep a reference to your object around. Try adding like this:

List<Person> pList = new List<Person>;
Person p = new Person("Joe", "Smith");
pList.Add(p);
p.address = "Test";

Alternatively you can access it directly through the list.

pList[0].address = "Test";
Cashley
  • 516
  • 5
  • 16
  • Well, no, actually, you don't need to keep a reference around. You *can*, but you don't *need* to. – Servy Feb 01 '13 at 16:15
2

You can get the first item of the list like so:

Person p = pList[0]; or Person p = pList.First();

Then you can modify it as you wish:

p.firstName = "Jesse";

Also, I would recommend using automatic properties:

class public Person
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string address { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string zip { get; set; }

    public Person(string firstName, string lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

You'll get the same result, but the day that you'll want to verify the input or change the way that you set items, it will be much simpler:

class public Person
{
    private const int ZIP_CODE_LENGTH = 6;
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string address { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    private string zip_ = null;
    public string zip 
    { 
        get { return zip_; } 
        set
        {
            if (value.Length != ZIP_CODE_LENGTH ) throw new Exception("Invalid zip code.");
            zip_ = value;
        }
    }

    public Person(string firstName, string lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

Quite possibly not the best decision to just crash when you set a property here, but you get the general idea of being able to quickly change how an object is set, without having to call a SetZipCode(...); function everywhere. Here is all the magic of encapsulation an OOP.

Jesse Emond
  • 7,180
  • 7
  • 32
  • 37
1

You can access the item through it's index. If you want to find the last item added then you can use the length - 1 of your list:

List<Person> pList = new List<Person>;
// add a bunch of other items....
// ....
pList.Add(new Person("Joe", "Smith");
pList[pList.Length - 1].address = "....";
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
1

Should you have lost track of the element you're looking for in your list, you can always use LINQ to find the element again:

pList.First(person=>person.firstName == "John").lastName = "Doe";

Or if you need to relocate all "Doe"s at once, you can do:

foreach (Person person in pList.Where(p=>p.lastName == "Doe"))
{
    person.address = "Niflheim";
}
Nolonar
  • 5,962
  • 3
  • 36
  • 55