-1

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;
    }
}
Deke
  • 117
  • 1
  • 1
  • 8

2 Answers2

1

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)
Community
  • 1
  • 1
theB
  • 6,450
  • 1
  • 28
  • 38
  • Thanks I made the change to string – Deke Sep 09 '15 at 13:32
  • 1
    Few more steps and Deke own you a part of his salary. – Sinatr Sep 09 '15 at 13:37
  • Are there any articles that I can learn this stuff :) or do I just follow the MSDN – Deke Sep 09 '15 at 13:41
  • @theB now I get an error Cannot create an instance of the abstract class or interface 'System.Collections.ObjectModel.KeyedCollection' – Deke Sep 09 '15 at 13:43
  • @Deke - Whoops, missed that `KeyedCollection` is abstract. The answer is fixed. You may also want to review [Abstract Classes](https://msdn.microsoft.com/en-us/library/k535acbf%28v=VS.71%29.aspx?f=255&MSPPError=-2147217396) and the related literature. – theB Sep 09 '15 at 13:53
  • @theB sorry but I dont understand.. the answer is fixed. – Deke Sep 09 '15 at 13:55
  • @Deke - I updated the answer to cover the fact that `KeyedCollection` is an abstract class. (see # 3) – theB Sep 09 '15 at 13:57
  • @theB Thanks I will give it a shot Appreciate your time – Deke Sep 09 '15 at 14:01
0

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 *****************************   
Deke
  • 117
  • 1
  • 1
  • 8