0

I would like to use IDataStatistics:UniqueValues to iterate through a set of unique values. This worked flawlessly in VB6 and VBA but I am having a difficult time making it work in VS 2013, ArcGIS 10.3, targeting .Net 3.5.

I am following the example shown here

Debugging InvalidCastException was unhandled by user code error from ArcObjects DataStat? is a similar post with no resolution. I've already tried the suggestions provided there to no avail.

The error I get always occurs on the pEnumVar = CType(pData.UniqueValues, IEnumVariantSimple) line with Err.Description: "Unable to cast object of type 'System.Runtime.InteropServices.CustomMarshalers.EnumeratorViewOfEnumVariant' to type 'ESRI.ArcGIS.esriSystem.IEnumVariantSimple'."

I would really like to make this work rather than having to nest my loop to get the unique values the long way...

Here my code:

'pField As IField, pSelectionSet As ISelectionSet2 passed in 

Dim pData As IDataStatistics = New DataStatistics 
Dim pStatResults As IStatisticsResults
Dim pEnumVar As IEnumVariantSimple 
Dim value As Object

... ...

pSelectionSet.Search(Nothing, False, pCursor) 
pData = New DataStatistics
pData.Field = pField.Name 
pData.Cursor = pCursor
EnumVar = CType(pData.UniqueValues, IEnumVariantSimple) 
value = pEnumVar.Next
    Do Until IsDBNull(value)    ...     ...
2

Looks like IDataStatistics.UniqueValues returns a System.Collections.IEnumerator

Frankly, I've found it easier to just use a search cursor and populate a Generic Dictionary than to deal with IDataStatistics.

  • I know this question relates to 10.3 but its worth mentioning that there was a bug introduced by ESRI in a service pack for 10.1 which broke this interface and I too ended up just using cursors and have stuck with them, although I think ESRI should have resolved the issue by now. – Hornbydd Apr 3 '16 at 11:42
1

I was incorrect in saying that no suggestion in THIS post worked. Over the last decade I learned to always seriously consider anything mr. @Kirk Kuykendall posts with extra attention. The credit for this answer should go to him but since the OP didn't mark the original question as answered I will do it here and if Kirk can just copy his comment here and make it an answer I will mark his as the correct answer.

Turns out the problem is not with IDataStatistics but with the IEnumVariantSimple enumerator. I was able to confirm that although IDataStatistics:UniqueValues should return an IEnumVariantSimple type enumerator I was under certain situations able to crash it with a "this Type is not supported" error. Kirk stated "Looks like IDataStatistics.UniqueValues returns a System.Collections.IEnumerator" (NOT IEnumVariantSimple) Once I changed the enumerator type I was able to make it work. This system enumerator works differently but it is perfectly usable:

    Dim pData As IDataStatistics = New DataStatistics
    Dim pStatResults As IStatisticsResults
    Dim pEnumVar As IEnumVariantSimple
    Dim pEnum As System.Collections.IEnumerator
    Dim value As Object

    ...
    ...

    pSelectionSet.Search(Nothing, False, pCursor)
    pData = New DataStatistics
    pData.Field = pField.Name
    pData.Cursor = pCursor
    pEnum = CType(pData.UniqueValues, System.Collections.IEnumerator)
    pEnum.Reset()
    pEnum.MoveNext()
    value = pEnum.Current

    Do Until value Is Nothing
        ...
        ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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