Menggunakan Variabel
Penjelasan berikut menerangkan cara guna dasar variabel pada LibreOffice Basic.
Penamaan Konvensi untuk Identifir Variabel
A variable name can consist of a maximum of 255 characters. The first character of a variable name must be a letter A-Z or a-z. Numbers can also be used in a variable name, but punctuation symbols and special characters are not permitted, with exception of the underscore character ("_"). In LibreOffice Basic variable identifiers are not case-sensitive. Variable names may contain spaces but must be enclosed in square brackets if they do.
Contoh untuk variabel pengenal:
MyNumber=5 |
Benar |
MyNumber5=15 |
Benar |
MyNumber_5=20 |
Benar |
My Number=20 |
Tidak sah, variabel dengan spasi harus digabung dengan kurung siku |
[My Number]=12 |
Benar |
Déjà Vu=25 |
Tidak sah, karakter spesial tidak diperbolehkan |
5MyNumber=12 |
Tidak sah, variabel tidak dimulai dengan angka |
Number,Mine=12 |
Tidak sah, tanda baca tidak diperbolehkan |
Mendeklarasikan Variabel
In LibreOffice Basic you don't need to declare variables explicitly. A variable declaration can be performed with the Dim statement. You can declare more than one variable at a time by separating the names with a comma. To define the variable type, use either a type-declaration sign after the name, or the appropriate key word.
Contoh untuk deklarasi variabel:
Dim a$ |
Mendeklarasikan variabel "a" sebagai sebuah String |
Dim a As String |
Mendeklarasikan variabel "a" sebagai sebuah String |
Dim a$, b As Integer |
Mendeklarasikan sebuah variabel sebagai String dan lainnya sebagai Integer |
Dim c As Boolean |
Mendeklarasikan c sebagai variabel Boolean yang dapat TRUE (benar) atau FALSE (salah) |
It is very important when declaring variables that you use the type-declaration character each time, even if it was used in the declaration instead of a keyword. Thus the following statements are invalid:
Dim a$ |
Mendeklarasikan "a" sebagai sebuah String |
a="TestString" |
Ketikan deklarasi yang hilang: "a$=" |

Sekali Anda mendeklarasikan sebuah variabel dalam jenis tertentu, Anda tidak dapat mendeklarasikan variabel tersebut lagi dalam nama yang sama dan jenis berbeda!
Memaksakan Deklarasi Variabel
Untuk memaksakan pernyataan variabel, gunakanlah perintah berikut:
Option Explicit
Pernyataan Option Explicit harus berada di baris pertama modul bersangkutan, sebelumnya SUB yang pertama. Umumnya, hanya susunan (array) yang perlu dinyatakan eksplisit. Sementara pernyataan bagi variabel lainnya bergantung pada karakter deklarasi yang diketikka, atau - apabila diabaikan - sebagai jenis utama Single (tunggal).
Jenis-jenis Variabel
LibreOffice Basic mendukung empat kelas variabel:
-
Variabel Numerik mengandung nilai-nilai angka. Beberapa variabel digunakan untuk menyimpan sejumlah besar maupun kecil angka, dan lainnya digunakan untuk titik ambang (floating-point) atau angka-angka hasil pembagian.
-
Variabel String mengandung string-string karakter.
-
Variabel Boolean mengandung nilai TRUE (benar) atau FALSE (salah).
-
Variabel Objek digunakan untuk menyimpan sejumlah jenis objek, seperti tabel dan dokumen di dalam sebuah dokumen.
Variabel Integer
Integer variables range from -32768 to 32767. If you assign a floating-point value to an integer variable, the decimal places are rounded to the next integer. Integer variables are rapidly calculated in procedures and are suitable for counter variables in loops. An integer variable only requires two bytes of memory. "%" is the type-declaration character.
Dim Variable%
Dim Variable As Integer
Variabel Integer Panjang
Long integer variables range from -2147483648 to 2147483647. If you assign a floating-point value to a long integer variable, the decimal places are rounded to the next integer. Long integer variables are rapidly calculated in procedures and are suitable for counter variables in loops for large values. A long integer variable requires four bytes of memory. "&" is the type-declaration character.
Dim Variable&
Dim Variable As Long
Variabel Desimal
Variabel desimal bisa berupa angka positif, negatif, atau kosong. Keakuratannya mencapai 29 digit.
Anda dapat memakai tanda tambah (+) atau kurang (-) sebagai awalan untuk angka desimal (dengan atau tanpa spasi).
Apabila angka desimal ditetapkan sebagai variabel integer (bilangan bulat), maka LibreOffice Basic akan membulatkan angka tersebut dengan pembulatan ke atas.
Variabel Tunggal
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
Variabel Ganda
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
Variabel Mata Uang
Currency variables are internally stored as 64-bit numbers (8 Bytes) and displayed as a fixed-decimal number with 15 non-decimal and 4 decimal places. The values range from -922337203685477.5808 to +922337203685477.5807. Currency variables are used to calculate currency values with a high precision. The type-declaration character is "@".
Dim Variable@
Dim Variable As Currency
Variabel String
String variables can hold character strings with up to 65,535 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 64 Kbytes. The memory required for storing string variables depends on the number of characters in the variable. The type-declaration character is "$".
Dim Variable$
Dim Variable As String
Variabel Boolean
Variabel boolean hanya memiliki dua nilai: TRUE (benar) atau FALS (salah). Angka 0 berarti salah, sedangkan lainnya berarti benar.
Dim Variable As Boolean
Variabel Tanggal
Date variables can only contain dates and time values stored in an internal format. Values assigned to Date variables with Dateserial, Datevalue, Timeserial or Timevalue are automatically converted to the internal format. Date-variables are converted to normal numbers by using the Day, Month, Year or the Hour, Minute, Second function. The internal format enables a comparison of date/time values by calculating the difference between two numbers. These variables can only be declared with the key word Date.
Dim Variable As Date
Inisial Nilai Variabel
Segera setelah variabel dideklarasikan, maka ia akan secara otomatis diset menjadi nilai "Null" (kosong). Perhatikanlah konvensi berikut:
Variabel Numerik secara otomatis ditetapkan sebagai nilai "0" segera setelah dideklarasikan.
Date variables are assigned the value 0 internally; equivalent to converting the value to "0" with the Day, Month, Year or the Hour, Minute, Second function.
Variabel string ditetapkan sebagai string kosong ("") saat mereka dideklarasikan.
Susunan
LibreOffice Basic knows one- or multi-dimensional arrays, defined by a specified variable type. Arrays are suitable for editing lists and tables in programs. Individual elements of an array can be addressed through a numeric index.
Susunan (atau array) harus dideklarasikan bersama pernyataan Dim. Berikut beberapa cara untuk mendefinisikan jangkauan indeks dari sebuah susunan (array):
Dim Text$(20) |
21 elemen dinomori dari 0 sampai 20 |
Dim Text$(5,4) |
30 elemen (metriks dari 6 x 5 elemen) |
Dim Text$(5 To 25) |
21 elemen dinomori dari 5 sampai 25 |
Dim Text$(-15 To 5) |
21 elemen (termasuk 0), dinomori dari -15 sampai 5 |
Jangkauan indeks bisa memiliki angka-angka positif dan negatif.
Konstanta
Konstanta memiliki nilai tetap. Bilangan ini hanya bisa ditentukan satu kali dan tidak dapat diubah lagi di lain waktu:
Const ConstName=Expression