Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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).


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? Thank You!

        Dim pEnumVar As IEnumVariantSimple, value As Object

        Dim pODGSLyr As New ODGSLayer

        '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

        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.


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

            '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)
share|improve this question
Looks like IDataStatistics.UniqueValues returns a System.Collections.IEnumerator. –  Kirk Kuykendall May 20 '11 at 16:15
@Kirk Kuykendall, well I changed "Dim pEnumVar As IEnumVariantSimple, value As Object" to "Dim pEnumVar As System.Collections.IEnumerator, value As Object" and "value = pEnumVar.Next" to "value = pEnumVar.MoveNext". I now get a Error a bit further past the original code that I posted. I will re-post a update (called "Update1") of where the new error is. thx! –  artwork21 May 20 '11 at 16:41
Check to see if pRowLyrs is Nothing, and handle accordingly. –  Kirk Kuykendall Jun 15 '11 at 18:35

1 Answer 1

I don't see any checks for null. From the error it appears that lngFldGroupTOCOrder is null.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.