変数の使用法

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ステートメントで行います。変数名をコンマで区切ることで、1度に複数の変数を宣言できます。変数型を指定するには、変数名に続けて、型宣言子ないしは該当するキーワードを付けます。

下記は変数宣言の例です:


    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'
warning

特定の変数型として宣言した変数名は、別の型で再宣言することはできません。


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
note

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 ステートメントは、モジュールの 1 行目に置き、最初の SUB の前に記述する必要があります。通常こうした明示的な宣言をする必要があるのは、配列だけです。その他の変数は、型宣言子を使って型宣言をすればよく、型宣言が省略された場合はデフォルトで 単精度型 とされます。

変数型

LibreOffice Basicのサポートする変数には4つのクラスがあります。

整数変数

整数変数には -32768 から 32767 までの整数を収めることができます。こうした整数変数に浮動小数点型の数値を代入すると、小数点以下を丸めた整数値が収められます。整数変数は、プロシージャー内で高速計算が可能であり、またループカウンター用の変数としても適しています。整数変数が消費するメモリ量は、2 バイトです。型宣言用の記号は「%」です。


Dim Variable%
Dim Variable As Integer

ロング整数変数

ロング整数変数には -2147483648 から 2147483647 までの整数を収めることができます。このロング整数変数に浮動小数点型の数値を代入すると、小数点以下を丸めた整数値が収められます。ロング整数変数は、プロシージャー内で高速計算が可能であり、またループカウンター用の変数としても適しています。ロング整数変数が消費するメモリ量は4バイトです。型宣言用の記号は「&」です。


Dim Variable&
Dim Variable As Long

10 進数の変数

10 進数の変数は、正の数、負の数、または 0 を受け取ることができます。 桁数は 29 桁までです。

10 進数には、プラス (+) またはマイナス (-) 記号をプレフィックスとして使用できます (スペースあり、またはスペースなし)。

整数変数に10進数が代入される場合、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 "$".

tip

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



Dim Variable$
Dim Variable As String

ブール型変数

ブール型変数には、TRUE (真) および FALSE (偽) という 2 つの値の一方のみを収めることができます。数値 0 は FALSE に評価され、その他すべての値は TRUE に評価されます。


Dim Variable As Boolean

日付変数

日付変数には、日付と時刻を示す値を内部形式で収めることができます。 日付変数へ値を代入する際に、DateserialDatevalueTimeserialTimevalue を使用すると、自動的に内部形式へ変換されます。 日付変数の値を通常の数値に変換するには、DayMonthYear の各関数および HourMinuteSecond の各関数を使用できます。 時刻や日付は、内部形式に変換することにより、時刻間の比較計算など行えます。 日付変数は、キーワード 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:


  start_date = #12/30/1899# ' = 1
  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
note

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.

warning

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」が代入されます。これは、DayMonthYear の各関数および HourMinuteSecond の各関数を使用して値を「0」に変換した場合と同じです。

文字列変数 には、宣言後に空の文字列 ("") が代入されます。

配列

LibreOffice Basicでは1次元および多次元配列を使用することができ、変数宣言をする際に変数型を指定します。配列は、プログラム中でリストやテーブルを操作する場合に適しています。配列の各要素の指定は、数値によるインデックスで行います。

配列の宣言には、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

ご支援をお願いします!