lua-users home
lua-l archive

RE: How to change class member data reference?

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


You're mixing 2 things I think....
> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of albert_200200
> Sent: donderdag 11 september 2014 10:09
> To: lua-l
> Subject: How to change class member data reference?
> 
> First I use the following code to define a lua class:
> clsSMSSend = {}
> function clsSMSSend:new()
> local o = {}
> setmetatable(o, self)
> self.__index = self
> o.MobileNo = "oldmobile"
> o.tbl_body = {
> [1] = o.MobileNo,
> }
You put in in a sub-table, which indicates you're intending to use it as a proxy table. But in that case you need to add __newindex and __index methods that handle retrieval and storage of those elements
> return o
> end
> 
> The problem is as below:
> obj = clsSMSSend:new()
> obj.MobileNo = "newmobile"
> for k, v in ipairs(obj.tbl_body) do
Now you're looking in obj.tbl_body where you put the value in obj.MobileNo, not in obj.tbl_body.MobileNo. If you use the proxy approach, then set an __index method that looks key up inside tbl_body.
Hth
Thijs
> print(k, v) --but here still print "oldmobile" not "newmobile"
> end
> 
> If I want the code to print "newmobile", how can I correct it? Thanks.
> 

AltStyle によって変換されたページ (->オリジナル) /