PDA

View Full Version : Problem Returning an array from a method (resolved)



Greg LaPolla
07-07-2009, 06:34 AM
Here is the method




PUB Mget | R[10], X

meter.Init(RC_PIN, HL_PIN, CAP_VAL, COR_FACTR)
output.tx(output#CLS)
waitcnt(clkfreq + cnt)
repeat until ina[24] == 1
R[0] := meter.Resistance / 1066
waitcnt(clkfreq/10 + cnt)
output.tx(output#HOME)
output.Str(String("Steps = "))
output.Str(num.decf(R[0],3))
output.tx(output#CLREOL)

repeat X from 1 to R[0]
output.tx(output#CLS)
waitcnt(clkfreq + cnt)
repeat until ina[24] == 1
R[X] := meter.Resistance / 9
waitcnt(clkfreq/10 + cnt)
output.tx(output#HOME)
output.Str(String("Step "))
output.Str(num.decf(X,1))
output.Str(String(" = "))
output.Str(num.decf(R[X],3))
output.tx(output#CLREOL)

Result := @R




calling this method as follows

myVals := Mget

myVals is declared as long myVals[10]

mVals[X] is always corrupted. Is this not the correct way to return an array ? or is it not possible at all ?

I have even tried accessing as long[myVals][X] that doesn't seem to work either.

Any Ideas or thoughts on this ?


Thanks

Greg

Post Edited (Greg LaPolla) : 7/7/2009 1:24:46 AM GMT

Phil Pilgrim (PhiPi)
07-07-2009, 06:54 AM
R will have to be a VAR variable for this to work. Local variables reside in the stack and effectively disappear once the method that hosts them returns.

-Phil

SamMishal
07-07-2009, 08:40 AM
The way you should do this is by declaring MyVals as an array in the Vars section of the top object
e.g.
Var
иииииииииииииии Long MyVals[10]
и
The MGet method should be declared like this
Pub MGet(ArrayAddress)
иииииииииииииии ‘some code
иииииииииииииии ‘code to fill array elements (maybe in a loop)
иииииииииииииии Long[ArrayAddress][i] := someexpression
и
Then when you call the method use this
иииииииииииииии MGet(@MyVals)


By the way if you want the type could be Word or Byte.....

Sam
и

Greg LaPolla
07-07-2009, 09:22 AM
Phil and Sam,

Thanks for the input the problem is resolved.