1

I am having trouble with this class. I am converting from VB6 VB.NET to C#.

In particular the Item, AddBefore and AddAfter methods. For the Item I am passing in a geometry shape.

Reference question

I need to use Ordered Dictionary because I need a format of m_oCol(string, clsFeature). In this collection, I need to insert clsFeatures in a certain order, could be 1, 6, 3, 4, 5, 2 because of processing rules. I have another class that accesses this class.

// ************************** Ordered Dictionary  ****************
    // https://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 Dictionary<string, string> m_oColReverse;

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

    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))
        {
            m_oCol.Add(pFeature.OID.ToString(), 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))
        {
            if (strBefore != null)
            {
                int intStringBefore = Int32.Parse(strBefore);

                int index = m_oCol.FindIndex(a => a.OID == intStringBefore);

                if (index > 0)
                {
                    m_oCol.Insert(index - 1, pFeature.OID.ToString(), pFeature.ShapeCopy);
                }
                else
                {
                    m_oCol.Insert(0, pFeature.OID.ToString(), 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))
        {
            if (!string.IsNullOrEmpty(strAfter))
            {
                int intStringAfter = Int32.Parse(strAfter);

                int index = m_oCol.FindIndex(a => a.OID == intStringAfter);

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

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

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

//        public clsFeature this[int Position]
//        {
//            get { return m_oCol[Position]; }
//            set;
//        }

    public clsFeature Item(int Position)
    {
        clsFeature value = default(clsFeature);

        value = m_oCol[Position]; // .GetObjectData(, Position);

        return value;
    }

    public void Clear()
    {
        m_oCol = new OrderedDictionary();
        m_oColReverse = new Dictionary<string, string>();
    }

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

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

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

        return bReverse;
    }

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

        int intOID = oidValue;

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

1 Answers1

1

Use System.Collections.Specialized.OrderedDictionary instead

It has an Insert method that takes an index as one of its input. That way you can insert after or before an item you want.

  • To check existence of a key use Contains method.
  • To get item by key use indexer syntax with key, for example collection["mykey"]
  • To get item by index use indexer syntax with index, for example collection[5]

You can write an extension method for IndexOf:

public static class Extensions
{
    public static int IndexOf(this System.Collections.Specialized.OrderedDictionary od, object key)
    {
        for (int index = 0; index < od.Count; index++)
        {
            if (od.Keys.OfType<object>().ToList()[index] == key)
                return index;
        }
        return -1;
    }
}

The above method, returns index of given key in dictionary and if the key not exists in dictionary, it returns -1

Wherever you want to use your extension method, remember to include its namespace to 'using's

And here is the usage:

var dictionary = new System.Collections.Specialized.OrderedDictionary();

dictionary.Add("A", "A Value");
dictionary.Add("C", "C Value");
dictionary.Add("D", "D Value");

MessageBox.Show(dictionary.IndexOf("C").ToString()); //Shoud be 1
MessageBox.Show(dictionary.IndexOf("B").ToString()); //Shoud be -1

dictionary.Insert(1, "B", "B Value");

MessageBox.Show(dictionary.IndexOf("B").ToString()); //Shoud be 1
MessageBox.Show(dictionary.IndexOf("D").ToString()); //Shoud be 3
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398