Difference between revisions of "Documentation/DevGuide/ProUNO/Basic/Mapping of Structs"
m (1 revision(s)) |
|||
(7 intermediate revisions by 4 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 | ||
}} | }} | ||
+ | {{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. | ||
− | + | <syntaxhighlight 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 15: | 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 | ||
− | + | </syntaxhighlight> | |
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: | ||
− | + | <syntaxhighlight lang="oobas"> | |
Dim o As New "com.sun.star.beans.Optional<long>" | Dim o As New "com.sun.star.beans.Optional<long>" | ||
− | + | </syntaxhighlight> | |
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 29: | 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 [https://extensions.services.openoffice.org/project/MRI MRI tool]</b> can display UNO struct instances. |
− | + | <syntaxhighlight 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 39: | Line 40: | ||
' Access “Language” property | ' Access “Language” property | ||
aLocale.Language = "en" | aLocale.Language = "en" | ||
− | + | </syntaxhighlight> | |
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 45: | 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: | ||
− | + | <syntaxhighlight 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! | ||
− | + | </syntaxhighlight> | |
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): | ||
− | + | <syntaxhighlight 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 71: | 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! | ||
− | + | </syntaxhighlight> | |
{{PDL1}} | {{PDL1}} | ||
− | [[Category: Professional UNO]] | + | |
+ | [[Category:Documentation/Developer's Guide/Professional UNO]] |
Latest revision as of 13:39, 23 December 2020
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 propertyObjectName
. - The struct provided by
MyStruct
supports a string propertyStructName
.
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). |