I have some code that adds a bunch of group layers into ArcMap based on a table within ArcMap. The code works fine in VBA, however in VS 2010 I get an error on the "pEnumVar = pDataStat.UniqueValues" line (see code below).
ERROR:
InvalidCastException was unhandled by user code
Unable to cast object of type 'System.Runtime.InteropServices.CustomMarshalers.EnumeratorViewOfEnumVariant' to type 'ESRI.ArcGIS.esriSystem.IEnumVariantSimple'.
I'm guessing .NET does not like to use the IEmumVariantSimple object? Does anyone have a suggestion of how I can work around this or update this line with a different enumeration object?
Dim pEnumVar As IEnumVariantSimple, value As Object
Dim pODGSLyr As New ODGSLayer
'Start
'Find the Layer Files metadata table
pStdTableColl = m_map
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 '<<<<<<<<<< ERROR HERE
value = pEnumVar.Next
Update 1
Update 1 is an update from @Kirk Kuykendall comment. The new error is on the "lngGroupTOCOrder = pRowLyrs.Value(lngFldGroupTOCOrder)" line (see code below). I'm not sure what I need to do next.
ERROR:
NullReferenceException ws unhandled by user code Object reference not set to an instance of an object.
pEnumVar = pDataStat.UniqueValues
value = pEnumVar.MoveNext
Do Until IsNothing(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) '<<<<<<< NEW ERROR
blnGroupVis = pRowLyrs.Value(lngFldGroupVis)
No comments:
Post a Comment