PDA

View Full Version : redim byref passed array-variable



ReneMiner
24-01-2015, 10:12
i want to know something :D

see the small example:


Uses "console"

Function fTest( ByRef a(), ByVal newDims As Long ) As Long

' this function shall redim some array a and return new ubound

ReDim Preserve a(newDims)

Function = UBound(a)

End Function


Dim foo() As Long ' the real, global array

PrintL Str$( fTest( foo, 123) )

PrintL "now:" & UBound(foo) ' i want it to print 123 too :(


WaitKey


thinBasic itself has a few functions that can do this as Dir_ListArray or Parse - where the passed variable to redim is mandatory a string.
But is it possible from script-side to do similar and redim any array passed by user like that?

Any ideas- hints?

Petr Schreiber
24-01-2015, 10:29
It is simple, just specify the type in the generic array parameter:


Uses "console"

Long laurel() ' the global array

Function TBMain()

Long hardy() ' the local array

PrintL fTest( laurel, 128)
PrintL "now:" & UBound(laurel)
PrintL
PrintL fTest( hardy, 256)
PrintL "now:" & UBound(hardy)

WaitKey

End Function

Function fTest( ByRef a() As Any, ByVal newDims As Long ) As Long

ReDim Preserve a(newDims)
Return UBound(a)

End Function



Petr

ReneMiner
24-01-2015, 11:08
Thank you :)