Main Menu

User Types and Functions

Started by tide, October 07, 2007, 07:32:51 PM

Previous topic - Next topic

tide

I want to use a function to initialize the values in a user type. However, after initializing one variable with the function, its values are overwritten with any subsequent call to the initializing function. Here's sample code:

// start
Type thing
   x#, y#
EndType
   
Dim a As thing
Dim b As thing

a = init(1, 2)
b = init(3, 4)

Print "The following pair of numbers should be 1 and 2"
Print a.x
Print a.y

Print " "
Print "The following pair of numbers should be 3 and 4"
Print " "
Print b.x
Print b.y

WaitKey

Function init(xx#, yy#)
Dim temp As thing
   temp.x = xx#
   temp.y = yy#
EndFunction temp

//end

Once "a" is initialized any subsequent call to init() should have no effect on it but it does.

kevin

 You can't return 'Temp' like that.


Type thing
   x#, y#
EndType
   
Dim a As thing
Dim b As thing

init(a(),1, 2)
init(b(),3, 4)

Print "The following pair of numbers should be 1 and 2"
Print a.x
Print a.y

Print " "
Print "The following pair of numbers should be 3 and 4"
Print " "
Print b.x
Print b.y

Sync
WaitKey


Function init(me.thing,xx#, yy#)
   me.x# = xx#
   me.y# = yy#
EndFunction



tide