创建自定义有序字典

时间:2015-09-01 15:03:06

标签: c# vb.net vb6

我在上课时遇到了麻烦。我正在从VB6 VB.NET转换为C#。

特别是ItemAddBeforeAddAfter方法。对于Item我传递的几何形状。

Reference question

我需要使用Ordered Dictionary,因为我需要格式为m_oCol(string, clsFeature)。在此集合中,我需要按特定顺序插入clsFeatures,因为处理规则可能是1,6,3,4,5,2。我有另一个访问这个类的类。

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

1 个答案:

答案 0 :(得分:1)

使用System.Collections.Specialized.OrderedDictionary代替

它有一个Insert方法,它将索引作为其输入之一。这样你就可以在你想要的项目之前或之前插入。

  • 检查是否存在密钥使用包含方法。
  • 按键使用索引器语法获取项目,例如集合[&#34; mykey&#34;]
  • 要通过索引获取项目,请使用带索引的索引器语法,例如collection [5]

您可以为IndexOf写一个extension method

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

上面的方法,返回字典中给定键的索引,如果字典中不存在键,则返回-1

无论您何时想要使用扩展方法,请务必将其名称空间包含在使用&#39;

以下是用法:

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