简体   繁体   中英

C# - Update list item with new object values

I have an object which has some properties and a few of those properties are Lists . Each list contains instances of other classes. What i want to do is take the first item from a list and overwrite those property values.

Here's a pseudo example of what i have:

public class User
{
    public List<Address> Addresses = new List<Address>();

    public User ( )
    {
        Addresses = fill with data;
    }
}


public class TestUser
{
    public User user; // Is filled somewhere in this class

    public void TestUpdateList ( Address addr )
    {
        // The param "addr" contains new values
        // These values must ALWAYS be placed in the first item
        // of the "Addresses" list.

        // Get the first Address object and overwrite that with
        // the new "addr" object
        user.Addresses[0] = addr; // <-- doesn't work, but should give you an idea
    }
}

I hope this example shed some light on what i want to do.

So i am basically looking for a way to "update" an existing item in a list, which is in this case an object .

Your example doesn't compile because you're accessing the Addresses property via class name. That is only possible if it is static. So you need an instance of a user first, to update his addresses:

User u = new User(userID); // assuming that theres a constructor that takes an identifier
u.Addresses[0] = addr;

C# Language Specification: 10.2.5 Static and instance members

It is not entirely clear what you are trying to accomplish, however, see the following code -- there is an Address, a User, and an utility called FeatureX that replaces the first Address of a User with a given value.

class Address {
    public string Street { get; set; }
}

class User {
    public List<Address> Addresses = new List<Address>();
}

class FeatureX {
    public void UpdateUserWithAddress(User user, Address address) {
        if (user.Addresses.Count > 0) {
            user.Addresses[0] = address;
        } else {
            user.Addresses.Add(address);
        }
    }
}

The following usage outputs 'Xyz' two times:

User o = new User();
Address a = new Address() { Street = "Xyz" };

new FeatureX().UpdateUserWithAddress(o, a);
Console.WriteLine(o.Addresses[0].Street);

o = new User();
o.Addresses.Add(new Address { Street = "jjj" });
new FeatureX().UpdateUserWithAddress(o, a);
Console.WriteLine(o.Addresses[0].Street);

Be aware that public fields may cause a lot of trouble if you share your DLL with a third party.

I think the problem is that Addresses is a private field.

This works:

[TestFixture]
public class ListTest
{
    [Test]
    public void UpdateTest()
    {
        var user = new User();
        user.Addresses.Add(new Address{Name = "Johan"});
        user.Addresses[0] = new Address { Name = "w00" };
    }
}
public class User
{
    public List<Address> Addresses { get;private  set; }

    public User()
    {
        Addresses= new List<Address>();
    }
}
public class Address
{
    public string Name { get; set; }
}
public void TestUpdateList ( User user, Address addr )
    {
        // The param "addr" contains new values
        // These values must ALWAYS be placed in the first item
        // of the "Addresses" list.

        // Get the first Address object and overwrite that with
        // the new "addr" object
        user.Addresses[0] = addr; // <-- doesn't work, but should give you an idea
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM