Base Types and Structures


Most of the internal data used by OOo is based on standard types such as strings and integers. These types are combined into structures (called structs), which act like user -defined data types. The structures are combined to form the more complex UNO objects that are described later.

You must create a UNO structure before you can use it. The most common method to create a UNO structure is to use the format Dim As New. For example, to create a structure named aProp, use the following code:

 Dim aProp As New com.sun.star.beans.PropertyValue aProp.Name = "FirstName"       'Set the Name property aProp.Value = "Paterton"       'Set the Value property 

This method also allows you to create an array of structures in one statement-many UNO routines require an array of structures.

 Dim aProp(4) As New com.sun.star.beans.PropertyValue aProp(0).Name  = "FirstName"   'Set the Name property aProp(0).Value = "Clyde"       'Set the Value property 

Use the CreateUnoStruct function to dynamically create a UNO structure rather than declare it ahead of time. Dynamically creating a UNO structure allows the name of the structure to be provided at run time rather than compile time. Providing a name at run time looks like this:

 Dim aProp aProp = CreateUnoStruct("com.sun.star.beans.PropertyValue") aProp.Name  = "FirstName"      'Set the Name property aProp.Value = "Paterton"       'Set the Value property 

Providing a name at compile time looks like this:

 Dim aProp As New com.sun.star.beans.PropertyValue aProp.Name = "FirstName"       'Set the Name property aProp.Value = "Paterton"       'Set the Value property 

The With statement simplifies the process of setting the properties of a structure.

 Dim aProp(4) As New com.sun.star.beans.PropertyValue With aProp(0)   .Name = "FirstName"       'Set the Name property   .Value = "Paterton"       'Set the Value property End With 

The function CreateUnoStruct used to be the only method to create a UNO structure. It has been used less frequently since the introduction of the "Dim As New" syntax. The CreateObject function is a more general function than CreateUnoStruct. It is able to create instances of all types supported by the Basic internal factory mechanism. This includes user-defined types.

 Type PersonType   FirstName As String   LastName As String End Type Sub ExampleCreateNewType   Dim Person As PersonType   Person.FirstName = "Andrew"   Person.LastName  = "Pitonyak"   PrintPerson(Person)   Dim Me As Object   Me = CreateObject("PersonType")   Me.FirstName = "Andy"   Me.LastName  = "Pitonyak"   PrintPerson(Me) End Sub Sub PrintPerson(x)   Print "Person = " & x.FirstName & " " & x.LastName End Sub 

The CreateObject function accepts the same arguments as CreateUnoStruct, but it works for all supported types, whereas CreateUnoStruct works only for UNO structures. Therefore, there is no reason to use CreateUnoStruct rather than CreateObject .

 Dim aProp aProp = CreateObject("com.sun.star.beans.PropertyValue") aProp.Name = "FirstName"       'Set the Name property aProp.Value = "Paterton"       'Set the Value property 
Tip  

The Create Object function offers more flexibility than CreateUnoStruct to dynamically create objects at run time rather than at compile time. On the other hand, you can use the Dim As New syntax to create a structure at compile time.

The TypeName function indicates that a UNO structure is an object. Use the IsUnoStruct function to determine if a variable is a UNO structure.

 Dim aProp As New com.sun.star.beans.PropertyValue Print TypeName(aProp)     'Object Print IsUnoStruct(aProp)  'True 



OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net