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

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Robot: Changing Category:Professional UNO)
 
(5 intermediate revisions by 4 users not shown)
Line 7: Line 7:
 
|NextPage=Documentation/DevGuide/ProUNO/Basic/Instantiating UNO Services
 
|NextPage=Documentation/DevGuide/ProUNO/Basic/Instantiating UNO Services
 
}}
 
}}
 +
{{Documentation/DevGuideLanguages|Documentation/DevGuide/ProUNO/Basic/{{SUBPAGENAME}}}}
 
{{DISPLAYTITLE:Handling UNO Objects}}
 
{{DISPLAYTITLE:Handling UNO Objects}}
 
=== Accessing UNO Services ===
 
=== Accessing UNO Services ===
Line 17: 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:
 
+
<syntaxhighlight lang="oobas">
 
   ' Basic
 
   ' Basic
 
   oExample = getExampleObjectFromSomewhere()
 
   oExample = getExampleObjectFromSomewhere()
Line 23: 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
 
+
</syntaxhighlight>
Additionally, {{PRODUCTNAME}} Basic interprets pairs of get and set methods at UNO objects as Basic object properties if they follow this pattern:
+
Additionally, {{AOo}} Basic interprets pairs of get and set methods at UNO objects as Basic object properties if they follow this pattern:
  
 
   SomeType getSomeProperty()
 
   SomeType getSomeProperty()
 
   void setSomeProperty(SomeType aValue)  
 
   void setSomeProperty(SomeType aValue)  
  
In this pattern, {{PRODUCTNAME}} Basic offers a property of type <code>SomeType</code> named <code>SomeProperty</code>. This functionality is based on the <idl>com.sun.star.beans.Introspection</idl> service. For additional details, see [[Documentation/DevGuide/AdvUNO/UNO Reflection API|UNO Reflection API]].  
+
In this pattern, {{AOo}} Basic offers a property of type <code>SomeType</code> named <code>SomeProperty</code>. This functionality is based on the <idl>com.sun.star.beans.Introspection</idl> service. For additional details, see [[Documentation/DevGuide/AdvUNO/UNO Reflection API|UNO Reflection API]].  
  
 
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:
 
+
<syntaxhighlight 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 45: Line 46:
 
    
 
    
 
   oExample.It = x ' Modify property It represented by XFoo3
 
   oExample.It = x ' Modify property It represented by XFoo3
 
+
</syntaxhighlight>
 
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.
 
+
<syntaxhighlight 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 61: 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"
 
+
</syntaxhighlight>
 
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>:
 
+
<syntaxhighlight 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 86: Line 87:
 
   oExample2.setPropertyValue( "Value2", y )
 
   oExample2.setPropertyValue( "Value2", y )
 
   oExample2.setPropertyValue( "Value3", z )
 
   oExample2.setPropertyValue( "Value3", z )
 
+
</syntaxhighlight>
 
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:
 
+
<syntaxhighlight 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 107: Line 108:
 
   ' replaceByName() sets the element Value1 to 42
 
   ' replaceByName() sets the element Value1 to 42
 
   oNameReplace.replaceByName( "Value1", 42 )
 
   oNameReplace.replaceByName( "Value1", 42 )
 
+
</syntaxhighlight>
 
{{PDL1}}
 
{{PDL1}}
  
[[Category:Documentation/Developers Guide/Professional UNO]]
+
[[Category:Documentation/Developer's Guide/Professional UNO]]

Latest revision as of 13:19, 23 December 2020



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