# OOP Question - Passing an object method as a function parameter?

**URL:** <https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684>\
**Category:** Lua Language\
**Created:** [October 7, 2013, 4:54pm UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684 "2013-10-07T16:54:26Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![SegaBoy](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@SegaBoy](https://forums.solar2d.com/u/SegaBoy)\
**Post date:** [October 7, 2013, 4:54pm UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/1 "2013-10-07T16:54:26Z")

</div>

I’m trying to pass an object method as a parameter, essentially as a callback function for a network request in another class.

[lua]

function ItemManager:loadData()  
&nbsp;&nbsp;&nbsp; --print(tostring(self:getItemData))

&nbsp; &nbsp; --cant pass with the : sytax  
&nbsp;&nbsp;&nbsp; Parse.getItems(self.getItemData)  
end

[/lua]

This in turn should call the following function/method when the network request is finished.

[lua]

function ItemManager:getItemData(data)  
&nbsp;&nbsp;&nbsp; print("ItemManager: data - "…data.response)  
&nbsp;&nbsp;&nbsp; self.data = data.response  
end

[/lua]

The problem is that I can’t pass the function as a method with the : syntax, and if I attempt to pass it with a . &nbsp;it doesn’t fire the call.

I need getItemData to be a method because it needs to access the object’s properties.

Does anybody know how to handle this situation; or perhaps an alternative way of dealing with this?

Many thanks…

---

<div class="post-metadata">

**Author:** ![piotrz55](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@piotrz55](https://forums.solar2d.com/u/piotrz55)\
**Post date:** [October 7, 2013, 5:06pm UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/2 "2013-10-07T17:06:15Z")

</div>

Solution is simple, just wrap your function using colon with anonymous function. Then call this wrapper function which will execute your code.

---

<div class="post-metadata">

**Author:** ![SegaBoy](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@SegaBoy](https://forums.solar2d.com/u/SegaBoy)\
**Post date:** [October 8, 2013, 9:03am UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/3 "2013-10-08T09:03:20Z")

</div>

Thanks for your input, however I still can’t get this to work. I changed it to an anonymous function, like so:

[lua]

function ItemManager:loadData()  
&nbsp;&nbsp;&nbsp; --print(tostring(self:getItemData))  
&nbsp;&nbsp;&nbsp; Parse.getItems(  
&nbsp;&nbsp;&nbsp; function()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self:getItemData()  
&nbsp;&nbsp;&nbsp; end)  
end

[/lua]

However it’s not firing. The only way I’ve managed to get the callback to work is by having a local function like so:

[lua]

local function getItemData(data)  
&nbsp;&nbsp;&nbsp; print("ItemManager: data - "…data.response)  
&nbsp;&nbsp;&nbsp; self:populateData(data.response)&nbsp;  
end

function ItemManager:loadData()  
&nbsp;&nbsp;&nbsp; --print(tostring(self:getItemData))  
&nbsp;&nbsp;&nbsp; Parse.getItems(getItemData)  
end

[/lua]

However I need getItemData to be a method as the call to self:populateData won’t work otherwise?!?

---

<div class="post-metadata">

**Author:** ![SegaBoy](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@SegaBoy](https://forums.solar2d.com/u/SegaBoy)\
**Post date:** [October 8, 2013, 9:39am UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/4 "2013-10-08T09:39:51Z")

</div>

Looking further it seems as the anonymous function is being called immediately, perhaps due to the asynchronous nature of network.request.

---

<div class="post-metadata">

**Author:** ![piotrz55](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@piotrz55](https://forums.solar2d.com/u/piotrz55)\
**Post date:** [October 8, 2013, 12:36pm UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/5 "2013-10-08T12:36:26Z")

</div>

You put it in wrong place as I see… it’s hard to say because I dont know what your functions do and cannot see data structure.

---

<div class="post-metadata">

**Author:** ![SegaBoy](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@SegaBoy](https://forums.solar2d.com/u/SegaBoy)\
**Post date:** [October 8, 2013, 12:48pm UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/6 "2013-10-08T12:48:48Z")

</div>

ItemManager is a basic class - it’s interacting with [Parse.com](http://Parse.com) to download a bunch of objects in a [Parse.com](http://Parse.com) class. The only real thing that is happening here is passing a callback to a function that makes a query to [Parse.com](http://Parse.com) via a network.request() call.

I’m a little confused by what you mean “… wrong place”; all I’m trying to do is pass a callback that refers to a method of ItemManager; however the anonymous function approach doesn’t work (well it seems to fire immediately with event == nil). The only approach that works so far is to have the callback as the local/private function.

Can I just ask where the anonymous call should go? Many thanks for your help.

---

<div class="post-metadata">

**Author:** ![piotrz55](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@piotrz55](https://forums.solar2d.com/u/piotrz55)\
**Post date:** [October 8, 2013, 3:42pm UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/7 "2013-10-08T15:42:47Z")

</div>

But shouldn’t function you pass to Parse take an argument?  
In your not-working example you made anonymous function but it never takes argument!

I do not know what structure Parse returns but you never pass data from it to your function.  
If so it should be

```
local function func(dataFromParse) self:getItemData(dataFromParse) end Parse.getItems(func)
```

---

<div class="post-metadata">

**Author:** ![SegaBoy](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@SegaBoy](https://forums.solar2d.com/u/SegaBoy)\
**Post date:** [October 8, 2013, 3:53pm UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/8 "2013-10-08T15:53:56Z")

</div>

Thank you Piotrz - the noob problem was that I hadn’t specified the arguments in the anonymous callback function, once I changed that everything is working as expected.

Many thanks…

---

<div class="post-metadata">

**Author:** ![piotrz55](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@piotrz55](https://forums.solar2d.com/u/piotrz55)\
**Post date:** [October 7, 2013, 5:06pm UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/9 "2013-10-07T17:06:15Z")

</div>

Solution is simple, just wrap your function using colon with anonymous function. Then call this wrapper function which will execute your code.

---

<div class="post-metadata">

**Author:** ![SegaBoy](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@SegaBoy](https://forums.solar2d.com/u/SegaBoy)\
**Post date:** [October 8, 2013, 9:03am UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/10 "2013-10-08T09:03:20Z")

</div>

Thanks for your input, however I still can’t get this to work. I changed it to an anonymous function, like so:

[lua]

function ItemManager:loadData()  
&nbsp;&nbsp;&nbsp; --print(tostring(self:getItemData))  
&nbsp;&nbsp;&nbsp; Parse.getItems(  
&nbsp;&nbsp;&nbsp; function()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self:getItemData()  
&nbsp;&nbsp;&nbsp; end)  
end

[/lua]

However it’s not firing. The only way I’ve managed to get the callback to work is by having a local function like so:

[lua]

local function getItemData(data)  
&nbsp;&nbsp;&nbsp; print("ItemManager: data - "…data.response)  
&nbsp;&nbsp;&nbsp; self:populateData(data.response)&nbsp;  
end

function ItemManager:loadData()  
&nbsp;&nbsp;&nbsp; --print(tostring(self:getItemData))  
&nbsp;&nbsp;&nbsp; Parse.getItems(getItemData)  
end

[/lua]

However I need getItemData to be a method as the call to self:populateData won’t work otherwise?!?

---

<div class="post-metadata">

**Author:** ![SegaBoy](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@SegaBoy](https://forums.solar2d.com/u/SegaBoy)\
**Post date:** [October 8, 2013, 9:39am UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/11 "2013-10-08T09:39:51Z")

</div>

Looking further it seems as the anonymous function is being called immediately, perhaps due to the asynchronous nature of network.request.

---

<div class="post-metadata">

**Author:** ![piotrz55](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@piotrz55](https://forums.solar2d.com/u/piotrz55)\
**Post date:** [October 8, 2013, 12:36pm UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/12 "2013-10-08T12:36:26Z")

</div>

You put it in wrong place as I see… it’s hard to say because I dont know what your functions do and cannot see data structure.

---

<div class="post-metadata">

**Author:** ![SegaBoy](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@SegaBoy](https://forums.solar2d.com/u/SegaBoy)\
**Post date:** [October 8, 2013, 12:48pm UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/13 "2013-10-08T12:48:48Z")

</div>

ItemManager is a basic class - it’s interacting with [Parse.com](http://Parse.com) to download a bunch of objects in a [Parse.com](http://Parse.com) class. The only real thing that is happening here is passing a callback to a function that makes a query to [Parse.com](http://Parse.com) via a network.request() call.

I’m a little confused by what you mean “… wrong place”; all I’m trying to do is pass a callback that refers to a method of ItemManager; however the anonymous function approach doesn’t work (well it seems to fire immediately with event == nil). The only approach that works so far is to have the callback as the local/private function.

Can I just ask where the anonymous call should go? Many thanks for your help.

---

<div class="post-metadata">

**Author:** ![piotrz55](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@piotrz55](https://forums.solar2d.com/u/piotrz55)\
**Post date:** [October 8, 2013, 3:42pm UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/14 "2013-10-08T15:42:47Z")

</div>

But shouldn’t function you pass to Parse take an argument?  
In your not-working example you made anonymous function but it never takes argument!

I do not know what structure Parse returns but you never pass data from it to your function.  
If so it should be

```
local function func(dataFromParse) self:getItemData(dataFromParse) end Parse.getItems(func)
```

---

<div class="post-metadata">

**Author:** ![SegaBoy](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@SegaBoy](https://forums.solar2d.com/u/SegaBoy)\
**Post date:** [October 8, 2013, 3:53pm UTC](https://forums.solar2d.com/t/oop-question-passing-an-object-method-as-a-function-parameter/318684/15 "2013-10-08T15:53:56Z")

</div>

Thank you Piotrz - the noob problem was that I hadn’t specified the arguments in the anonymous callback function, once I changed that everything is working as expected.

Many thanks…
