3.3.2 Using Methods and Properties

LuaCOM

next up previous contents Next: 3.3.2.1 Extensible interfaces Up: 3.3 Automation binding Previous: 3.3.1 Implementing dispinterfaces in   Contents


3.3.2 Using Methods and Properties

The dispinterfaces have two ``types'' of members: properties and methods. LuaCOM deals with both.

Method accesses are done in the same way as calling Lua functions stored in a table and having a ``self'' parameter:

obj = luacom.CreateObject("TEST.Test")
if obj == nil then
  exit(1)
end
-- method call
a = obj:Teste(1,2)
-- another one
obj:Teste2(a+1)

It's important to notice the need of using the colon - ``:'' - for method calls. Although LuaCOM does not use the self parameter that Lua passes in this case, its presence is assumed, that is, LuaCOM always skips the first parameter in the case of method calls; forgetting it may cause nasty bugs. Notice that this rule doesn't apply when using the default method of a LuaCOM object stored in a table or in a property of another LuaCOM object (see section 3.3.2 below).

Accessing properties is much like the same of accessing fields in Lua tables:

obj = luacom.CreateObject("TEST.Test")
if obj == nil then
  exit(1)
end
-- property access
a = obj.TestData
-- property setting
obj.TestData = a + 1

Properties may also be accessed as methods. This is mandatory when dealing with parameterized properties, that it, ones that accept (or demand) parameters. A common example of this situation is the ``Item'' property of collections.

-- property access
a = obj:TestData()
-- Parametrized property access
b = obj:TestInfo(2)
-- Accessing collections
c = obj.Files:Item(2)

Notice that the colon - ``:'' - must also be used in this situation.

When accessing properties with method calls, LuaCOM always translates the method call to a read access (property get). To set the value of a property using a method call, it's necessary append the prefix ``set''3.1 to the property name and the new value must be supplied as the last argument.

-- property access
a = obj:TestData()
-- Setting the property
b = obj:setTestInfo(2)
-- Setting a parametrized property
c = obj.Files:setItem(2, "test.txt")

The prefix ``get'' may also be used, to clarify the code, although it's not necessary, as the default behavior is to make a read access.

-- property access
a = obj:getTestData()
b = obj:getTestInfo(2)
c = obj.Files:getItem(2)



Subsections
next up previous contents
Next: 3.3.2.1 Extensible interfaces Up: 3.3 Automation binding Previous: 3.3.1 Implementing dispinterfaces in   Contents
Fabio Mascarenhas de Queiroz 2005-01-07