Converting Object Array Variant to Array of Variants

How do you convert a Variant Object Array to an array when you don’t know the type of Object? All I get are TypeMismatch exceptions!

[code] Dim dDicts() As Dictionary
Dim vVariant As Variant

For i As Integer = 0 to 5
dDicts.Append New Dictionary
Next

vVariant = dDicts

//How to get an array if the Variant.Type= Object?!?!?!?!
If vVariant.IsArray Then
Select Case vVariant.ArrayElementType
Case Variant.TypeObject
Dim xFinalValues() As Variant
Dim xObject() As Object = vVariant //Type MisMatch
For i As Integer = 0 to uBound(xObject)
Dim vThisValue As Variant = xObject(i)
xFinalValues.Append vThisValue
Next
End Select
End If[/code]

The only way I can possibly conceive to do this is if you did a try/catch statement for each possible variant type.

Here’s what I’m doing now which seems horribly inefficient:

[code]
Function ArrayValueKSW(Extends theVar As Variant) As Variant()
Dim xValues() As Variant
Dim i, iEnd As Integer

If theVar.IsNull Then Return xValues

Select Case theVar.ArrayElementType
Case Variant.TypeNil
Return xValues
Case Variant.TypeBoolean
Dim bValues() As Boolean = theVar
iEnd = uBound(bValues)
For i = 0 to iEnd
xValues.Append bValues(i)
Next
Case Variant.TypeColor
Dim cValues() As Color = theVar
iEnd = uBound(cValues)
For i = 0 to iEnd
xValues.Append cValues(i)
Next
Case Variant.TypeDate
Dim dValues() As Date = theVar
iEnd = uBound(dValues)
For i = 0 to iEnd
xValues.Append dValues(i)
Next
Case Variant.TypeDouble
Dim dValues() As Double = theVar
iEnd = uBound(dValues)
For i = 0 to iEnd
xValues.Append dValues(i)
Next
Case Variant.TypeInteger
Dim iValues() As Integer = theVar
iEnd = uBound(iValues)
For i = 0 to iEnd
xValues.Append iValues(i)
Next
Case Variant.TypeLong
Dim iValues() As Int64 = theVar
iEnd = uBound(iValues)
For i = 0 to iEnd
xValues.Append iValues(i)
Next
Case Variant.TypeSingle
Dim iValues() As Single = theVar
iEnd = uBound(iValues)
For i = 0 to iEnd
xValues.Append iValues(i)
Next
Case Variant.TypeString
Dim sValues() As String = theVar
iEnd = uBound(sValues)
For i = 0 to iEnd
xValues.Append sValues(i)
Next
Case Variant.TypeObject
Dim has_found As Boolean
#pragma BreakOnExceptions False

If Not has_found then
  Try
    Dim xObject() As Object = theVar
    For i = 0 to uBound(xObject)
      Dim vTest As Variant = xObject(i)
      If vTest.IsArray Then xValues.Append vTest.ArrayValueKSW() Else xValues.Append vTest
    Next
    has_found = True
  Catch xNotRight As TypeMismatchException
    //Ignore
  End Try
End If

If Not has_found then
  Try
    Dim xObject() As Variant = theVar
    For i = 0 to uBound(xObject)
      Dim vTest As Variant = xObject(i)
      If vTest.IsArray Then xValues.Append vTest.ArrayValueKSW() Else xValues.Append vTest
    Next
    has_found = True
  Catch xNotRight As TypeMismatchException
    //Ignore
  End Try
End If

If Not has_found then
  Try
    Dim dDicts() As Dictionary = theVar
    For i = 0 to uBound(dDicts)
      xValues.Append dDicts(i)
    Next
    has_found = True
  Catch xNotRight As TypeMismatchException
    //Ignore
  End Try
End If

If Not has_found Then Break
#pragma BreakOnExceptions True

Else
Break
End Select

Return xValues
End Function[/code]

I have GetVariantArrayUboundMBS and GetVariantArrayValueMBS in our plugin so you don’t need to cast objects to all the types you may have.

YOu see that in our Web Starter Kit.