【问题标题】:KeyedCollection ErrorKeyedCollection 错误
【发布时间】:2015-12-05 11:45:01
【问题描述】:

我对 C# 集合很陌生。我不知道如何解决这个问题。

这行代码

public class clsFeatureCollection : KeyedCollection<string, clsFeature>

给出错误

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

这是我的代码。我不确定如何更正此问题有人可以帮忙吗?

public class clsFeature
{
    private int m_OID { get; set; }
    private IGeometry m_Geometry { get; set; }
}

public class clsFeatureCollection : KeyedCollection<string, clsFeature> // : IEnumerable
{
    // ************************** Keyed Collection ****************
    // https://msdn.microsoft.com/en-us/library/ms132438(v=vs.100)

    public KeyedCollection<string, clsFeature> m_oCol; // KeyedCollection<string, clsFeature>();
    public Dictionary<string, string> m_oColReverse;

    public clsFeatureCollection() : base() 
    {
        m_oCol = new clsFeatureCollection();
        m_oColReverse = new Dictionary<string, string>();
    }

    public int GeyKeyForItem(clsFeature item)
    {
        return item.OID;
    }
}

【问题讨论】:

  • 必须是protected override string GetKeyForItem(clsFeature item)。小心KeyedCollection,容易丢失物品。
  • @HansPassant 感谢它编译.. 现在看看它是否给出正确的结果
  • @HansPassantNow 我在 m_oCol = new clsFeatureCollection 处得到一个无限循环
  • @Deke - 你不能做你正在做的事情。我更新了我的答案。

标签: c# keyedcollection


【解决方案1】:

存在三个主要问题:

  1. 与其他答案一样,方法名称拼写错误
  2. KeyedCollection&lt;TKey, TItem&gt;.GetKeyForItem 应该返回 TKey (docs) 类型的对象
  3. 构造函数中的m_oCol = new clsFeatureCollection(); 行递归调用构造函数。由于KeyedCollection 是抽象的,所以不能直接实例化它。您需要使用base() 调用基本构造函数。 (见this answerthis answer

【讨论】:

  • 感谢我对字符串进行了更改
  • 再走几步,德克就将他的一部分薪水归给你。
  • 有没有我可以学习的文章:) 或者我只是按照 MSDN
  • @theB 现在出现错误无法创建抽象类或接口'System.Collections.ObjectModel.KeyedCollection'的实例
  • @Deke - 哎呀,错过了 KeyedCollection 是抽象的。答案是固定的。您可能还想查看Abstract Classes 和相关文献。
【解决方案2】:

感谢大家的意见和帮助

以下是我想出的最终工作解决方案。它与其他类似的集合集成。我最初使用了一个列表,但我需要一个字符串作为其他集合的键

// ************************** Ordered Dictionary - works ****************
// http://stackoverflow.com/questions/2722767/c-sharp-order-preserving-data-structures
// http://www.go4expert.com/articles/understanding-c-sharp-dictionaries-t30034/

    public OrderedDictionary m_oCol;
    public OrderedDictionary m_oColReverse;

    public clsFeatureCollection()
        : base()
    {
        m_oCol = new OrderedDictionary();
        m_oColReverse = new OrderedDictionary();
    }

    public IEnumerator GetEnumerator()
    {
        return m_oCol.GetEnumerator();
    }

    public void Add(IFeature pFeature, string strBefore = "", string strAfter = "", bool bReverse = false)
    {
        if (bReverse == true)
        {
            m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim());
        }

        if (!ContainsItem(pFeature.OID.ToString()))
        {
            m_oCol.Add(pFeature.OID.ToString(), new clsFeature(pFeature.OID, pFeature.ShapeCopy));
        }
    }

    public void AddBefore(IFeature pFeature, string strBefore, bool bReverse = false)
    {
        if (bReverse == true)
        {
            m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim());
        }

        if (!ContainsItem(pFeature.OID.ToString()))
        {
            if (strBefore != null)
            {
                int index = GetIndex(m_oCol, strBefore);

                if (index > 0)
                {
                    m_oCol.Insert(index - 1, pFeature.OID.ToString(), new clsFeature(pFeature.OID, pFeature.ShapeCopy));

                }
                else
                {
                    m_oCol.Insert(0, pFeature.OID.ToString(), new clsFeature(pFeature.OID, pFeature.ShapeCopy));
                }
            }
        }
    }

    public void AddAfter(IFeature pFeature, string strAfter, bool bReverse = false)
    {
        if (bReverse == true)
        {
            m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim());
        }

        if (!ContainsItem(pFeature.OID.ToString()))
        {
            if (!string.IsNullOrEmpty(strAfter))
            {
                int index = GetIndex(m_oCol, strAfter);

                m_oCol.Insert(index + 1, pFeature.OID.ToString(), new clsFeature(pFeature.OID, pFeature.ShapeCopy));
            }
            else
            {
                m_oCol.Insert(0, pFeature.OID.ToString(), new clsFeature(pFeature.OID, pFeature.ShapeCopy));
            }
        }
    }

    public int Count
    {
        get { return m_oCol.Count; }
    }

    public void Remove(int Id)
    {
        m_oCol.RemoveAt(Id);
    }

    public clsFeature Item(int Position)
    {
        try
        {
            clsFeature value = (clsFeature)m_oCol.Cast<DictionaryEntry>().ElementAt(Position).Value;

            return value;
        }
        catch (Exception)
        {
            throw;
        }
    }

    public void Clear()
    {
        m_oCol = new OrderedDictionary();
        m_oColReverse = new OrderedDictionary();
    }

    public bool Reverse(string valueRenamed)
    {
        bool bReverse = false;

        try
        {
            if (m_oColReverse.Contains(valueRenamed))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        catch (Exception ex)
        {
            if (ex is ArgumentException | ex is IndexOutOfRangeException)
            {
                bReverse = false;
            }
        }

        return bReverse;
    }

    public bool ContainsItem(string oidValue)
    {
        bool bContainsItem = false;

        string intOID = oidValue.ToString();

        try
        {
            // dictionary
            if (m_oCol.Contains(intOID))
            {
                bContainsItem = true;
            }
            else
            {
                bContainsItem = false;
            }

            return bContainsItem;
        }

        catch (Exception ex)
        {
            if (ex is ArgumentException | ex is IndexOutOfRangeException)
            {
                bContainsItem = false;
            }
        }

        return bContainsItem;
    }

    public static int GetIndex(OrderedDictionary dictionary, string key)
    {
        for (int index = 0; index < dictionary.Count; index++)
        {
            if (dictionary[index] == dictionary[key])
            {
                return index;
            }
        }

        return -1;
    }
}

// ******************************  End Ordered Dictionary - works *****************************   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-20
    • 2016-07-08
    • 2018-02-08
    • 1970-01-01
    • 2010-10-30
    • 2011-12-27
    • 1970-01-01
    • 2011-03-10
    相关资源
    最近更新 更多