2015-06-25 38 views
-2

嗨大家情況如何?VB集合轉換爲c#LinkedList

我對於我的生活一直無法弄清楚這一點。我一直在轉換GIS應用程序。我所擁有的基本上是VB中的一個集合,我需要將它轉換爲c#中的鏈表。

任何幫助入門的讚賞。

VB代碼如下

Imports ESRI.ArcGIS.esriSystem 

Public Class clsFeature 

Private m_OID As Integer 
Private m_Geometry As ESRI.ArcGIS.Geometry.IGeometry 

Public Sub New(ByRef iOID As Integer, ByRef pGeometry As ESRI.ArcGIS.Geometry.IGeometry) 
    m_OID = iOID 
    m_Geometry = pGeometry 
End Sub 

Public ReadOnly Property OID() As Integer 
    Get 
     OID = m_OID 
    End Get 
End Property 

Public ReadOnly Property Geometry() As ESRI.ArcGIS.Geometry.IGeometry 
    Get 
     Geometry = m_Geometry 
    End Get 
End Property 
End Class 

Friend Class clsFeatureCollection 
Implements System.Collections.IEnumerable 

' linkedlist??????????????????????????????????????? 
Private m_oCol As Collection 
Private m_oColReverse As Collection 

Public Sub New() 
    MyBase.New() 
    m_oCol = New Collection 
    m_oColReverse = New Collection 
End Sub 

Public Sub Add(ByRef pFeature As ESRI.ArcGIS.Geodatabase.IFeature, Optional ByRef strBefore As String = "", Optional ByRef strAfter As String = "", Optional ByRef bReverse As Boolean = False) 
    'Create a new cFoo object based on parameters 
    'passed to this method, then add the new cFoo to 
    'the private collection, and key it by a 
    'unique identifier built into cFoo 
    'so we can retrieve it quickly later 

    'Add the new foo object to the collection 

    If bReverse Then 
     m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim()) 
    End If 

    If Not ContainsItem(pFeature.OID.ToString().Trim()) Then 
     If strBefore <> "" Then 
      m_oCol.Add(New clsFeature(pFeature.OID, pFeature.ShapeCopy), pFeature.OID.ToString().Trim(), strBefore) 
     ElseIf strAfter <> "" Then 
      m_oCol.Add(New clsFeature(pFeature.OID, pFeature.ShapeCopy), pFeature.OID.ToString().Trim()) 
     Else 
      m_oCol.Add(New clsFeature(pFeature.OID, pFeature.ShapeCopy), pFeature.OID.ToString().Trim()) 
     End If 
    End If 

End Sub 

Public Sub AddBefore(ByRef pFeature As ESRI.ArcGIS.Geodatabase.IFeature, ByRef strBefore As String, Optional ByRef bReverse As Boolean = False) 
    'Create a new cFoo object based on parameters 
    'passed to this method, then add the new cFoo to 
    'the private collection, and key it by a 
    'unique identifier built into cFoo 
    'so we can retrieve it quickly later 

    'Add the new foo object to the collection 
    If bReverse Then 
     m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim()) 
    End If 

    If Not ContainsItem(pFeature.OID.ToString().Trim()) Then 
     If strBefore <> "" Then 
      m_oCol.Add(New clsFeature(pFeature.OID, pFeature.ShapeCopy), pFeature.OID.ToString().Trim(), strBefore) 
     Else 
      m_oCol.Add(New clsFeature(pFeature.OID, pFeature.ShapeCopy), pFeature.OID.ToString().Trim()) 
     End If 
    End If 

End Sub 

Public Sub AddAfter(ByRef pFeature As ESRI.ArcGIS.Geodatabase.IFeature, ByRef strAfter As String, Optional ByRef bReverse As Boolean = False) 
    'Create a new cFoo object based on parameters 
    'passed to this method, then add the new cFoo to 
    'the private collection, and key it by a 
    'unique identifier built into cFoo 
    'so we can retrieve it quickly later 

    'Add the new foo object to the collection 
    If bReverse Then 
     m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim()) 
    End If 

    If Not ContainsItem(pFeature.OID.ToString().Trim()) Then 
     If strAfter <> "" Then 
      m_oCol.Add(New clsFeature(pFeature.OID, pFeature.ShapeCopy), pFeature.OID.ToString().Trim(), , strAfter) 
     Else 
      m_oCol.Add(New clsFeature(pFeature.OID, pFeature.ShapeCopy), pFeature.OID.ToString().Trim()) 
     End If 
    End If 

End Sub 

Public ReadOnly Property Count() As Short 
    Get 
     'Return the number of objects in m_oCol 
     Count = m_oCol.Count() 
    End Get 
End Property 

Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator 
    GetEnumerator = m_oCol.GetEnumerator 
End Function 

Public Sub Remove(ByRef vIndex As Object) 
    'Remove the specified object. Note here 
    'that this method will operate on either 
    'the index of the object we want removed 
    'or the key of the object we want removed 
    m_oCol.Remove(vIndex) 
End Sub 

Public Function Item(ByRef vIndex As Object) As clsFeature 
    'Retrieve the specified object. Note here 
    'that this method will operate on either 
    'the index of the object we want 
    'or the key of the object we want 
    Item = m_oCol.Item(vIndex) 
End Function 

Public Sub Clear() 
    'remove all objects from the private collection 
    m_oCol = New Collection 
    m_oColReverse = New Collection 
End Sub 

Public Function Reverse(ByRef val_Renamed As Object) As Boolean 
    Try 
     If m_oColReverse.Contains(val_Renamed) Then 
      Return True 
     Else 
      Return False 
     End If 
    Catch ex As Exception 
     If TypeOf ex Is ArgumentException Or TypeOf ex Is IndexOutOfRangeException Then 
      Reverse = False 
     End If 
    End Try 
End Function 

Public Function ContainsItem(ByRef val_Renamed As Object) As Boolean 
    Try 
     If m_oCol.Contains(val_Renamed) Then 
      Return True 
     Else 
      Return False 
     End If 

    Catch ex As Exception 
     If TypeOf ex Is ArgumentException Or TypeOf ex Is IndexOutOfRangeException Then 
      ContainsItem = False 
     End If 
    End Try 
End Function 

C#代碼 - 一旦我得到的鏈表正確的,我應該能夠完成剩下的

namespace NSTDB_QC_Utility 
{ 
public class clsFeature 
{ 
    private int m_OID; 
    private IGeometry m_Geometry; 

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

    public int OID 
    { 
     get { return m_OID; } 
    } 

    public IGeometry Geometry 
    { 
     get { return m_Geometry; } 
    } 
} 

public class clsFeatureCollection : System.Collections.IEnumerable 
{ 
    // used dictionary -> Should really use a linked list because of the strBefore and strAfter 
    // possible but need a way to handle m_ocol and strbefore - result was to reverse m_ocol on the strBefore 

    public LinkedList<clsFeature> m_oCol; 

//  public Dictionary<int, object> m_oCol; 
    public Dictionary<string, string> m_oColReverse; 

    public clsFeatureCollection() 
     : base() 
    { 
     m_oCol = new LinkedList<clsFeature>(); 

//   m_oCol = new Dictionary<int, object>(); 
     m_oColReverse = new Dictionary<string, string>(); 
    } 

    public IEnumerator GetEnumerator() 
    { 
     return m_oCol.GetEnumerator(); 
    } 
+2

堆棧溢出不是代碼轉換服務。你試過什麼了? –

+0

我明白這一點。我已經閱讀了很多例子..我想我無法獲得鏈接列表..以包含一個OID和幾何形狀文件。 OID將是一個Int並且幾何體將是一個對象 – Deke

+1

'LinkedList'不是特定於C#的,它是一個NET集合;或者你的意思是你試圖將VB代碼轉換爲C#(這不是所顯示的代碼似乎在做什麼) – Plutonix

回答

-1

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

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

VB「收藏」是一個奇怪的野獸它允許交替地將集合視爲鍵控列表或位置列表,以便單個.NET集合不會捕獲所有內容。此外,它基於1,與幾乎所有其他收藏不同。爲了更好地理解VB的'Collection'類型,我想出了以下內容 - 隨時使用它。請注意,它的目的是作爲插入式替換,因此它使用基於1的參數(yuk)。我將替換集合作爲一種改進 - 將'Collection'替換爲'Collection <object>'以獲得確切的VB等效值,但是您可能需要指定實際類型,即使您未在VB中執行該操作。

public class Collection<T> 
{ 
    private System.Collections.Generic.List<T> objects = new System.Collections.Generic.List<T>(); 
    private System.Collections.Generic.List<string> keys = new System.Collections.Generic.List<string>(); 

    public void Add(T newObject, string key = null, object before = null, object after = null) 
    { 
     if (after != null) 
     { 
      if (after as string != null) 
       Insert(newObject, keys.IndexOf(after as string) + 1, key); 
      else 
       Insert(newObject, (int)after, key); 
     } 
     else if (before != null) 
     { 
      if (before as string != null) 
       Insert(newObject, keys.IndexOf(before as string), key); 
      else 
       Insert(newObject, (int)before - 1, key); 
     } 
     else 
      Insert(newObject, objects.Count, key); 
    } 

    private void Insert(T newObject, int index, string key) 
    { 
     objects.Insert(index, newObject); 
     keys.Insert(index, key); 
    } 

    public void Clear() 
    { 
     objects.Clear(); 
     keys.Clear(); 
    } 

    public bool Contains(string key) 
    { 
     return keys.Contains(key); 
    } 

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

    public void Remove(string key) 
    { 
     RemoveAt(keys.IndexOf(key)); 
    } 

    public void Remove(int positionOneBased) 
    { 
     RemoveAt(positionOneBased - 1); 
    } 

    private void RemoveAt(int index) 
    { 
     objects.RemoveAt(index); 
     keys.RemoveAt(index); 
    } 

    public T this[int positionOneBased] 
    { 
     get 
     { 
      return objects[positionOneBased - 1]; 
     } 
    } 

    public T this[string key] 
    { 
     get 
     { 
      return objects[keys.IndexOf(key)]; 
     } 
    } 

    public System.Collections.Generic.IEnumerator<T> GetEnumerator() 
    { 
     return objects.GetEnumerator(); 
    } 
} 
+0

謝謝先生。我感謝你的努力 – Deke