Difference between revisions of "Documentation/DevGuide/ProUNO/Basic/Mapping of Structs"

From Apache OpenOffice Wiki
Jump to: navigation, search
m
m
(3 intermediate revisions by 3 users not shown)
Line 7: Line 7:
 
|NextPage=Documentation/DevGuide/ProUNO/Basic/Mapping of Enums and Constant Groups
 
|NextPage=Documentation/DevGuide/ProUNO/Basic/Mapping of Enums and Constant Groups
 
}}
 
}}
[[zh:Zh/Documentation/DevGuide/ProUNO/Basic/Mapping of Structs]]
+
{{Documentation/DevGuideLanguages|Documentation/DevGuide/ProUNO/Basic/{{SUBPAGENAME}}}}
 
{{DISPLAYTITLE:Mapping of Structs}}
 
{{DISPLAYTITLE:Mapping of Structs}}
 
UNO struct types can be instantiated with the <code>Dim As New</code> command as a single instance and array.  
 
UNO struct types can be instantiated with the <code>Dim As New</code> command as a single instance and array.  
 
+
<source lang="oobas">
 
   ' Instantiate a Property struct
 
   ' Instantiate a Property struct
 
   Dim aProperty As New com.sun.star.beans.Property
 
   Dim aProperty As New com.sun.star.beans.Property
Line 16: Line 16:
 
   ' Instantiate an array of Locale structs
 
   ' Instantiate an array of Locale structs
 
   Dim Locales(10) As New com.sun.star.lang.Locale
 
   Dim Locales(10) As New com.sun.star.lang.Locale
 
+
</source>
 
For instantiated polymorphic struct types, there is a special syntax of the <code>Dim As New</code> command, giving the type as a string literal instead of as a name:
 
For instantiated polymorphic struct types, there is a special syntax of the <code>Dim As New</code> command, giving the type as a string literal instead of as a name:
 
+
<source lang="oobas">
 
   Dim o As New "com.sun.star.beans.Optional<long>"
 
   Dim o As New "com.sun.star.beans.Optional<long>"
 
+
</source>
 
The string literal representing a UNO name is built according to the following rules:
 
The string literal representing a UNO name is built according to the following rules:
  
Line 30: Line 30:
 
No spurious spaces or other characters may be introduced into these string representations.
 
No spurious spaces or other characters may be introduced into these string representations.
  
UNO struct instances are handled like UNO objects. Struct members are accessed using the . operator. The <code>Dbg_Properties</code> property is supported. The properties <code>Dbg_SupportedInterfaces</code> and <code>Dbg_Methods</code> are not supported because they do not apply to structs.
+
UNO struct instances are handled like UNO objects. Struct members are accessed using the . operator. The <code>Dbg_Properties</code> property is supported. The properties <code>Dbg_SupportedInterfaces</code> and <code>Dbg_Methods</code> are not supported because they do not apply to structs. Inspectors <b>[[Extensions_development_basic#Xray_tool|Xray tool]] or [http://extensions.services.openoffice.org/project/MRI MRI tool]</b> can display UNO struct instances.
 
+
<source lang="oobas">
 
   ' Instantiate a Locale struct
 
   ' Instantiate a Locale struct
 
   Dim aLocale As New com.sun.star.lang.Locale
 
   Dim aLocale As New com.sun.star.lang.Locale
Line 40: Line 40:
 
   ' Access “Language” property
 
   ' Access “Language” property
 
   aLocale.Language = "en"
 
   aLocale.Language = "en"
 
+
</source>
 
Objects and structs are different. Objects are handled as references and structs as values. When structs are assigned to variables, the structs are copied. This is important when modifying an object property that is a struct, because a struct property has to be reassigned to the object after reading and modifying it.
 
Objects and structs are different. Objects are handled as references and structs as values. When structs are assigned to variables, the structs are copied. This is important when modifying an object property that is a struct, because a struct property has to be reassigned to the object after reading and modifying it.
  
Line 46: Line 46:
  
 
* The object provided by <code>MyObject</code> supports a string property <code>ObjectName</code>.
 
* The object provided by <code>MyObject</code> supports a string property <code>ObjectName</code>.
* The struct provided by <code>MyStruct</code> supports a string property StructName.  
+
* The struct provided by <code>MyStruct</code> supports a string property <code>StructName</code>.  
 
Both <code>oExample.MyObject.ObjectName</code> and <code>oExample.MyStruct.StructName</code> should be modified. The following code shows how this is done for an object:
 
Both <code>oExample.MyObject.ObjectName</code> and <code>oExample.MyStruct.StructName</code> should be modified. The following code shows how this is done for an object:
 
+
<source lang="oobas">
 
   ' Accessing the object
 
   ' Accessing the object
 
   Dim oObject
 
   Dim oObject
 
   oObject = oExample.MyObject
 
   oObject = oExample.MyObject
   oObject.ObjectName = “Tim” ' Ok!
+
   oObject.ObjectName = “Tim”           ' Ok!
 
    
 
    
 
   ' or shorter
 
   ' or shorter
 
+
 
 
   oExample.MyObject.ObjectName = “Tim” ' Ok!
 
   oExample.MyObject.ObjectName = “Tim” ' Ok!
 
+
</source>
 
The following code shows how it is done correctly for the struct (and possible mistakes):
 
The following code shows how it is done correctly for the struct (and possible mistakes):
 
+
<source lang="oobas">
 
   ' Accessing the struct
 
   ' Accessing the struct
 
   Dim aStruct
 
   Dim aStruct
 
   aStruct = oExample.MyStruct ' aStruct is a copy of oExample.MyStruct!
 
   aStruct = oExample.MyStruct ' aStruct is a copy of oExample.MyStruct!
   aStruct.StructName = “Tim” ' Affects only the property of the copy!
+
   aStruct.StructName = “Tim” ' Affects only the property of the copy!
 
    
 
    
 
   ' If the code ended here, oExample.MyStruct wouldn't be modified!
 
   ' If the code ended here, oExample.MyStruct wouldn't be modified!
Line 72: Line 72:
 
   ' only a temporary copy of the struct is modified!
 
   ' only a temporary copy of the struct is modified!
 
   oExample.MyStruct.StructName = “Tim” ' WRONG! oExample.MyStruct is not modified!
 
   oExample.MyStruct.StructName = “Tim” ' WRONG! oExample.MyStruct is not modified!
 
+
</source>
  
 
{{PDL1}}
 
{{PDL1}}
  
 
[[Category:Documentation/Developer's Guide/Professional UNO]]
 
[[Category:Documentation/Developer's Guide/Professional UNO]]

Revision as of 06:40, 1 May 2016



UNO struct types can be instantiated with the Dim As New command as a single instance and array.

  ' Instantiate a Property struct
  Dim aProperty As New com.sun.star.beans.Property
 
  ' Instantiate an array of Locale structs
  Dim Locales(10) As New com.sun.star.lang.Locale

For instantiated polymorphic struct types, there is a special syntax of the Dim As New command, giving the type as a string literal instead of as a name:

  Dim o As New "com.sun.star.beans.Optional<long>"

The string literal representing a UNO name is built according to the following rules:

  • The strings representing the relevant simple UNO types are "boolean", "byte", "short", "long", "hyper", "float", "double", "char", "string", "type", and "any", respectively.
  • The string representing a UNO sequence type is "[]" followed by the string representing the component type.
  • The string representing a UNO enum, plain struct, or interface type is the name of that type.
  • The string representing an instantiated polymorphic struct type is the name of the polymorphic struct type template, followed by "<", followed by the representations of the type arguments (separated from one another by ","), followed by ">".

No spurious spaces or other characters may be introduced into these string representations.

UNO struct instances are handled like UNO objects. Struct members are accessed using the . operator. The Dbg_Properties property is supported. The properties Dbg_SupportedInterfaces and Dbg_Methods are not supported because they do not apply to structs. Inspectors Xray tool or MRI tool can display UNO struct instances.

  ' Instantiate a Locale struct
  Dim aLocale As New com.sun.star.lang.Locale
 
  ' Display properties
  MsgBox aLocale.Dbg_Properties
 
  ' Access “Language” property
  aLocale.Language = "en"

Objects and structs are different. Objects are handled as references and structs as values. When structs are assigned to variables, the structs are copied. This is important when modifying an object property that is a struct, because a struct property has to be reassigned to the object after reading and modifying it.

In the following example, oExample is an object that has the properties MyObject and MyStruct.

  • The object provided by MyObject supports a string property ObjectName.
  • The struct provided by MyStruct supports a string property StructName.

Both oExample.MyObject.ObjectName and oExample.MyStruct.StructName should be modified. The following code shows how this is done for an object:

  ' Accessing the object
  Dim oObject
  oObject = oExample.MyObject
  oObject.ObjectName = “Tim”           ' Ok!
 
  ' or shorter
 
  oExample.MyObject.ObjectName = “Tim” ' Ok!

The following code shows how it is done correctly for the struct (and possible mistakes):

  ' Accessing the struct
  Dim aStruct
  aStruct = oExample.MyStruct ' aStruct is a copy of oExample.MyStruct!
  aStruct.StructName = “Tim”  ' Affects only the property of the copy!
 
  ' If the code ended here, oExample.MyStruct wouldn't be modified!
 
  oExample.MyStruct = aStruct ' Copy back the complete struct! Now it's ok!
 
  ' Here the other variant does NOT work at all, because 
  ' only a temporary copy of the struct is modified!
  oExample.MyStruct.StructName = “Tim” ' WRONG! oExample.MyStruct is not modified!
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages