Difference between revisions of "Zh/Documentation/DevGuide/ProUNO/Basic/Mapping of Simple Types"
m |
m |
||
Line 7: | Line 7: | ||
|NextPage=Zh/Documentation/DevGuide/ProUNO/Basic/Mapping of Sequences and Arrays | |NextPage=Zh/Documentation/DevGuide/ProUNO/Basic/Mapping of Sequences and Arrays | ||
}} | }} | ||
− | + | {{Documentation/DevGuideLanguages|Documentation/DevGuide/ProUNO/Basic/{{SUBPAGENAME}}}} | |
{{DISPLAYTITLE:简单类型映射}} | {{DISPLAYTITLE:简单类型映射}} | ||
Latest revision as of 02:53, 14 May 2009
一般来说,OpenOffice.org Basic 类型系统并不严格。与 C++ 和 Java 不同,OpenOffice.org Basic 不要求声明变量,除非使用了强制进行声明的 Option Explicit
命令。要声明变量,使用的是 Dim
命令。此外,还可以通过 Dim
命令有选择地指定 OpenOffice.org Basic 类型。一般语法是:
Dim VarName [As Type][, VarName [As Type]]...
在未指定类型的情况下,声明的所有变量都为 Variant
类型。可以将任意 Basic 类型的值指定给类型为 Variant
的变量。未声明的变量为 Variant,除非将类型后缀与其名称一起使用。后缀也可以在 Dim
命令中使用。下表包含 Basic 所支持的类型及其相应后缀的一个完整列表:
类型 | 后缀 | 范围 |
---|---|---|
Boolean | True or False | |
Integer | % | -32768 to 32767 |
Long | & | -2147483648 to 2147483647 |
Single | ! | 浮点数
负数: -3.402823E38 到 -1.401298E-45 正数: 1.401298E-45 |
Double | # | 双精度浮点数
负数: -1.79769313486232E308 到 -4.94065645841247E-324 正数: 4.94065645841247E-324 |
Currency | @ | 带有四位小数的固定点数
-922,337,203,685,477.5808 到 922,337,203,685,477.5807 |
Date | 01/01/100 to 12/31/9999 | |
Object | Basic 对象 | |
String | $ | 字符串 |
Variant | 任意 Basic 类型 |
请看以下 Dim 示例
Dim a, b ' Type of a and b is Variant Dim c as Variant ' Type of c is Variant Dim d as Integer ' Type of d is Integer (16 bit!) ' The type only refers to the preceding variable Dim e, f as Double ' ATTENTION! Type of e is Variant! ' Only the type of f is Double Dim g as String ' Type of g is String Dim i as Date ' Type of g is Date ' Usage of Postfixes Dim i% ' is the same as Dim i as Integer Dim d# ' is the same as Dim d as Double Dim s$ ' is the same as Dim s as String
下面的关系表用于将 UNO 中的类型映射成 Basic 中的类型,反过来也可以。
UNO | Basic |
---|---|
void | 内部类型 |
boolean | Boolean |
byte | Integer |
short | Integer |
unsigned short | 内部类型 |
long | Long |
unsigned long | 内部类型 |
hyper | 内部类型 |
unsigned hyper | 内部类型 |
float | Single |
double | Double |
char | 内部类型 |
string | String |
type | com.sun.star.reflection.XIdlClass |
any | Variant |
简单 UNO 类型 type 被映射成 com.sun.star.reflection.XIdlClass 接口,以获取类型特有的信息。如果需要详细信息,请参阅 高级 UNO - 语言绑定 - UNO 反射 API。
访问 UNO 方法或属性且已知目标 UNO 类型时,Basic 会自动选择相应的类型:
' The UNO object oExample1 has a property "Count" of type short a% = 42 oExample1.Count = a% ' a% has the right type (Integer) pi = 3,141593 oExample1.Count = pi ' pi will be converted to short, so Count will become 3 s$ = "111" oExample1.Count = s$ ' s$ will be converted to short, so Count will become 111
有时,OpenOffice.org Basic 不知道所需的目标类型,尤其是在接口方法或属性的参数类型为 any 时。在这种情况下,OpenOffice.org Basic 会机械地将 OpenOffice.org Basic 类型转换成上表中所示的 UNO 类型,而实际可能需要一种不同的类型。OpenOffice.org Basic 提供的唯一机制是自动向下转换数值:
Long 和 Integer 值通常被转换为尽可能最短的整数类型:
- 如果 -128 <= Value <= 127, 转换为
byte
- 如果 -32768 <= Value <= 32767, 转换为
short
如果 Single/Double
值不带有小数位,则以同样的方式转换为整数。
之所以使用此机制,是因为 OpenOffice.org 中用于实现 UNO 功能的某些内部 C++ 工具提供自动向上转换,但不提供向下转换。因此,将一个 byte
值传递到一个需要 long
值的接口会成功,但反之却不会成功。
在以下示例中,oNameCont
是一个对象,支持 com.sun.star.container.XNameContainer 并包含 short
类型的元素。假定 FirstValue
为一个有效项。
a% = 42 oNameCount.replaceByName( "FirstValue", a% ) ' Ok, a% is downcasted to type byte b% = 123456 oNameCount.replaceByName( "FirstValue", b% ) ' Fails, b% is outside the short range
方法调用失败,因此,该实现应该抛出相应的异常,OpenOffice.org Basic RTL 将把该异常转换为 OpenOffice.org Basic 错误。一个实现接受不适合的类型而且不抛出异常也是可能的事。可以利用没有超出目标范围的数字值,或者在应用到 UNO 之前将它们转换为正确的 Basic 类型,来确保使用的值适合其 UNO 目标。
通常使用类型 Variant
而不是使用类型 Object
来声明 UNO Basic 对象变量。OpenOffice.org Basic 类型 Object
适合于纯粹的 OpenOffice.org Basic 对象,而不适合于 UNO OpenOffice.org Basic 对象。Variant
变量非常适合于 UNO Basic 对象,避免了类型 的 OpenOffice.org Basic 特有行为将会导致的问题:
Dim oService1 ' Ok oService1 = CreateUnoService( "com.sun.star.anywhere.Something" ) Dim oService2 as Object ' NOT recommended oService2 = CreateUnoService( "com.sun.star.anywhere.SomethingElse" )
Content on this page is licensed under the Public Documentation License (PDL). |