Contents

# 使用變數

下面介紹 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 陳述式必須位於模組的第一行，且在第一個 SUB 之前。通常，只有陣列需要明確地宣告。所有其他變數都依類型宣告字元進行宣告，或者如果省略了類型宣告字元，則宣告為預設類型 單精度型變數。

## 變數類型

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#

      start_date = #1899-12-30# ' = 0
      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

## 初始變數值

只要變數一經宣告，就會自動將其設定為「空」值。請注意以下慣例：

只要數值型變數一經宣告，就會自動為其指定值「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

Please support us!