Question

I have a series of API calls that are returning J# data types. I have been able to convert most of the data types (Integer, Boolean, Double, Float, etc) just fine.

What I need to do now is convert a java.Util.Collection to a VB .NET collection (ArrayList?)

Here is my attempt:

Public Function MakeDotNETCollection(ByVal javaCol As java.util.Collection) As Collection

    Dim dotNetCol As Collection

    If Not javaCol Is Nothing Then
      dotNetCol = New Collection

      Dim it As IEnumerator = javaCol.iterator()

      For Each it In CType(javaCol, Collection)
        dotNetCol.Add(it.Current)
      Next it

    End If

    Return dotNetCol
  End Function

I keep getting a runtime error "Unable to cast object of type 'AbstractListlistIterator' to type 'System.Collections.IEnumerator. Any ideas?

Was it helpful?

Solution

Not tested, but should work:

Public Function MakeDotNETCollection(ByVal javaCol As java.util.Collection) As Collection

  Dim dotNetCol As Collection

  If Not javaCol Is Nothing Then
    dotNetCol = New Collection

    Dim it As java.util.Iterator = javaCol.iterator()

    While it.HasNext()
      dotNetCol.Add(it.next())
    End While

  End If

  Return dotNetCol

End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top