创建自定义有序字典

本文关键字:字典 自定义 创建 | 更新日期: 2023-09-27 18:10:46

这门课我有困难。我是从VB6 VB转换。. NET转换为c#

特别是Item, AddBeforeAddAfter方法。对于Item,我以几何形状传递。

参考问题

我需要使用Ordered Dictionary,因为我需要m_oCol(string, clsFeature)的格式。在这个collection中,我需要按照一定的顺序插入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;
    }

创建自定义有序字典

使用System.Collections.Specialized.OrderedDictionary代替

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

  • 使用Contains方法检查是否存在一个键。
  • 按键获取项目使用索引器语法与键,例如collection["mykey"]
  • 通过索引使用索引语法来获取项目,例如collection[5]

你可以为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;
    }
}

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

无论您想在哪里使用您的扩展方法,请记住将其命名空间包含到'using's

用法如下:

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