结构映射
From Apache OpenOffice Wiki
< Zh | Documentation
可以通过 Dim As New
命令将 UNO 结构类型实例化为单个实例和数组。
' 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
对于实例化的多态结构类型,Dim As New
命令有一个特殊语法,给定类型时用作字符串文字而不是名称:
Dim o As New "com.sun.star.beans.Optional<long>"
表示 UNO 名称的字符串文字按照以下规则构成:
- 表示相应的 UNO 简单类型的字符串分别为
"boolean"
、"byte"
、"short"
、"long"
、"hyper"
、"float"
、"double"
、"char"
、"string"
、"type" 和 "any"
。 - 表示 UNO 序列类型的字符串为 "
[]
",后跟表示组件类型的字符串。 - 表示 UNO 枚举、普通结构或接口类型的字符串是该类型的名称。
- 表示实例化多态结构类型的字符串是多态结构类型模板的名称,后跟 "<"、类型参数表示(各参数之间用 "," 分隔)和 ">"。
这些字符串中表示不会引入伪空格或其他字符。
UNO 结构实例的处理方式与 UNO 对象类似。可以使用 . 运算符访问结构成员。支持 Dbg_Properties
属性。不支持属性 Dbg_SupportedInterfaces
和 Dbg_Methods
,因为它们不适用于结构:
' 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"
对象与结构不同。对象是作为引用进行处理的,而结构是作为值进行处理的。将结构指定到变量时,结构被复制。修改身为结构的对象属性时,这非常重要,因为在读取和修改对象属性之后,必须将一个结构属性重新指定到该对象。
在以下示例中,oExample
是一个对象,其具有属性 MyObject
和 MyStruct
。
MyObject
提供的对象支持字符串属性ObjectName
。MyStruct
提供的结构支持字符串属性StructName
。
oExample.MyObject.ObjectName
和 oExample.MyStruct.StructName
都应该修改。以下代码示意如何对一个对象执行此操作:
' Accessing the object Dim oObject oObject = oExample.MyObject oObject.ObjectName = “Tim” ' Ok! ' or shorter oExample.MyObject.ObjectName = “Tim” ' Ok!
而以下代码示意如何对结构(以及可能的错误)正确执行此操作:
' 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). |