Replace element on a List of object retrived as property / public field

I’m trying to do simple thing – replace object on a list with another one, ‘more up to date’. The problem is the actual list is not getting updated. List is defined and stored in my ‘DataProvider’ class:

public class CountriesDataSet
{
    List<Country> b;
    private IXmlBinder xmlBinder;

    public CountriesDataSet()
    {            
        xmlBinder = new BasicXmlLoader();
        Countries = xmlBinder.Load();
    }

    public List<Country> Countries;        

    public void Save()
    {
        xmlBinder.Save(Countries);
    }

}

And the usage is in another class, lets call it controller, where I store instance of my CountriesDataSet class. I’m trying to do the update there:

  var countries = countriesDataSet.Countries;
  Country country = countries.First(c => c.Id == id);
  if (country != null)
  {                    
      country = newCountry;
      countriesDataSet.Save();                    
  }

I can see that country is replaced by new instance of newCountry, but no update on the countriesDataSet.Countries, what am I doing wrong? What would be correct way of approaching this problem?


Source: .net

Leave a Reply