Handling UNO Objects

From Apache OpenOffice Wiki
Jump to: navigation, search

Accessing UNO Services

UNO objects are used through their interface methods and properties. Basic simplifies this by mapping UNO interfaces and properties to Basic object methods and properties.

First, in Basic it is not necessary to distinguish between the different interfaces an object supports when calling a method. The following illustration shows an example of an UNO service that supports three interfaces:

Basic Hides Interfaces

In Java and C++, it is necessary to obtain a reference to each interface before calling one of its methods. In Basic, every method of every supported interface can be called directly at the object without querying for the appropriate interface in advance. The '.' operator is used:

  ' Basic
  oExample = getExampleObjectFromSomewhere()
  oExample.doNothing()' Calls method doNothing of XFoo1
  oExample.doSomething()' Calls method doSomething of XFoo2
  oExample.doSomethingElse(42)' Calls method doSomethingElse of XFoo2

Additionally, Apache OpenOffice Basic interprets pairs of get and set methods at UNO objects as Basic object properties if they follow this pattern:

 SomeType getSomeProperty()
 void setSomeProperty(SomeType aValue) 

In this pattern, Apache OpenOffice Basic offers a property of type SomeType named SomeProperty. This functionality is based on the com.sun.star.beans.Introspection service. For additional details, see UNO Reflection API.

The get and set methods can always be used directly. In our example service above, the methods getIt() and setIt(), or read and write a Basic property It are used:

  Dim x as Integer
  x = oExample.getIt() ' Calls getIt method of XFoo3
  ' is the same as
  x = oExample.It ' Read property It represented by XFoo3
  oExample.setIt( x ) ' Calls setIt method of XFoo3
  ' is the same as
  oExample.It = x ' Modify property It represented by XFoo3

If there is only a get method, but no associated set method, the property is considered to be read only.

  Dim x as Integer, y as Integer
  x = oExample.getMore() ' Calls getMore method of XFoo1
  y = oExample.getLess() ' Calls getLess method of XFoo1
  ' is the same as
  x = oExample.More ' Read property More represented by XFoo1
  y = oExample.Less ' Read property Less represented by XFoo1
  ' but
  oExample.More = x ' Runtime error "Property is read only"
  oExample.Less = y ' Runtime error "Property is read only"

Properties an object provides through com.sun.star.beans.XPropertySet are available through the . operator. The methods of com.sun.star.beans.XPropertySet can be used also. The object oExample2 in the following example has three integer properties Value1, Value2 and Value3:

  Dim x as Integer, y as Integer, z as Integer
  x = oExample2.Value1
  y = oExample2.Value2
  z = oExample2.Value3
  ' is the same as
  x = oExample2.getPropertyValue( "Value1" )
  y = oExample2.getPropertyValue( "Value2" )
  z = oExample2.getPropertyValue( "Value3" )
  ' and
  oExample2.Value1 = x
  oExample2.Value2 = y
  oExample2.Value3 = z
  ' is the same as
  oExample2.setPropertyValue( "Value1", x )
  oExample2.setPropertyValue( "Value2", y )
  oExample2.setPropertyValue( "Value3", z )

Basic uses com.sun.star.container.XNameAccess to provide named elements in a collection through the . operator. However, XNameAccess only provides read access. If a collection offers write access through com.sun.star.container.XNameReplace or com.sun.star.container.XNameContainer, use the appropriate methods explicitly:

  ' oNameAccessible is an object that supports XNameAccess
  ' including the names "Value1", "Value2"
  x = oNameAccessible.Value1
  y = oNameAccessible.Value2
  ' is the same as
  x = oNameAccessible.getByName( "Value1" )
  y = oNameAccessible.getByName( "Value2" )
  ' but
  oNameAccessible.Value1 = x' Runtime Error, Value1 cannot be changed
  oNameAccessible.Value2 = y' Runtime Error, Value2 cannot be changed
  ' oNameReplace is an object that supports XNameReplace
  ' replaceByName() sets the element Value1 to 42
  oNameReplace.replaceByName( "Value1", 42 )
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages