| View previous topic :: View next topic |
| Author |
Message |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 4021 Location: Lawrence, Kansas, USA
|
Posted: Thu Jan 22, 2004 8:54 am Post subject: MakePropertyValue function |
|
|
I have posted this useful OOo Basic function many times before. I'm going to post it one more time here, and then just link to its definition in the future.
| Code: | '----------
' Create and return a new com.sun.star.beans.PropertyValue.
'
Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue
Dim oPropertyValue As New com.sun.star.beans.PropertyValue
If Not IsMissing( cName ) Then
oPropertyValue.Name = cName
EndIf
If Not IsMissing( uValue ) Then
oPropertyValue.Value = uValue
EndIf
MakePropertyValue() = oPropertyValue
End Function
|
This function allows you to concisely express a sequence of com.sun.star.beans.PropertyValue's.
For instance, when calling the desktop object's method loadComponentFromURL() you can pass in an array of com.sun.star.beans.PropertyValue's.
(Note: In OOo Basic, the desktop object is conveniently available in a variable named StarDesktop.)
You typically see code in the SDK examples written something like this....
Example 1
| Code: | Dim aArgs() As com.sun.star.beans.PropertyValue
oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, aArgs() )
|
Or worse, like this, if you need to supply some actual property values...
Example 2
| Code: | Dim aArgs(1) As com.sun.star.beans.PropertyValue
aArgs(0).Name = "Hidden"
aArgs(0).Value = False
aArgs(1).Name = "AsTemplate"
aArgs(1).Value = False
oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, aArgs() )
|
Example 1 can be concisely and clearly rewritten as...
| Code: | | oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() ) |
The Array() function allows you to dynamically create an array on the fly. See a previous message I wrote on the topic of Array Semantics.
Example 2 can be rewritten more neatly using the Array() function in concert with my MakePropertyValue() function.
| Code: | oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0,_
Array(_
MakePropertyValue( "Hidden", False ),_
MakePropertyValue( "AsTemplate", False ) ) )
|
Lisp users will recognize the influence.
There are numerous other places in the OOo API where you need to supply an array of property values, and the above technique of using Array() combined with MakePropertyValue() is a concise way to write it. _________________ Want to make OOo Drawings like the colored flower design to the left?
Last edited by DannyB on Mon Sep 20, 2004 7:13 am; edited 1 time in total |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 4021 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 4021 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 4021 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 4021 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 4021 Location: Lawrence, Kansas, USA
|
Posted: Mon Sep 20, 2004 7:14 am Post subject: |
|
|
Thanks to B Marcelly for pointing out two errors in Example 1 and Example 2, in the first message in this thread. I have edited the first message (above) to correct these errors.
In the original MakePropertyValue function, as shown at the top of this thread, you'll notice that I use the Dim statement.....
Dim oPropertyValue As New com.sun.star.beans.PropertyValue
as the first line of the function.
For quite some time now, I've favored the following approach instead. I use the createUnoStruct() function instead of a Dim statement. Like this....
oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" )
The rewritten function now looks like this....
| Code: | Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue
oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" )
If Not IsMissing( cName ) Then
oPropertyValue.Name = cName
EndIf
If Not IsMissing( uValue ) Then
oPropertyValue.Value = uValue
EndIf
MakePropertyValue() = oPropertyValue
End Function
|
Reasons....
* For uniformity across languages, I'd rather use createUnoStruct(). I always write a counterpart to createUnoStruct() for every programming language that I use in conjunction with OOo.
* I just plain don't like the Dim statement, preferring dynamically allocated everything, having been corrupted by Lisp in my misspent youth. _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 4021 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|