VB 集合转换为 c# LinkedList

VB Collection Conversion to 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();
    }

VB 'Collection' 是一个奇怪的野兽,因为它允许将集合交替视为键控列表或位置列表,因此单个 .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();
    }
}

以下是我想出的最终可行解决方案。它与其他类似的集合集成。我最初使用了一个列表,但我需要一个字符串作为其他集合的键

// ************************** Ordered Dictionary - works ****************
// 
// 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 **********************