[Resolved] Cant pass string as parameter for class functions

If I have a class named ‘team’ and a public member function of that class as below, why can I not pass a string to it as a parameter? The string is always received in the function as ‘nil’. (However, passing in numbers works fine!!? :S)

I’m using http://www.coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/ as my guide to classes (and have set everything up as detailed in there)…

[lua]function team:setName(msg)
print("MSG = " … msg) – receive an error telling me ‘msg’ is ‘nil’
end[/lua]

Here’s the code for calling the function from a ‘main.lua’ file

[lua]local team = require(“team”)
local team1 = team.new(“no name”)
team1.setName(“Red Team”)[/lua]

Thanks for the help!!!.. [import]uid: 134465 topic_id: 29920 reply_id: 329920[/import]

There is a very significant difference between : and . when calling/declaring functions.

These two are functionally equivalent

function team:setName(msg) end  
function team.setName(self, msg) end  

And so are these

team1.setName(team1, "Red Team")  
team1:setName("Read Team")  

And those calls will work with those declarations

Because you declared the function with : msg is the second parameter to the function, not the first (which is self). The error is given because “Red Team” is the self variable inside of setName and not msg. Because : was not an original part of the language and merely syntax sugar to make things easier to read, it’s very easy to trick (both for good and bad), allowing you to pass fake ‘self’ values into any function by simply using the . and adding a different first parameter.
You can either pass team1 into the function, call it with a :, or declare it with a . (if you don’t need the self parameter). [import]uid: 134101 topic_id: 29920 reply_id: 119939[/import]

Ok, that makes sense (and explains why a number worked - it was an additional parameter I had passed in after the string!!).

Thanks for the help. [import]uid: 134465 topic_id: 29920 reply_id: 119987[/import]

There is a very significant difference between : and . when calling/declaring functions.

These two are functionally equivalent

function team:setName(msg) end  
function team.setName(self, msg) end  

And so are these

team1.setName(team1, "Red Team")  
team1:setName("Read Team")  

And those calls will work with those declarations

Because you declared the function with : msg is the second parameter to the function, not the first (which is self). The error is given because “Red Team” is the self variable inside of setName and not msg. Because : was not an original part of the language and merely syntax sugar to make things easier to read, it’s very easy to trick (both for good and bad), allowing you to pass fake ‘self’ values into any function by simply using the . and adding a different first parameter.
You can either pass team1 into the function, call it with a :, or declare it with a . (if you don’t need the self parameter). [import]uid: 134101 topic_id: 29920 reply_id: 119939[/import]

Ok, that makes sense (and explains why a number worked - it was an additional parameter I had passed in after the string!!).

Thanks for the help. [import]uid: 134465 topic_id: 29920 reply_id: 119987[/import]