VB6到VB.NET的转换 - System.Collections.IEnumerator

时间:2011-06-01 18:59:29

标签: vb.net visual-studio-2010 vb6-migration arcgis arcobjects

我有一个相当长的代码块,我正在尝试从VB6转换为VB.NET。 ArcObjects GIS代码基本上查看表格并将一堆GIS图层组合在一起,并将它们添加到ArcMap内容列表中。在Visual Studio 2010中测试时,我遇到了这条线的问题:

VB6这一行:

Dim pEnumVar As IEnumVersionInfo, value As Varient 

添加我被告知需要将其转换为:

Dim pEnumVar As System.Collections.IEnumerator, value As Object  'I also tried value as String

另外,我不得不改变这一行(4x):

value = pEnumVar.Next

对此:

value = pEnumVar.Current 'and I tried this value = pEnumVar.MoveNext

VB6版本“value”返回一个字符串,.NET版本“value”返回“”或null。如何获得返回字符串的“值”?这是长代码。

由于

        Dim pLayer As ILayer
        Dim pGrpLayer As IGroupLayer

        Dim pStdTableColl As IStandaloneTableCollection
        Dim pStdTable As IStandaloneTable = Nothing
        Dim pTable As ITable = Nothing
        Dim pTableSort As ITableSort
        Dim pTblSortLyrs As ITableSort
        Dim pRowLyrs As IRow
        Dim pDataStat As IDataStatistics
        Dim pCursor As ICursor
        Dim pLyrCursor As ICursor
        Dim pQf As IQueryFilter

        Dim lngFldLayerName As Long
        Dim lngFldPath As Long
        Dim lngFldGroupOrder As Long
        Dim lngFldGroupName As Long
        Dim lngFldGroupTOCOrder As Long
        Dim lngFldGroupVis As Long
        Dim i As Integer

        Dim strLayerName As String
        Dim strPath As String
        Dim lngGroupTOCOrder As Long
        Dim blnGroupVis As Boolean

        Dim pEnumVar As IEnumVersionInfo, value As Object ' <<<<<<<<<<<<<<<<<<

        Dim pODGSLyr As New ODGSLayer

        'Start
        'Find the Layer Files metadata table
        pStdTableColl = m_pMap
        For i = 0 To pStdTableColl.StandaloneTableCount - 1
            pStdTable = pStdTableColl.StandaloneTable(i)
            If pStdTable.Name = "ODGSLAYERS" Then
                pTable = pStdTable
                lngFldLayerName = pTable.FindField("LAYERNAME")
                lngFldPath = pTable.FindField("PATH")
                lngFldGroupOrder = pTable.FindField("GROUPORDER")
                lngFldGroupName = pTable.FindField("GROUPNAME")
                lngFldGroupTOCOrder = pTable.FindField("GROUPTOCORDER")
                lngFldGroupVis = pTable.FindField("GROUPVISABLE")
            End If
        Next i

        If pStdTable Is Nothing Then
            Exit Sub
        End If

        'Sort the Table
        pTableSort = New TableSort
        With pTableSort
            .Fields = "GROUPTOCORDER, GROUPNAME"
            .Ascending("GROUPTOCORDER") = True
            .Ascending("GROUPNAME") = True
            .QueryFilter = Nothing
            .Table = pTable
        End With
        pTableSort.Sort(Nothing)

        pCursor = pTableSort.Rows

        'Find Unique Values in the Table
        pDataStat = New DataStatistics
        pDataStat.Field = "GROUPNAME"
        pDataStat.Cursor = pCursor

        pEnumVar = pDataStat.UniqueValues
        value = pEnumVar.Current ' <<<<<<<<<<<<<<<<<<<<<<<<<<

        Do Until IsDBNull(value)
            'Now resort the table based upon the layer order in the group
            pQf = New QueryFilter
            pQf.WhereClause = "[GROUPNAME] = '" & value & "'"
            pLyrCursor = pTable.Search(pQf, False)
            pTblSortLyrs = New TableSort
            With pTblSortLyrs
                .Fields = "GROUPORDER"
                .Ascending("GROUPORDER") = True
                .QueryFilter = pQf
                .Table = pTable
            End With
            pTblSortLyrs.Sort(Nothing)

            'Get the newly sorted rows and create the new Group and Layers inside the Group
            pLyrCursor = pTblSortLyrs.Rows
            pRowLyrs = pLyrCursor.NextRow

            'Create the new Group
            lngGroupTOCOrder = pRowLyrs.Value(lngFldGroupTOCOrder)
            blnGroupVis = pRowLyrs.Value(lngFldGroupVis)

            pGrpLayer = New GroupLayer
            pGrpLayer.Visible = blnGroupVis
            pGrpLayer.Expanded = False
            pGrpLayer.Name = pRowLyrs.Value(lngFldGroupName)

            'Add layers to the new Group
            Do Until pRowLyrs Is Nothing
                strLayerName = pRowLyrs.Value(lngFldLayerName)
                strPath = pRowLyrs.Value(lngFldPath)

                pODGSLyr = New ODGSLayer
                pLayer = pODGSLyr.LoadLayer(strPath, strLayerName)
                pGrpLayer.Add(pLayer)
                pRowLyrs = pLyrCursor.NextRow
            Loop

            'Add the Group layer to the map
            m_pMap.AddLayer(pGrpLayer)
            m_pMap.MoveLayer(pGrpLayer, lngGroupTOCOrder)

            '        Debug.Print "value - " & value & vbTab & "GroupVis = " & blnGroupVis
            value = pEnumVar.Current ' <<<<<<<<<<<<<<<<<<

        Loop

1 个答案:

答案 0 :(得分:3)

迭代一个对象看起来像这样:

While iterator.MoveNext() Do
  val=iterator.Current

  'do work with val here
End While

你正在做的是在没有先移动迭代器的情况下调用Current(然后无缘无故地调用Current)。

顺便说一句,整个混乱相当于:

For Each val in list 'or whatever the source object is
  ' use val
Next