目录
索引 🔎︎

# 使用变量

下面介绍 LibreOffice Basic 中变量的基本用法。

## 变量标识符的命名规范

变量名称最多可以含有 255 个字符。变量名称的第一个字符「必须」是字母 A-Z 或 a-z。变量名称中可以使用数字，但不能使用除下划线字符 ("_") 以外的标点符号和特殊字符。在 LibreOffice Basic 中，变量标志是不区分大小写的。变量名称可以含有空格，但如果含有空格，就必须将其放在方括号中。

变量标识符示例:

        MyNumber=5      'Correct'
        MyNumber5=15    'Correct'
        MyNumber_5=20   'Correct'
        My Number=20    'Not valid, variable with space must be enclosed in square brackets'
        [My Number]=12  'Correct'
        DéjàVu=25       'Not valid, special characters are not allowed'
        5MyNumber=12    'Not valid, variable may not begin with a number'
        Number,Mine=12  'Not valid, punctuation marks are not allowed'

## 声明变量

在 LibreOffice Basic 中，您不需要显式声明变量。变量声明时使用「Dim」语句。一次可以声明多个变量，但变量名称之间需要用逗号分隔。要定义变量类型，请在名称后使用类型声明符号，或者使用适当的关键字。

变量声明示例:

        Dim a$               'Declares the variable "a" as a String'
        Dim a As String      'Declares the variable "a" as a String'
        Dim a$, b As Integer 'Declares one variable as a String and one as an Integer'
        Dim c As Boolean     'Declares c as a Boolean variable that can be TRUE or FALSE'

一旦将某个变量声明为某种类型，就不能再将同名的变量声明为不同的类型！

When you declare multiple variables in a single line of code you need to specify the type of each variable. If the type of a variable is not explicitly specified, then Basic will assume that the variable is of the Variant type.

      ' Both variables "a" and "b" are of the Integer type
      Dim a As Integer, b As Integer
      ' Variable "c" is a Variant and "d" is an Integer
      Dim c, d As Integer
      ' A variable can also be explicitly declared as a Variant
      Dim e As Variant, f As Double

The Variant type is a special data type that can store any kind of value. To learn more, refer to the section The Variant type below.

### 强制变量声明

要强制声明变量，请使用以下命令:

    Option Explicit

「Option Explicit 语句必须是模块中的第一行，位于第一个子程序之前。通常，只有数组需要显式声明。所有其他变量都按照类型声明字符进行声明，而如果省略了类型声明字符，则声明为默认类型单精度」。

## 变量类型

LibreOffice Basic 支持四种变量:

- 「数字」变量可以含有数字值。有些变量用于存储较大或较小的数字，另一些则用于存储浮点数或分数。

- 「字符串」变量含有字符串。

- 「布尔」变量含有 TRUE 或 FALSE 值。

- 「对象」变量可以存储各种类型的对象，例如在文档中含有表格和文档。

### 整数变量

整数变量的范围从 -32768 到 32767。如果向整数变量指定浮点数值，小数部分将被转换成下一个整数。整数变量在过程中的计算速度非常快，适合用作循环中的计数器变量。整数变量只需要两个字节的内存。其类型声明字符是「%」。

    Dim Variable%
    Dim Variable As Integer

### 长整数变量

长整数变量的范围从 -2147483648 到 2147483647。如果向长整数变量指定浮点数值，小数部分将被转换成下一个整数。长整数变量在过程中的计算速度非常快，适合用作大值循环中的计数器变量。长整数变量需要四个字节的内存。其类型声明字符是「&」。

    Dim Variable&
    Dim Variable As Long

### 小数变量

小数变量可以取正值、负值或零值，精确度最多到 29 位。

您可以用加号 (+) 或减号 (-) 作为小数的前缀。 (之间的空格可有可无)。

如果将一个小数赋值给一个整型变量，LibreOffice Basic 向上或向下取整。

### 单精度变量

Single variables can take positive or negative values ranging from 3.402823 x 10E38 to 1.401298 x 10E-45. Single variables are floating-point variables, in which the decimal precision decreases as the non-decimal part of the number increases. Single variables are suitable for mathematical calculations of average precision. Calculations require more time than for Integer variables, but are faster than calculations with Double variables. A Single variable requires 4 bytes of memory. The type-declaration character is "!".

    Dim Variable!
    Dim Variable As Single

### 双精度变量

Double variables can take positive or negative values ranging from 1.79769313486232 x 10E308 to 4.94065645841247 x 10E-324. Double variables are floating-point variables, in which the decimal precision decreases as the non-decimal part of the number increases. Double variables are suitable for precise calculations. Calculations require more time than for Single variables. A Double variable requires 8 bytes of memory. The type-declaration character is "#".

    Dim Variable#
    Dim Variable As Double

### 货币变量

货币变量在内部存储为 64 位数字 (8 个字节)，并显示为固定位数的小数，其中含有 15 位非小数和 4 位小数。 取值范围从 -922337203685477.5808 到 +922337203685477.5807。货币变量用于计算货币值，且具有高精确度。 类型声明符是「@」。

    Dim Variable@
    Dim Variable As Currency

### Literals for integers

Numbers can be encoded using octal and hexadecimal forms.

      xi = &o13 '    8 + 3
      ci = &h65 ' 6*16 + 5
      MAX_Integer =  &o77777 '  32767 = &h7FFF
      MIN_Integer = &o100000 ' -32768 = &h8000
      MAX_Long = &h7fffffff '  2147483647 = &o17777777777
      MIN_Long = &h80000000 ' -2147483648 = &o20000000000

### 字符串变量

String variables can hold character strings with up to 2,147,483,648 characters. Each character is stored as the corresponding Unicode value. String variables are suitable for word processing within programs and for temporary storage of any non-printable character up to a maximum length of 2 Gbytes. The memory required for storing string variables depends on the number of characters in the variable. The type-declaration character is "$".

In BASIC String functions, the first character of the string has index 1.

    Dim Variable$
    Dim Variable As String

### 布尔变量

布尔变量只存储以下两个值之一: TRUE 或 FALSE。数字 0 的计算结果为 FALSE，其他任何值的计算结果均为 TRUE。

    Dim Variable As Boolean

### 日期变量

日期变量只能含有以内部格式存储的日期值和时间值。 通过 Dateserial、 Datevalue、 Timeserial 或者 Timevalue 赋于日期变量的值将自动转换为内部格式。 可用 Day、Month、 Year 或者 Hour、 Minute、Second 函数将日期变量转换为普通数字。 内部格式可以通过计算两个值之间的差比较日期/时间值。 这些变量只能用关键字「Date」声明。

    Dim Variable As Date

### Literals for Dates

Date literals allow to specify unambiguous date variables that are independent from the current language. Literals are enclosed between hash signs #. Possible formats are:

- #yyyy-mm-dd#

- #mm/dd/yyyy#

      zeroDay = #12/30/1899#
      dob = #2010-09-28#

## The Variant type

Variables declared as Variant can handle any data type. This means that the actual data type is defined during runtime as a value is assigned to the variable.

There are three main ways to create a Variant variable, as shown below:

      Dim varA            ' The type is not specified, hence the variable is a Variant
      Dim varB as Variant ' The variable is explicitly declared as a Variant
      varC = "abc"        ' Previously undeclared variables are treated as Variants

The example below uses the TypeName function to show how the type of a Variant variable changes upon assignment.

      Dim myVar As Variant
      MsgBox TypeName(myVar) ' Empty
      myVar = "Hello!"
      MsgBox TypeName(myVar) ' String
      myVar = 10
      MsgBox TypeName(myVar) ' Integer

A Variant variable is initialized with the Empty special data type. You can use the IsEmpty function to test if a variable is an Empty Variant.

You can also use the keyword Any to declare a variable as a Variant. However, Any is deprecated and is available for backward compatibility.

Arguments with type Variant or Any passed in function calls are not checked for their types.

      Dim myVar As Any ' Variable "myVar" is a Variant

## 变量初始值

只要一声明变量，就会自动将其设置为「NULL」值。请注意以下规范:

声明「数字」变量后将自动指定值「0」。

「日期」变量在内部被指定值 0，相当于使用 Day、Month、Year 或 Hour、Minute、Second 函数将其值转换为「0」。

「字符串」变量在声明时被指定为空字符串 ("")。

## 数组

LibreOffice Basic 可以通过指定的变量类型来定义一维和多维数组。数组适用于在程序中编辑列表和表格。数组中的各个元素可以通过数字索引来定位。

数组「必须」使用「Dim」语句进行声明。定义数组的索引范围时可以使用以下方法:

        Dim Text$(20)       '21 elements numbered from 0 to 20'
        Dim Text$(5,4)      '30 elements (a matrix of 6 x 5 elements)'
        Dim Text$(5 To 25)  '21 elements numbered from 5 to 25'
        Dim Text$(-15 To 5) '21 elements (including 0), numbered from -15 to 5'

索引范围可以包括正数与负数。

## 常数

常数有一个固定的数值，在程序中只能定义一次，不能重复定义:

    Const ConstName=Expression

相关主题

Basic Constants

Dim Statement