VB Collection to C#Conversion

时间:2015-08-27 15:40:31

标签: c# vb.net vb6-migration

我希望这是有道理的。

我有一个程序,我将VB6和VB.Net 2的组合转换为C#4。

我在VB中遇到集合问题,它们是KeyValuePair。我有三个系列:

m_oCol(string, clsFeatureObjectCollection) which contains clsFeatureObject<br />
m_oCol(string, clsFeatureObject) which contains clsFeatureCollection<br />
m_oCol(string, clsFeatureCollection) which contains clsFeature

在C#中,我将包含clsFeature的m_oCol(string,clsFeatureCollection)转换为列表,因为我需要clsFeature来保存以特定顺序插入的特征以进行精确处理,即键可以是1,3,2,4,5。

我转换成字典的其他人但我不确定这是否正确,但我不确定还有什么用。

这些都应该是字典,因为当我访问一个集合中包含所有三个集合的集合时,我当前会收到强制转换错误。

我想我要问的是这些收藏品应该是同一类型吗?名单?字典?或者是其他东西?关键因素是必须按特定顺序插入clsFeature,即键可以是1,3,2,4,5。似乎我只能在列表中获取它以保存订单

感谢任何建议

clsFeatureCollection.vb

Option Strict Off
Option Explicit On

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

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

Private Sub Class_Terminate_Renamed()
    'Set up the collection
    m_oCol = Nothing
    m_oColReverse = Nothing
End Sub

Protected Overrides Sub Finalize()
    Class_Terminate_Renamed()
    MyBase.Finalize()
End Sub
End Class

1 个答案:

答案 0 :(得分:1)

您有几种不同的选择。

  1. List<KeyValuePair<string, clsFeatureObjectCollection>>
    如果需要重复键,请使用此选项,快速查找并不重要O(n)和插入顺序需要保留。
  2. Dictionary<string, clsFeatureObjectCollection>
    如果键是唯一的,则使用此选项,您需要快速查找O(1)并且顺序无关紧要。
  3. SortedDictionary<string, clsFeatureObjectCollection>
    如果键是唯一的,则使用此选项,您需要快速查找O(log n),并且项目的顺序应该跟随键。 (或者你提供的任何比较器)
  4. SortedList<string, clsFeatureObjectCollection>
    与SortedDictionary相似。请参阅What's the difference between SortedList and SortedDictionary?进行比较。