3.3.2.2 Default methods

LuaCOM

next up previous contents Next: 3.3.2.3 Generic LuaCOM objects Up: 3.3.2 Using Methods and Previous: 3.3.2.1 Extensible interfaces   Contents


3.3.2.2 Default methods

A dispinterface can have a default method or property, that is, one that is called when the client does not specify the method name. LuaCOM calls the default method when the object itself is used as a function.

  excel = luacom.CreateObject("Excel.Application")
  excel.Visible = true
  excel.Workbooks:Add()
  -- Here we call the default method
  -- notice we DID NOT use the colon, as
  -- the object used is Sheets, not excel
  sheet = excel.Sheets(1)
  print(sheet.Name)
  -- Here we also call the default method
  -- We must supply the self parameter
  sheets = excel.Sheets
  sheet2 = sheets(2)
  print(sheet2.Name)
  -- Setting values
  excel.Sheets(1).Name = "MySheet1"
  excel:Quit()

This can be very useful when dealing with collections, as commonly they have a default Item property.

WARNING: one must be careful not to put the colon when using default methods of LuaCOM objects contained in table or in other LuaCOM objects (see the sample above).



Fabio Mascarenhas de Queiroz 2005-01-07