Mapping of Structs

From Apache OpenOffice Wiki
Jump to: navigation, search

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
  ' Instantiate an array of Locale structs
  Dim Locales(10) As New

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 "<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
  ' 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