【问题标题】:add a Place label inside the second Country在第二个国家/地区内添加一个地方标签
【发布时间】:2014-10-20 02:20:07
【问题描述】:

如果我一直在正确地研究这个,我之前得到了一些帮助,一位用户说最好使用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);
}

假设用户添加了Country,它在运行时创建了这个Dictionary,他们添加了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 内的列表中,我该如何找到它?

【问题讨论】:

  • 考虑使用 OrderedDictionary ,看起来这是唯一的方法。然而,它不是通用的。性能可能比普通的通用字典稍差。
  • 这个问题也不应该这么长。它应该只有几行。为什么?问题只是如何获取字典的第二个元素。就这样。提出这样的问题会使许多用户(包括专家)忽略这个问题(我们通常害怕长问题)。

标签: c# wpf class dictionary


【解决方案1】:

我找到了答案,我只需使用我存储的密钥:

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

如果我只想知道第二个,要么采用List&lt;NewCountryClass&gt; 并使用for 进行索引迭代,要么采用OrderedDictionary。它将对象键、对象值作为参数添加,我可以通过NTCD[3] 访问它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多