Android Question Array parameters

LucaMs

Expert
Licensed User
Longtime User
Typical beginner question 😄

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    Dim arr(10) As Int

    For i = 0 To 9
        arr(i) = i
    Next

    ' Why the first call is wrong and the second one not?
    LogArr(arr) ' <--- ERROR
    LogArr(Array(1,2,3))
End Sub

B4X:
Private Sub LogArr(Params() As Object)
    For i = 0 To Params.Length - 1
        Log(i & TAB & Params(i))
    Next
End Sub

Ok, the second call:
LogArr(Array(1,2,3))
implies Array As Object, so the two arrays are exactly the same type, but Params() As Object should accept an array of any type.
In fact the routine would work even if called passing an array of strings:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    Dim arr(10) As String

    For i = 0 To 9
        arr(i) = "a " & i
    Next

    LogArr(arr) ' <--- works
End Sub
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Testing more.

That routine does not accept arrays of other types, like custom classes, for example.

It accepts an array of Nodes, here, but... read the comments:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    ' layMain contains only one label and one button.
    MainForm.RootPane.LoadLayout("layMain")
 
    Dim arr(MainForm.RootPane.NumberOfNodes) As Pane
    Dim Index As Int = 0
    For Each n As Node In MainForm.RootPane.GetAllViewsRecursive
        arr(Index) = n ' <--- Only by setting a breakpoint here the project does not crash.
        Index = Index + 1
    Next
 
    LogArr(arr) ' <--- no warnings for cast exception!
End Sub

Private Sub LogArr(Params() As Object)
    For Each obj As Object In Params
        Log(GetType(obj))
    Next
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Post the error message.
For this one:
' Why the first call is wrong and the second one not?
LogArr(arr) ' <--- ERROR
LogArr(Array(1,2,3))
Main - 19: Cannot cast type: {Type=Int,Rank=1, RemoteObject=True} to: {Type=Object,Rank=1, RemoteObject=True}

For this one:
For Each n As Node In MainForm.RootPane.GetAllViewsRecursive
arr(Index) = n ' <--- Only by setting a breakpoint here the project does not crash.
java.lang.ClassCastException: javafx.scene.control.Button cannot be cast to javafx.scene.layout.Pane

Use B4XView instead of Node.
Use List instead of Array.
I know but I was trying something that needs an array of int (or any other type) and a routine which should receive it. The test with nodes was just one of the tests to check which array types the routine would accept without casting errors.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Despite that problem, I managed to develop a Function that compensates for the lack of "Redim Preserve", which is completely... useless 😄 (there is a simpler and more useful solution: using a List - although, in some rare cases, it is necessary to use an Array) .

But it was a good workout, anyway.

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    Dim arr(10) As Int

    For i = 0 To 9
        arr(i) = i + 1
    Next

    arr = modUtils.RedimPreserve(arr, 15)
    
    For i = 0 To arr.Length - 1
        Log(arr(i))
    Next
End Sub

Waiting for debugger to connect...
Program started.
1
2
3
4
5
6
7
8
9
10
0
0
0
0
0
 
Upvote 0
Top