LucaMs Expert Licensed User Longtime User Sep 17, 2020 #1 Typical beginner question ? B4X: Copy 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: Copy 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: Copy 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: Sep 17, 2020
Typical beginner question ? B4X: Copy 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: Copy 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: Copy 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
LucaMs Expert Licensed User Longtime User Sep 17, 2020 #2 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: Copy 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
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: Copy 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
Erel B4X founder Staff member Licensed User Longtime User Sep 17, 2020 #3 Post the error message. Use B4XView instead of Node. Use List instead of Array. Upvote 0
LucaMs Expert Licensed User Longtime User Sep 17, 2020 #4 Erel said: Post the error message. Click to expand... For this one: LucaMs said: ' Why the first call is wrong and the second one not? LogArr(arr) ' <--- ERROR LogArr(Array(1,2,3)) Click to expand... Main - 19: Cannot cast type: {Type=Int,Rank=1, RemoteObject=True} to: {Type=Object,Rank=1, RemoteObject=True} For this one: LucaMs said: For Each n As Node In MainForm.RootPane.GetAllViewsRecursive arr(Index) = n ' <--- Only by setting a breakpoint here the project does not crash. Click to expand... java.lang.ClassCastException: javafx.scene.control.Button cannot be cast to javafx.scene.layout.Pane Erel said: Use B4XView instead of Node. Use List instead of Array. Click to expand... 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
Erel said: Post the error message. Click to expand... For this one: LucaMs said: ' Why the first call is wrong and the second one not? LogArr(arr) ' <--- ERROR LogArr(Array(1,2,3)) Click to expand... Main - 19: Cannot cast type: {Type=Int,Rank=1, RemoteObject=True} to: {Type=Object,Rank=1, RemoteObject=True} For this one: LucaMs said: For Each n As Node In MainForm.RootPane.GetAllViewsRecursive arr(Index) = n ' <--- Only by setting a breakpoint here the project does not crash. Click to expand... java.lang.ClassCastException: javafx.scene.control.Button cannot be cast to javafx.scene.layout.Pane Erel said: Use B4XView instead of Node. Use List instead of Array. Click to expand... 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.
Erel B4X founder Staff member Licensed User Longtime User Sep 17, 2020 #5 Arrays of primitives cannot be casted to arrays of objects. Upvote 0
LucaMs Expert Licensed User Longtime User Sep 17, 2020 #6 Erel said: Arrays of primitives cannot be casted to arrays of objects. Click to expand... Wonderful ?. Ok. Upvote 0
Erel said: Arrays of primitives cannot be casted to arrays of objects. Click to expand... Wonderful ?. Ok.
LucaMs Expert Licensed User Longtime User Sep 17, 2020 #7 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: Copy 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
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: Copy 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