B4J Question IIF error when converting string array to list

Chris2

Active Member
Licensed User
Longtime User
Can someone please tell me why in the following the If...Else...EndIF works OK, but the Inline IIf fails or at least seems to produce an array rather than a list:

B4X:
Private Sub Button1_Click
   
    Private b As Boolean
   
    'This works OK.
    If b Then
        Dim colours As List = Array As String(xui.Color_Black, xui.Color_Black)
    Else
        Dim colours As List = Array As String(xui.Color_Blue, xui.Color_Blue)
    End If
    Log(colours)    '(ArrayList) [-16776961, -16776961]
           
           
'    But here colours seems to end up as an array rather than a list, or sometimes I get
'    an exception: java.lang.ClassCastException:
'    class [Ljava.lang.String; cannot be cast to class java.util.List ([Ljava.lang.String; and java.util.List are in module java.base of loader 'bootstrap')
    Dim colours As List = IIf(b, Array As String(xui.Color_Black, xui.Color_Black), Array As String(xui.Color_Blue, xui.Color_Blue))
    Log(colours)    '(String[]) [Ljava.lang.String;@3fb4917d
   
End Sub

Changing the string arrays to Int arrays still fails in a similar way.
Many thanks.
 

Daestrum

Expert
Licensed User
Longtime User
B4X:
	Dim colours As List = IIf(b,make_colours(xui.Color_Black), make_colours(xui.Color_Blue))
	Log(colours)   

End Sub
Sub make_colours(i As Int) As List
	Return Array As String(i,i)
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Something to do with the intermediate casting from IIf.

This works:
B4X:
Dim CArr() As Object = IIf(b, Array(XUI.Color_Black, XUI.Color_Black), Array(XUI.Color_Blue, XUI.Color_Blue))
    Dim colours As List = CArr
    Log(colours)
 
Upvote 0
Top