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?

        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)
  • 4
    Looks like IDataStatistics.UniqueValues returns a System.Collections.IEnumerator. Commented May 20, 2011 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
    Commented May 20, 2011 at 16:41
  • 1
    Check to see if pRowLyrs is Nothing, and handle accordingly. Commented Jun 15, 2011 at 18:35
  • Did you ever find a solution to this. Very little feedback in this on the web. Commented Apr 2, 2016 at 17:30
  • If this issue persists, then I think it should be overhauled based on any learnings that have come out of the comments so far. It is not clear whether any attention has been paid to the only answer offered so far.
    – PolyGeo
    Commented Apr 4, 2016 at 7:03

1 Answer 1


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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