2014-10-20 66 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]訪問它。