Trouble with OOP and accessing variables

Hi!
I’m new to Corona, coming from Actionscript. My app features a time picker, which I want to re-use in a few different places, so obviously wanted to implement some OOP. Trouble is, I can’t quite get my head around how it actually works. I have a functioning time picker, but am having trouble accessing its variables from within the class itself. Here’s my code:

[lua]module(…, package.seeall)

local widget = require “widget”

picker = {}
picker.__index = picker

function picker.new()
local picker_object = {}
setmetatable(picker_object,picker)

picker_object.theHour = 12
picker_object.theMin = 0
picker_object.am = true

picker_object.increaseHrBtn = widget.newButton{
default=“gfx/up_regular.png”,
over=“gfx/up_press.png”,
width=40, height=40,
onRelease = picker.incHr
}

picker_object.decreaseHrBtn = widget.newButton{
default=“gfx/down_regular.png”,
over=“gfx/down_press.png”,
width=40, height=40,
onRelease = picker.decHr
}

picker_object.decreaseHrBtn.y = 100

picker_object.hrText = display.newText(“12”, 6, 41, native.systemFont, 24)
picker_object.hrText:setTextColor(0, 0, 0)

picker_object.dotsText = display.newText(":", 58, 39, native.systemFont, 24)
picker_object.dotsText:setTextColor(0, 0, 0)

picker_object.increaseMinBtn = widget.newButton{
default=“gfx/up_regular.png”,
over=“gfx/up_press.png”,
width=40, height=40,
onRelease = picker.incMin
}

picker_object.increaseMinBtn.x = 100

picker_object.decreaseMinBtn = widget.newButton{
default=“gfx/down_regular.png”,
over=“gfx/down_press.png”,
width=40, height=40,
onRelease = picker.decMin
}
picker_object.decreaseMinBtn.y = 100
picker_object.decreaseMinBtn.x = 100

picker_object.minText = display.newText(“00”, 86, 41, native.systemFont, 24)
picker_object.minText:setTextColor(0, 0, 0)

picker_object.ampmBtn = widget.newButton{
default=“gfx/blank_normal.png”,
over=“gfx/blank_press.png”,
label = “AM”,
font = “Korean Calligraphy”,
fontSize = 25,
width=58, height=58,
onRelease = picker.toggleAmPm
}

picker_object.ampmBtn.x = 160
picker_object.ampmBtn.y = 58

return picker_object
end

function picker:getHour()
return self.theHour
end

function picker:getMin()
return self.theMin
end

function picker:incHr(event)
print(“inc”)
if self.theHour < 12 then
self.theHour = self.theHour + 1
else
self.theHour = 1
end

if self.theHour < 10 then
picker_object.hrText.text = “0”…self.theHour
else
picker_object.hrText.text = self.theHour
end
end

function picker:decHr(event)
print(“dec”)
if self.theHour > 1 then
self.theHour = self.theHour - 1
else
self.theHour = 12
end
if self.theHour < 10 then
picker_object.hrText.text = “0”…self.theHour
else
picker_object.hrText.text = self.theHour
end
end

function picker:incMin(event)
print(“inc”)
if self.theMin < 59 then
self.theMin = self.theMin + 1
else
self.theMin = 0
end

if self.theMin < 10 then
picker_object.minText.text = “0”…self.theMin
else
self.minText.text = self.theMin
end
end

function picker:decMin(event)
print(“dec”)
if self.theMin > 0 then
self.theMin = self.theMin - 1
else
self.theMin = 59
end
if self.theMin < 10 then
picker_object.minText.text = “0”…self.theMin
else
picker_object.minText.text = self.theMin
end
end

function picker:toggleAmPm(event)
if self.am == true then
self.am = false
picker_object.ampmBtn:setLabel( “PM” )
else
self.am = true
picker_object.ampmBtn:setLabel( “AM” )
end
end[/lua]

I thought I could access the variables theHour and theMin using self.theHour… but I get the error that those values are nil. Can anyone tell me where I’m going wrong here? I can access the same variables from outside the class using the getHr and getMin functions no problem, and those use the self. notation.

Appreciate the help!

Emma. [import]uid: 147695 topic_id: 27794 reply_id: 327794[/import]

Hi.

From your code …

self.theHour would be equal to Picker.theHour which is nil. since self is inherited from picker.

If you want it to work like that you would need to change:

picker_object.theHour

to

Picker.theHour

Or use picker_object.theHour where needed.
Hope this helps
[import]uid: 84637 topic_id: 27794 reply_id: 114760[/import]