Passing an objref array by reference to a function

The basics of how to develop, test, and use models.
Post Reply
miller

Passing an objref array by reference to a function

Post 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
ted
Site Admin
Posts: 5784
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post 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
miller

Post 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.
hines
Site Admin
Posts: 1600
Joined: Wed May 18, 2005 3:32 pm

Post 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
Post Reply