Difference between revisions of "Documentation/DevGuide/ProUNO/Basic/Handling UNO Objects"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (FINAL VERSION FOR L10N)
m
 
Line 18: Line 18:
  
 
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 '<code>.</code>' operator is used:
 
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 '<code>.</code>' operator is used:
 
+
<source lang="oobas">
 
   ' Basic
 
   ' Basic
 
   oExample = getExampleObjectFromSomewhere()
 
   oExample = getExampleObjectFromSomewhere()
Line 24: Line 24:
 
   oExample.doSomething()' Calls method doSomething of XFoo2
 
   oExample.doSomething()' Calls method doSomething of XFoo2
 
   oExample.doSomethingElse(42)' Calls method doSomethingElse of XFoo2
 
   oExample.doSomethingElse(42)' Calls method doSomethingElse of XFoo2
 
+
</source>
 
Additionally, {{PRODUCTNAME}} Basic interprets pairs of get and set methods at UNO objects as Basic object properties if they follow this pattern:
 
Additionally, {{PRODUCTNAME}} Basic interprets pairs of get and set methods at UNO objects as Basic object properties if they follow this pattern:
  
Line 33: Line 33:
  
 
The get and set methods can always be used directly. In our example service above, the methods <code>getIt()</code> and <code>setIt()</code>, or read and write a Basic property <code>It</code> are used:
 
The get and set methods can always be used directly. In our example service above, the methods <code>getIt()</code> and <code>setIt()</code>, or read and write a Basic property <code>It</code> are used:
 
+
<source lang="oobas">
 
   Dim x as Integer
 
   Dim x as Integer
 
   x = oExample.getIt() ' Calls getIt method of XFoo3
 
   x = oExample.getIt() ' Calls getIt method of XFoo3
Line 46: Line 46:
 
    
 
    
 
   oExample.It = x ' Modify property It represented by XFoo3
 
   oExample.It = x ' Modify property It represented by XFoo3
 
+
</source>
 
If there is only a get method, but no associated set method, the property is considered to be read only.
 
If there is only a get method, but no associated set method, the property is considered to be read only.
 
+
<source lang="oobas">
 
   Dim x as Integer, y as Integer
 
   Dim x as Integer, y as Integer
 
   x = oExample.getMore() ' Calls getMore method of XFoo1
 
   x = oExample.getMore() ' Calls getMore method of XFoo1
Line 62: Line 62:
 
   oExample.More = x ' Runtime error "Property is read only"
 
   oExample.More = x ' Runtime error "Property is read only"
 
   oExample.Less = y ' Runtime error "Property is read only"
 
   oExample.Less = y ' Runtime error "Property is read only"
 
+
</source>
 
Properties an object provides through <idl>com.sun.star.beans.XPropertySet</idl> are available through the <code>.</code> operator. The methods of <idl>com.sun.star.beans.XPropertySet</idl> can be used also. The object <code>oExample2</code> in the following example has three integer properties <code>Value1</code>, <code>Value2</code> and <code>Value3</code>:
 
Properties an object provides through <idl>com.sun.star.beans.XPropertySet</idl> are available through the <code>.</code> operator. The methods of <idl>com.sun.star.beans.XPropertySet</idl> can be used also. The object <code>oExample2</code> in the following example has three integer properties <code>Value1</code>, <code>Value2</code> and <code>Value3</code>:
 
+
<source lang="oobas">
 
   Dim x as Integer, y as Integer, z as Integer
 
   Dim x as Integer, y as Integer, z as Integer
 
   x = oExample2.Value1
 
   x = oExample2.Value1
Line 87: Line 87:
 
   oExample2.setPropertyValue( "Value2", y )
 
   oExample2.setPropertyValue( "Value2", y )
 
   oExample2.setPropertyValue( "Value3", z )
 
   oExample2.setPropertyValue( "Value3", z )
 
+
</source>
 
Basic uses <idl>com.sun.star.container.XNameAccess</idl> to provide named elements in a collection through the . operator. However, XNameAccess only provides read access. If a collection offers write access through <idl>com.sun.star.container.XNameReplace</idl> or <idl>com.sun.star.container.XNameContainer</idl>, use the appropriate methods explicitly:
 
Basic uses <idl>com.sun.star.container.XNameAccess</idl> to provide named elements in a collection through the . operator. However, XNameAccess only provides read access. If a collection offers write access through <idl>com.sun.star.container.XNameReplace</idl> or <idl>com.sun.star.container.XNameContainer</idl>, use the appropriate methods explicitly:
 
+
<source lang="oobas">
 
   ' oNameAccessible is an object that supports XNameAccess
 
   ' oNameAccessible is an object that supports XNameAccess
 
   ' including the names "Value1", "Value2"
 
   ' including the names "Value1", "Value2"
Line 108: Line 108:
 
   ' replaceByName() sets the element Value1 to 42
 
   ' replaceByName() sets the element Value1 to 42
 
   oNameReplace.replaceByName( "Value1", 42 )
 
   oNameReplace.replaceByName( "Value1", 42 )
 
+
</source>
 
{{PDL1}}
 
{{PDL1}}
  
 
[[Category:Documentation/Developer's Guide/Professional UNO]]
 
[[Category:Documentation/Developer's Guide/Professional UNO]]

Latest revision as of 10:17, 22 October 2009



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, OpenOffice.org 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, OpenOffice.org 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