Deke
Deke

Reputation: 117

KeyedCollection Error

I'm fairly new to C# collectons. I do not know how to solve this issue.

This line of code

public class clsFeatureCollection : KeyedCollection<string, clsFeature>

Gives the error

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

This is the code I have. I am not sure how to correct this Can anyone help?

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;
    }
}

Upvotes: -1

Views: 279

Answers (2)

Deke
Deke

Reputation: 117

Thanks everyone for their input and help

Below is the final working solution I came up with. It integrates with the other similar collections. I initially used a list but I needed a string as a key for the other collections

// ************************** 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 *****************************   

Upvotes: 0

theB
theB

Reputation: 6738

There's three major issues:

  1. As in the other answer, there's a misspelling of the method name
  2. KeyedCollection<TKey, TItem>.GetKeyForItem is supposed to return an object of type TKey (docs)
  3. The line m_oCol = new clsFeatureCollection(); in the constructor recursively calls the constructor. Since KeyedCollection is abstract, you can't directly instantiate it. You need to call the base constructor with base(). (See this answer or this answer)

Upvotes: 1

Related Questions