Class cClass
End Class
Class cClassSub
Inherits cClass
Public pText As String
End Class
Function Test() As cClass()
Var oEssais() As cClassSub
Var vVariant As Variant
Var vSuccess As Boolean
vVariant = oEssais
If vVariant IsA cClass Then
vSuccess = True // No
Else
vSuccess = False // Yes…
End If
// Return oEssais : Works
Return vVariant // Fails
End Function
If my array was not empty, it would work as intended, but with an empty array, the IsA and the Return fails. Is this a bug, or should I do a feature request?
K. VarType() will not help me, I know it’s an Array. I want to know the type of elements the array contains.
How can I know that? Should I use the Introspection?
Also, my problem is also with the Return.
Why the Variant doesn’t convert?
Function Test() As cEssais()
Var oEssais(), vEssai As cEssaisSousClasse
Var vVariant As Variant
Var vRes As Boolean
vVariant = oEssais
'Return vVariant // Fails
vEssai = New cEssaisSousClasse()
vRes = vEssai IsA cEssais
// vRes = True
oEssais.Add(vEssai)
vVariant = oEssais
'Return vVariant // Fails
Return oEssais // Works
End Function
Class MySuperClass
End Class
Class MySubClass1
Inherits MySuperClass
End Class
Class MySubClass2
Inherits MySuperClass
End Class
Public Function MyArrayOfSubClasses() As MySuperClass()
Var myArraysSubs() As MySuperClass // Array of SuperClass
myArraysSubs.Add New MySubClass1 // Add One Sub
myArraysSubs.Add New MySubClass2 // Add Another Sub
Return myArraysSubs // Return the array
End Function
It just doesn’t work that way in Xojo due to the way it handles Arrays - you can’t typecast an Array in any meaningful way, so there’s no way to return subClass() when the return type is superClass(). Arrays, no matter what the type of their contents, are always type peers and never sub- or super-classes of each other.
You’ll have to convert your array of subclass objects like this:
dim resultArray() as superClass
for each currentSubClassObject in subClassObjects()
resultArray.Add superClass(currentSubClassObject)
next
return resultArray
My sample does what he asked. Return an array of superclass with subclass objects (that can differ)
Later he can identify each content as
Var arrayReturned() As MySuperClass = MyArrayOfSubClasses() // get array
For Each item As MySuperClass in arrayReturned
If item IsA MySubClass1 Then
System.DebugLog "Received a MySubClass1"
ElseIf item IsA MySubClass2 Then
System.DebugLog "Received a MySubClass2"
End
Next
Break
But you interrupted an answer to introduce a noise, probably leading to error, or at least misunderstandings, as I can’t understand what you wrote in a positive way until now. So I couldn’t keep quiet seeing such thing, then showed the use of what, for me, you said it was impossible.
I don’t think (and Xojo at runtime) that an empty array is null…
Function Test() As cEssais()
Var oEssais(), vEssai As cEssaisSousClasse
Var vVariant As Variant
Var vRes As Boolean
vEssai = New cEssaisSousClasse()
vRes = vEssai IsA cEssais
// vRes = True
oEssais.Add(vEssai)
vVariant = oEssais
oEssais = vVariant
vRes = vVariant.IsNull()
// vRes = False
vRes = vVariant.IsArray()
// vRes = True
Return vVariant // Fails
Return oEssais // Works
End Function
After line vRes = vVariant.IsNull(), vRes is False.
After line vRes = vVariant.IsArray(), vRes is True.
And this line doesn’t do errors, so it proves that the conversion is possible: oEssais = vVariant
My real problem here is this line at the end: Return vVariant // Fails
I was not clear, since I had 3 different issues in this post. So I will them post separately so it’s clearer. My problem is that I have to return a Variant, and if it’s a Variant containing an empty array, it generates a runtime error, but if the conversion is done when not through the return statement then it works…
Also, see the debugger pane, Xojo knows at runtime that it’s an empty array of a type subclass…