2014-10-20 65 views
0

如果我一直在正确研究此问题,我之前得到了一些帮助,并且有一位用户表示,使用Dictionary来存储我的CountryPlaces将会很好。在第二个国家内添加地方标签

所以我创造我Dictionary

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

当用户点击该按钮,我想它在运行时创建的Dictionary内的newCountryClass一个实例,就会触发这个类。它会添加将是newCountryTitle.Country,然后是Class的字符串。

public void AddCountryCollection() 
{ 

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

因此,可以说用户已经增加了其创造了这个Dictionary在运行时Country,和他们增加了4 Countries,但现在想回去,并添加第二个国家内Place标签。

这是newCountryClass

private string _country; 

public string Country 
{ 
    get { return _country; } 
    set 
    { 
     if (_country.Equals(value)) 
     return; 

     _country= value; 
     RaisePropertyChanged(() => Country); 
    } 
} 

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

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

如果他们创造4,他们希望到Place添加到列表中的第二一个他们创造的是Country里面,我怎么会发现呢?

+0

考虑使用[OrderedDictionary](http://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary(v = vs.110).aspx),看起来像是唯一的方法。但它不是通用的。性能可能会比通常的普通字典稍差。 – 2014-10-20 02:42:32

+0

另外这个问题不应该这么长。它应该只是一些线*** ***。为什么?问题只是***如何获得字典***的第二个元素。就这样。问这样的问题将会使许多用户(包括专家)忽略这个问题(我们通常害怕长期问题)。 – 2014-10-20 02:50:13

回答

0

我找到了答案,我只是用我存储在关键:与

NTCD["GER"].Places.Add("New Place inside GER"); 

如果我只是想知道第二个,无论是采取List<NewCountryClass>并反复通过索引弗洛尔采取OrderedDictionary。它将对象键,对象值作为参数添加,我可以通过NTCD[3]访问它。