Arrays, UDF, and "by reference"

Started by galaara98, April 11, 2016, 12:56:21 PM

Previous topic - Next topic

galaara98

i passed an array to a UDF (by name)
i created another array from the first one (by assignment by name)
i redim'd the second array (NEEDED for a process i am running and NEED to not redim the first array because i need to search it)
the first Array Also seemed to be Redimensioned

#DefineFunction DoSumthing(anArray)
NumCols = ArrInfo(AnArray,2)
CurrentRow = ArrInfo(AnArray,1)
MaxRows = CurrentRow + 100

ArrayTWO = AnArray

ArrayRedim(ArrayTWO,MaxRows,NumCols)

;when i inspect AnArray, it has an additional 100 unInitialized rows
;this is bad, because in this function i run several binary ArraySearches against AnArray, and based on the results I add new Data to ArrayTwo

;how can I force, in this one circumstance, that ArrayTwo is a Copy and not a refernce

#EndFunction

td

Please read this

http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/Arrays+Array~copy~problem.txt

and this

http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/Arrays/Sort+Array~Sort~UDFs~from~Detlev.txt#012


You can also take the hack approach.  You can convert a WIL array to a COM variant array (safearray) with the ObjectType function.  You can then do a straight array variable assignment to make a copy of the safearray because safearrays are not assigned by reference like WIL arrays are.  Keep in mind, however, that that your safearray will loose its safearrayness when you pass it to the ArrayReDim function.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

A simple example:
Code (winbatch) Select
; Given the following
a1[1,1] = 1
a1[1,0] = 2
a1[0,1] = 3
a1[0,0] = 4

a2 = ObjectType("array",a1)
a3 = a2
ArrayReDim(a3, -1, 3) ; a3 becomes a WIL array of variants instead of a variant array.

strText = "'a1' has ":ArrInfo(a1,2):" columns":@Lf
strText := "'a2' has ":ArrInfo(a2, 2):" columns":@Lf
strText := "'a3' has ":ArrInfo(a3, 2):" columns"
Message("Number of Columns", strText )
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

galaara98

i was able to easily use the ObjectType and an ArrayRedim


;Copy two demensional Array (can be expended to intelligently copy any other array that VT ARRAY supports)
#DefineFunction CopyAnArray(AnArray)
        OrigDim1Size= ArrInfo(AnArray,1)
        OrigDim2Size= ArrInfo(AnArray,2)
   ArrayCopy = ObjectType( "ARRAY|BSTR", AnArray ) ; at this point the variable ArrayCopy is a ComObject
   ArrayRedim(ArrayCopy,OrigDim1Size,OrigDim2Size) ; at this point ArrayCopy is just a standard Winbatch Array
        Return ArrayCopy
#EndFunction

i see your doesn't bother with |BSTR, probably better idea


        This Created an independent copy of the array, in a very efficient manner
        and the results of the ArrayRedim converted the VT ARRAY|BSTR into a Winbatch Array


THANKS td, btw!!!!