2015-09-04 46 views
0

參考問題Creating a custom ordered dictionaryOrderedDictionary返回一個Item?

我正在使用OrderedDictionary,因爲我需要跟蹤鍵和輸入值的順序。

我想獲得該項目,並將其返回到該行:

pPolyline = (IPolyline)colConnectedFeatures.Item(i).Geometry;

我正在尋找一種方式來回報的項目?我使用GetObjectData嗎?我需要序列化嗎?

private static IPolyline BuildPolyline(clsFeatureCollection colConnectedFeatures, Hashtable htChecked) 
{  
    for (int i = 0; i < colConnectedFeatures.Count; i++) 
    { 
     pPolyline = (IPolyline)colConnectedFeatures.Item(i).Geometry; 

     if (colConnectedFeatures.Reverse(colConnectedFeatures.Item(i).OID.ToString().Trim())) 
     { 
      pPolyline.ReverseOrientation(); 
     } 
    } 


public class clsFeature 
{ 
    private int m_OID { get; set; } 
    private IGeometry m_Geometry { get; set; } 

    public clsFeature(int iOID, IGeometry pGeometry) 
    { 
     this.m_OID = iOID; 
     this.m_Geometry = pGeometry; 
    } 

    public int OID 
    { 
     get { return m_OID.GetHashCode(); } 
    } 

    public IGeometry Geometry 
    { 
     get { return m_Geometry; } 
    } 

    public override int GetHashCode() 
    { 
     return OID.GetHashCode(); 
    } 

    public override bool Equals(object obj) 
    { 
     if (obj == null) 
     { 
      return false; 
     } 

     return base.Equals(obj); 
    } 
} 

public class clsFeatureCollection : IEnumerable 
{ 

    // ************************** 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 clsFeatureCollection() 
     : base() 
    { 
     m_oCol = new OrderedDictionary(); 
    } 


    public clsFeature Item(int Position) 
    { 
    //   object key = m_oCol.Cast<DictionaryEntry>().ElementAt(Position).Key; 
    //   object value = m_oCol.Cast<DictionaryEntry>().ElementAt(Position).Value; 

     clsFeature clsValue = default(clsFeature); 

     object[] keys = new object[m_oCol.Keys.Count]; 
     object[] values = new object[m_oCol.Values.Count]; 

     m_oCol.Keys.CopyTo(keys, 0); 
     m_oCol.Values.CopyTo(values, 0); 

     for (int i = 0; i < m_oCol.Keys.Count; i++) 
     { 
      Console.WriteLine("Index = {0}, Key = {1}, Value = {2}", i, keys[i], m_oCol[i]); 
     }  

     return (clsFeature)clsValue; 
    } 
+3

您有什麼特別的問題?你是否要求[代碼評論](http://codereview.stackexchange.com/)? – 31eee384

+1

我很想念一些泛型。 'OrderedDictionary'首次在.NET 2.0中引入,泛型也是如此。還有'IEnumerable'和'HashTable'。 – Caramiriel

+0

@Deke也許您可以詢問您的解決方案中的具體問題。如果我們不知道你想解決什麼問題,這只是一個代碼審查。 (編輯迴應編輯:我應該更明確地說:Stack Overflow不是代碼評論的地方,請查看我評論中對於這個地方的鏈接。) – 31eee384

回答

0

謝謝大家對他們的意見和幫助

下面是我想出了最後的工作方案。它與其他類似的收藏集成在一起。我最初使用了一個列表,但我需要一個字符串作爲其他系列的鑰匙

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