Page 1 of 1

Passing an objref array by reference to a function

Posted: Tue Aug 02, 2005 4:02 pm
by miller
Hi!

Does anybody know how I can pass an objref array by reference to a function?

I want to do something like:

// begin
func foo(){localobj arr
arr = $o1
arr[0]=new Vector(10)
arr[1]=new Vector(20)
return ERR_OK
}

objref arr[2]

foo(arr)
// end

Thanks

Posted: Tue Aug 02, 2005 9:03 pm
by ted
I've always found arrays to be limiting, because no matter how many objects
I think I need today, sure enough, tomorrow I need a different number. Lists
are a far more flexible way to deal with multiple objects, even objects of
different classes (although this simple example sticks with objects of the
same kind).

Code: Select all

objref bah

bah  // will print NULLobject

proc foo() { local ii
  $o1 = new List()
  for ii=0,$2-1 $o1.append(new Vector())
}

foo(bah,3)

bah  // will print List...
bah.count()  // shows it contains objects
for jj=0,bah.count()-1 print bah.object(jj)  // says what they are

Posted: Wed Aug 03, 2005 3:35 am
by miller
Yes, I know, it can be done with a list. But isn't there also a possibility to do it with an array?
I write the function for an existing program, that already uses arrays and I would like not to modify it much.

Posted: Wed Aug 03, 2005 11:20 am
by hines
There is no direct way to pass an objref array to a function. Probably the least violent transformation to existing code is to make use of a wrapper object as is done with strdef.
The every occurrence of an array variable becomes variable.o and instead of declaring

Code: Select all

objref variable[n]
one declares

Code: Select all

objref variable
variable = new ObjectArray(n)
where

Code: Select all

begintemplate ObjectArray
public o, n
objref o
proc init() {
     n=$1
     objref o[n]
}
endtemplate ObjectArray