Android Question How to fix error with 2D-array ?

Volga_

Member
Hello,
I have problem with 2D-array in B4A when I compile this code:
2D-Array::
Dim s(,) As String
s=Array As String(Array As String("1","2","3"), Array As String("3","4","5"))
Compiler shows error:
Error::
Error description: Cannot cast type: {Type=String,Rank=1, RemoteObject=True} to: {Type=String,Rank=2, RemoteObject=True}
How to fix this? Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You cannot use Array keyword to directly create 2d arrays.

You can do something like this:
B4X:
Sub AppStart (Args() As String)
    Dim mat(0, 0) As String = Create2DArrayOfStrings(Array( _
        Array As String("a", "b", "c", "d"), _
        Array As String("aa", "bb", "cc", "dd"), _
        Array As String("aaa", "bbb", "ccc", "ddd")))
    Log(mat(0, 2))
    Log(mat(1, 3))
End Sub


Private Sub Create2DArrayOfStrings (Rows() As Object) As String(,)
    Dim FirstRow() As String = Rows(0)
    Dim result(Rows.Length, FirstRow.Length) As String
    For r = 0 To Rows.Length - 1
        Dim row() As String = Rows(r)
        For c = 0 To FirstRow.Length - 1
            result(r, c) = row(c)
        Next
    Next
    Return result
End Sub
 
Upvote 0
Top