2

私はC#にかなり慣れていないので、これがばかげた質問であれば許してください。エラーが発生していますが、解決方法がわかりません。Visual Studio 2010 を使用しています。

このコード行

public class GClass1 : KeyedCollection<string, GClass2>

エラーが表示されます

'GClass1' does not implement inherited abstract member 'System.Collections.ObjectModel.KeyedCollection<string,GClass2>.GetKeyForItem(GClass2)'

私が読んだことから、これは継承されたクラスに抽象メンバーを実装することで解決できます

public class GClass1 : KeyedCollection<string, GClass2>
{
  public override TKey GetKeyForItem(TItem item);
  protected override void InsertItem(int index, TItem item)
  {
    TKey keyForItem = this.GetKeyForItem(item);
    if (keyForItem != null)
    {
        this.AddKey(keyForItem, item);
    }
    base.InsertItem(index, item);
}

ただし、これにより、「型または名前空間名が見つかりませんでした TKey/TItem が見つかりませんでした」というエラーが表示されます。

ヘルプ!

4

1 に答える 1

4

TKeyTItemは の型パラメータですKeyedCollection<TKey, TItem>

KeyedCollection<string, GClass2>具体的な型stringとそれぞれから継承しているため、実装ではプレースホルダー型とをこれらの 2 つの型GClass2に置き換える必要があります。TKeyTItem

public class GClass1 : KeyedCollection<string, GClass2>
{
  public override string GetKeyForItem(GClass2 item);
  protected override void InsertItem(int index, GClass2 item)
  {
    string keyForItem = this.GetKeyForItem(item);
    if (keyForItem != null)
    {
        this.AddKey(keyForItem, item);
    }
    base.InsertItem(index, item);
}
于 2012-11-20T17:06:59.717 に答える