Android Question (re) dimension an array within a type structure

Rusty

Well-Known Member
Licensed User
Longtime User
I have a type structure similar to this:
B4X:
Type Demo(thisarray(10) as int, thisstring as string)
Dim d as Demo
I would like to be able to dimension the "thisarray" dynamically within code similar to this:
B4X:
dim d.thisarray(20) as int
but when I try the above, it says
"Unknown type thisarray are you missing a library reference"

Is it possible to dynamically change dimensions on an array within a Type structure? If so, how?
Thanks,
Rusty
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Reassign the variable. This works for me:
B4X:
 ...
    Type MyType(MyArray() As Int, AnotherVar As String)
....

Sub Activity_Resume
    Dim poNew As MyType
    Dim piIndex As Int
   
    poNew.Initialize
    Dim poTestArray(10) As Int
    poNew.MyArray = poTestArray
   
    Log("Length is: " & poNew.MyArray.Length)
    For piIndex = 0 To poNew.MyArray.Length - 1
        poNew.MyArray(piIndex) = piIndex
    Next
   
    Dim AnotherArray(20) As Int
    poNew.MyArray = AnotherArray
    Log("Length now is: " & poNew.MyArray.Length)
    For piIndex = 0 To poNew.MyArray.Length - 1
        poNew.MyArray(piIndex) = piIndex * 2
    Next
End Sub
 
Upvote 0
Top