[futurebasic] Re: [FB] rect -> array

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : September 2004 : Group Archive : Group : All Groups

From: Alain Pastor <apastor@...>
Date: Fri, 03 Sep 2004 21:04:21 +0200
Adam Bell wrote:
> 
> myrect does not behave as an array, however, in that I cannot refer to 
> myrect(4) and get back the last (bottom) coordinate for the rect.
> 
> How then do I put these coordinates into an array without referring to 
> them specifically as myrect.bottom, etc.?

In addition to what Jay and Robert have written, I will add that you 
might benefit from a built-in autoXRef feature in FB.

When you pass a record variable to a local function, FB sends the 
address of the structure and not the values it contains. You can 
decide in the formal argument list of the function how you want to 
treat the address received. It might be a pointer to a rect 
structure ( Fn doSomething( rPtr As ^Rect ) ), but also an array of 
four integers ( Fn doSomething( r(3) As Int ) ) or why not an array 
of 2 points ( Fn doSomething( r(1) As Point ) ).
Note that the arrays defined like above are pointing to the actual 
rect structure that has been sent to the function, i.e. this is not 
a copy, if you alter any item in that "local" array you will change 
  the original value in rect.



Local Fn PrintRect( r(3) As Int )
   Print r(0), r(1), r(2), r(3)
End Fn

Local Fn PrintRectPoint( p(1) As Point )
   Print p(0), p(1)
End Fn


Dim r As Rect

SetRect( r, 10, 15, 20, 25 )
Fn PrintRect( r )
Fn PrintRectPoint( r )

Do
   HandleEvents
Until _nil


You might prefer to explicitly XRef the address received which is a 
more conventional way to write such code.

Local Fn PrintXRef( address As Ptr )
   XRef r(3) As Int   : r = address
   XRef p(1) As Point : p = address

   Print r(0), r(1), r(2), r(3)
   Print p(0), p(1)
End Fn

Dim r As Rect

SetRect( r, 10, 15, 20, 25 )
Fn PrintXRef( r )

Do
   HandleEvents
Until _nil


Alain