Bruk av Variabler

I dette avsnittet finner du grunnleggende informasjon om bruk av variabler i LibreOffice Basic.

Navgivingskonvensjoner for variabelnavn

Et variabelnavn kan inneholde maksimalt 256 tegn. Det første tegnet i et variabelnavn må være en bokstav i det engelske alfabetet A-Z eller a-z. De andre tegnene kan være en bokstav, et tall eller understrek («_»). Ingen andre tegn er tilatt. LibreOffice Basic skiller ikke mellom store og små bokstavar i variabelnavnene. Et variabelnavn kan også inneholde mellomrom, men må da skrives mellom hakeparenteser.

Eksempel på variabelnavn:

MyNumber=5

Rett

MyNumber5=15

Rett

MyNumber_5=20

Rett

My Number=20

Ugyldig. Variabelnavn med mellomrom må skrives mellom hakeparenteser

[My Number]=12

Rett

DéjàVu=25

Ikke gyldig, spesialtegn ikke tillatt

5MyNumber=12

Ikke gyldig, variabel kan ikke starte med et tall

Number,Mine=12

Ikke gyldig, punktum er ikke tillatt


Definere Variabler

I LibreOffice Basic trenger du ikke å spesifikt deklarere variabler. En variabeldeklarasjon can utføres medDim uttrykket. Du kan deklarere mer enn en variabel samtidig , gjennom å separere de med kommategn. For å definere variabeltypen, bruk enten en type-dekalrasjon etter navnet, eller et passende nøkkelord.

Eksempler for variabeldeklarasjoner:

Dim a$

Deklarerer variablen som en streng

Dim a As String

Deklarerer variabelen som en streng

Dim a$, b As Integer

Deklarerer en variabel som Stren og en som heltall

Dim c As Boolean

Deklarerer c som en boolsk (logisk) variabel som kan være SANN eller FALSK


Det er veldig viktig at du bruker type- deklarasjon tegn når du deklarerer variabler, selv om brukt i deklarasjonen istedenfor et nøkkelord. Derfor er følgene uttrykk ugyldige.

Dim a$

Deklarerer "a" som en Streng

a="TestString"

Typedeklarasjonen mangler "a$="


Advarselsikon

NÃ¥r du har deklarert en variabel som en spesifik type, kan du ikke deklarere samme variabelnavn med en annen type


Tving Variabeldeklarasjoner

For tvingende deklarasjon av variabler, bruk følgende kommando:

Option Explicit

Uttrykket Option Explicit må være på den første linjen i modulen, foran den første SUB-prosedyren. Normalt skal bare tabeller («array») deklarerest spesielt. Alle andre variablar blir deklarerte med typedeklarasjonstegnet eller, dersom dette er sløyfet, som standardtypen Single.

Variabeltyper

LibreOffice Basic har støtte for fire variabelklasser:

Heltallsvariabler

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

Long Integer Variables

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

Decimal Variables

Decimal variables can take positive or negative numbers or zero. Accuracy is up to 29 digits.

You can use plus (+) or minus (-) signs as prefixes for decimal numbers (with or without spaces).

If a decimal number is assigned to an integer variable, LibreOffice Basic rounds the figure up or down.

Bruk av Variabler

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

Bruk av Variabler

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

Currency Variables

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

Bruk av Variabler

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

Boolean Variables

Boolean variables store only one of two values: TRUE or FALSE. A number 0 evaluates to FALSE, every other value evaluates to TRUE.

Dim Variable As Boolean

Bruk av Variabler

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

Initial Variable Values

As soon as the variable has been declared, it is automatically set to the "Null" value. Note the following conventions:

Numeric variables are automatically assigned the value "0" as soon as they are declared.

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.

String variables are assigned an empty-string ("") when they are declared.

Arrays

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.

Arrays must be declared with the Dim statement. There are several ways to define the index range of an array:

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


The index range can include positive as well as negative numbers.

Constants

Constants have a fixed value. They are only defined once in the program and cannot be redefined later:

Const ConstName=Expression