2015-09-01 40 views
0

我遇到了這個類的麻煩。我正在從VB6 VB.NET轉換到C#。創建自定義的有序字典

特別是ItemAddBeforeAddAfter方法。對於Item我傳遞了幾何形狀。

Reference question

我需要因爲我需要的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

這是什麼,你叫'VB6 Vb.Net'奇怪的生物? – Plutonix

+0

你的問題是什麼? – SLaks

+0

@Plutonix它的一箇舊程序被轉換爲C#4和一些部分給我的問題..尤其是集合 – Deke

回答

1

使用System.Collections.Specialized.OrderedDictionary代替

它的插入方法,該方法的索引作爲其輸入之一。這樣你可以在你想要的物品之後或之前插入。

  • 檢查密鑰使用情況是否存在方法。
  • 來者皆使用索引語法與鍵獲取項目,例如收集[「的myKey」]
  • 要通過索引的使用索引語法與指數獲得的項目,例如集合[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

無論你想用你的擴展方法,請記住,包括其命名爲「使用的

這裏是用法:

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