2

EDIT 2

I have got some help over the past few days on a problem that I am trying to work through. After receiving helpful support from several users, I have come across an error that I have been trying to fix over the weekend and still not succeeded.

I created a Dictionary, where I pass a string Country and also a ICollection of Places for that Country.

Dictionary<string, NewCountryClass> NTCD = new Dictionary<string, NewCountryClass>();

public void AddCountryCollection()
{

    newCountryClass = new NewCountryClass(newCountry);
    Collections.Add(newCountryClass);
    NTCD.Add(newCountryClass.Country, newCountryClass);

}


public void AddPlace()
{
    string Country = selectedItem.Country;
    RenameQuestion(placeName);
    NTCD[Country].Place.Add(placeName);    

}

Here is my newCountryClass where I stored the Country and Places in that Country.

private ICollection<string> _places;
public ICollection<string> Places
{
  get
  {
      return _places;
  }
  set
  {
     if (value == _places)
        return;

     _places = value;
     RaisePropertyChanged(() => Places);
  }
}

This is where the places could be added, but I create an instance of my class at the adding Country stage, and therefore can't pass a place at that time. (EDIT - I have moved the initialising of the collection into the constructor instead of in the Places property, as advised).

public NewCountryClass(string country)
{
    _places = new ObservableCollection<string>();

    if (country != null)
    {
        _country = country;
    }
}

Therefore, I attempted to create a renamePlace() method:

public void RenamePlace(string place)
{
    _places.Add(place);
}

However, _places still seems to be null even with this attempt. Any further ideas or anything I am doing wrong?

Ben
  • 245
  • 4
  • 13
  • Should you not use the Property Places rather than the field _places in the NewCountryClass constructor? Then the _places field would get initialised. – twrowsell Jan 27 '14 at 10:13
  • @twrowsell But then as you can see in the `get` section of the places property, `_places` is validated and still returns null. – Ben Jan 27 '14 at 10:17
  • Just to clarify, in the constructor it tests places != null . I can't see what field or variable that refers to. You have a field _places and a property Places. Should that be _places? – twrowsell Jan 27 '14 at 10:29
  • @twrowsell Look at my comment added to Sheridans answer, that should explain. – Ben Jan 27 '14 at 10:39
  • -1 For *changing your code so that current comments and answers no longer make sense*. – Sheridan Jan 27 '14 at 10:41
  • @Sheridan I had not finished editing, apologies. Hope this clarifies the changes properly. – Ben Jan 27 '14 at 10:47
  • Point accepted and down vote removed. – Sheridan Jan 27 '14 at 10:53

2 Answers2

0

You need to learn how to debug a program. Basically, you try to instantiate your NewCountryClass here:

public void AddCountryCollection()
{                                  // <<< Put breakpoint here <<<
    newCountryClass = new NewCountryClass(newCountry, placeName);
    Collections.Add(newCountryClass);
    NTCD.Add(newCountryClass.Country, newCountryClass);    
}

If the placeName input parameter is null in the constructor, then it is also null here... you need to add a breakpoint here and find out why the value is null and ensure that it has a value by this stage in your program.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Edited my question. I can't pass the place in the `AddCountryCollection` to the Constructor and therefore that is why `_places` is still null as when I instantiate, `_places` isn't getting passed a string value. I tried adding a method to pass _questions a string value separately but still errors and has a null value. – Ben Jan 27 '14 at 10:37
0

Would you not be better off initialising the _places collection in the constructor instead of the property get accessor?

public NewCountryClass(string country)
{
    _places = new ObservableCollection<string>();
    if (country != null)
    {
        _country = country;
    }

}

twrowsell
  • 467
  • 3
  • 8
  • Thanks for the answer, I have moved the collection down to the constructor, yet still have the problem with `_places` having nothing in the collection, even after calling the `RenamePlace()` method. – Ben Jan 27 '14 at 11:02
  • Is RenamePlace a member function on the NewCountryClass? – twrowsell Jan 27 '14 at 11:44
  • Yes, its in the same class. – Ben Jan 27 '14 at 14:32