in my program, the watch calls my onPosition function, and towards the start of the program I have it "initiate" points to be used. Is it possible to compress this code into a single function, instead of having to repeat the calls three times?
class ProgramView extends WatchUi.View { var pointA, pointB, pointC; function initiatePoints (minfo) { if ( (minfo != null) && (minfo.accuracy != null) ) { var curpos = minfo.position.toDegrees(); if (pointA == null) { pointA = new Position.Location({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees }); } else { pointA.initialize({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees }); } if (pointB == null) { pointB = new Position.Location({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees }); } else { pointB.initialize({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees }); } if (pointC == null) { pointC = new Position.Location({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees }); } else { pointC.initialize({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees }); } //...
I tried to create a smaller function like the below, however it isn't working as expected. The point data is somehow erroneously duplicating, as if it is only defining/redefining a single object. When I load the memory view in the simulator, when I go to print out the different pointA, PointB, PointC variables, they all end up referring to the same object, the same object number. Thus, it appears as if my attempt to do this, was unsuccessful.
// pardon this code has not been recently tested, I'm typing from memory class ProgramView extends WatchUi.View { var pointA, pointB, pointC; function initiatePoints (minfo) { if ( (minfo != null) && (minfo.accuracy != null) ) { var curpos = minfo.position.toDegrees(); setPoint(pointA, curpos); setPoint(pointB, curpos); setPoint(pointC, curpos); //... } function setPoint(locationVar, posInDegrees) if (locationVar == null) { locationVar = new Position.Location({ :latitude => posInDegrees[0], :longitude => posInDegrees[1], :format => :degrees }); } else { locationVar.initialize({ :latitude => posInDegrees[0], :longitude => posInDegrees[1], :format => :degrees }); } }
I think in order to stick the initiating code into a separate function, I have to use reference values or somehow specify that I am initiating separate variable objects, not the same object. Can anyone give me some good clues on how to do this, so I can prevent unnecessary duplication of code?
(apologies: the second code segment I wrote from memory, because I had to remove that functionality because it didn't work. I may have tried to use reference variables or somehow re-write the "setPoint" function so that it was better than how it appears here, however I was unable to make it work.)