Re-Dim an array inside a Type?

Jim Brown

Active Member
Licensed User
Longtime User
Is it possible to re-dim an array which exists in a type structure?
In my app the array size is unknown at the time and will need to be re-sized accordingly

As an example, I get the error "unknown type: thisarray" when I try to re-dim the array in this code:
B4X:
' Redim arrays inside type
Sub Process_Globals
End Sub

Sub Globals
   Type test(x As Int,y As Int,thisarray() As Float)
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim t1 As test
   t1.Initialize
   Dim t1.thisarray(100) As Float   ' <--- ERROR HERE
   t1.thisarray(0)=123.456
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Agraham's answer seems to universally fit all situations.

I wrestled with this question myself this morning, before I could find this great answer. My approach was to create a method in the class (test in your above example) which accepts a single integer parameter. The new method is simply a DIM of the array (thisarray in your above example). Just another way to do it. Both work. Agraham's approach is a bit tighter, while mine encapsulates it into the class, if you aspire to that. Mine may not be quite so tidy if there were lots of such member arrays.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Following a discussion on arrays vs list,maps - here is another "out of the box" solution: use a list instead of array.
 
Upvote 0
Top