I'm not sure if CE is capable of using Lua this way, but it's good practice to make sure that the object is instantiated before trying to access its properties.
The colon is just syntactic sugar for omitting the first parameter. The actual signature of the myClass:stop function is function myClass.stop(self, timer). When the stop function gets invoked by the timer, it passes the timer object to the first parameter (i.e. self) and nothing to the second parameter (i.e. timer == nil).
You'd be better off making timer a property of the class, unless for some reason it absolutely must be a global.
Code:
myClass = {}
function myClass:foo()
print("placeholder")
end
function myClass:stop(timer)
timer = timer or self.timer
if <some condition> then
timer.Enabled = false
else
self:foo()
end
end
function myClass:run()
self.timer.Enabled = true
end
function myClass:new(t)
t = t or {}
-- unless you're accessing something in the GUI of the main
-- form (e.g. address list), it shouldn't need a parent
t.timer = t.timer or createTimer(getMainForm(), false)
t.timer.Interval = 5 * 1000
t.timer.OnTimer = function(timer) self.stop(t, timer) end
self.__index = self
return setmetatable(t, self)
end
-- I *think* CE already does this, but just in case...
function myClass.__gc(o)
if o.timer then
o.timer.destroy()
o.timer = nil
end
end
myObj = myClass:new()
myObj:run()
_________________
I don't know where I'm going, but I'll figure it out when I get there.
The colon is just syntactic sugar for omitting the first parameter. The actual signature of the myClass:stop function is function myClass.stop(self, timer). When the stop function gets invoked by the timer, it passes the timer object to the first parameter (i.e. self) and nothing to the second parameter (i.e. timer == nil).
Yeah, that's what I thought... Thanks.
I made it like this, and it solved the problem.
Code:
timer.OnTimer = function(timer) self:stop(timer) end
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum