Array Access Help
Okay, I'm butting my head up against the wall trying to provide a public function which returns a value based on an index passed in - the index is an index into an array. The code works fine when I use an actual constant as the index to return a value from the array but as soon as I try to use the value passed in I get a very large number, and it's the same regardless of what index I'm using.
This is a public function in an object that I created.
Here is some sample code that actually works:
And here is code that does NOT work
Here is how I delcare inputValues:
I really don't want to have to put a case statement in there to return the values directly and the array access should work with a variable - or maybe I missed something in the manual, but there are other cases where I use a variable. In fact, in the same object causing problems I have an array of indices into another array (because I couldn't figure out how to do multi-dimensional arrays). TIA
This is a public function in an object that I created.
Here is some sample code that actually works:
' **************************************************************** ' * GetInputValue * ' * Get previously stored value * ' **************************************************************** PUB GetInputValue(index) return inputValues[noparse][[/noparse]ABUTTON]
And here is code that does NOT work
' ****************************************************************
' * GetInputValue *
' * Get previously stored value *
' ****************************************************************
PUB GetInputValue(index)
if( index >= 0 and index < MAXINPUTINDEX )
return inputValues[noparse][[/noparse]index]
else
return 0
Here is how I delcare inputValues:
CON MAXINPUTINDEX = 8 VAR long inputValues[noparse][[/noparse]8]
I really don't want to have to put a case statement in there to return the values directly and the array access should work with a variable - or maybe I missed something in the manual, but there are other cases where I use a variable. In fact, in the same object causing problems I have an array of indices into another array (because I couldn't figure out how to do multi-dimensional arrays). TIA

Comments
Here is what I have for the CON section in the object:
And here is my call:
I think the pantilt#ZBUTTON is what is incorrect. What is the proper syntax for referring to an objects constant values?
Again, TIA.
I found the error with the following debug-strategy:
CHECK EVERY SINGLE DAMMED DETAIL BY DEBUGOUTPUT
PUB GetInputValue(index) Debug.Str(string("Index=")) Debug.dec(index) Debug.Str(string(" ",13)) if( index >= 0 and index < MAXINPUTINDEX ) 'if( index => 0 and index < MAXINPUTINDEX ) Debug.Str(string("Index OK Index=")) Debug.dec(index) Debug.Str(string(" ",13)) Debug.Str(string("inputValues[noparse][[/noparse]")) Debug.dec(index) Debug.Str(string("]=")) Debug.dec(inputValues[noparse][[/noparse]index]) Debug.Str(string(" ",13)) return inputValues[noparse][[/noparse]index] else Debug.Str(string("Index out of range Index=")) Debug.dec(index) return 0this code lead me to the bug
debugoutput
right at the beginning parameter index contains the right value
after the if-condition it's "-1" all the time
aha there happends something inside the if-condition
it is the "index >= 0"
exactly the ">=" is the bug
the equal-sign BEHIND the greater sign means store result into variable index
the correct notation for the condition is equal or greater is
"=>" the equal-sign must be written INFRONT of the greater-sign
here the complete democode from my debugging
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 MAXINPUTINDEX = 8 OBJ Debug : "FullDuplexSerial" 'serial connection to PC VAR long inputValues[noparse][[/noparse] MAXINPUTINDEX] long i long value PUB Main Debug.Start(31,30,0,115200) 'start(rxpin, txpin, mode, baudrate) : '' okay '"rx" and "tx" viewed from Propeller-Chip Propeller-Chip-PIN-Tx ----> PC Debug.Str(string("START",13)) repeat i from 0 to 7 inputValues[noparse][[/noparse] i] := i * 10 repeat i from 0 to 7 Debug.Str(string("inputValues[noparse][[/noparse] ")) Debug.dec(i) Debug.Str(string(" ]=")) Debug.dec(inputValues[noparse][[/noparse] i]) Debug.Str(string(" ",13,13)) 'waitcnt(ClkFreq + cnt) repeat repeat i from 0 to 7 value := GetInputValue(i) Debug.Str(string(" ",13)) waitcnt(ClkFreq + cnt) PUB GetInputValue(index) Debug.Str(string("Index=")) Debug.dec(index) Debug.Str(string(" ",13)) 'if( index >= 0 and index < MAXINPUTINDEX ) if( index => 0 and index < MAXINPUTINDEX ) Debug.Str(string("Index OK Index=")) Debug.dec(index) Debug.Str(string(" ",13)) Debug.Str(string("inputValues[noparse][[/noparse] ")) Debug.dec(index) Debug.Str(string(" ]=")) Debug.dec(inputValues[noparse][[/noparse] index]) Debug.Str(string(" ",13)) return inputValues[noparse][[/noparse] index] else Debug.Str(string("Index out of range Index=")) Debug.dec(index) return 0debugoutput
best regards
Stefan
P.S.: I tested the syntax "objectname#constantname
it seems to work
Post Edited (StefanL38) : 12/26/2008 1:01:56 AM GMT