Using Variables

Sljedeće opisuje osnovno korištenje varijable u LibreOffice Basicu.

Imenovanje konvencija i identifikatora arijabli

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.

Primjer varijabli identifikatora:


    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'

Deklariranje varijabli

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.

Primjer deklariranja varijabli


    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 Icon

Jednom kada ste deklarirali varijable kao izvjesni tip, ne možete deklarirati varijable pod istim imenom ponovo a drugačijim tipom


Forcing Variable Declarations

Da bi prisilio deklaraciju varijabli, koristi sljedece komande:


Option Explicit

The Option Explicit statement has to be the first line in the module, before the first SUB. Generally, only arrays need to be declared explicitly. All other variables are declared according to the type-declaration character, or - if omitted - as the default type Single.

Tip varijabla

LibreOffice Basic podrzava 4 klase varijabla:

Cjelobrojne vrijednosti

Integralne vrijednosti nalaze se između -32768 i 32767. Ako određuje floating-point vrijednost na cjelobrojnu vrijednostm decimalna mjesta su zaokružena na sljedeći cijeli broj. Cjelobrojne vrijednosti su brzo izračunate u proceduri i pogodne su za računanje varijabla u petljama. Cjelobrojna vrijednost zahtjevaju samo 2 byta memorije."%" je znak tipa deklaracije-


Dim Variable%
Dim Variable As Integer

Long Integer Variables

Long Integer varijabla nalazi se između -2147483648 i 2147483647. Ako pridružite floating-point vrijednost u long integer vrijednost, decimalna mjesta su zaokružena na sljedeći cijeli broj. Long integer vrijednost su brzo izračunate u proceduri i su prikladni za brojalku varijablu u petlji za velike vrijednosti. A long integer varijabla zahtjeva 4 byta memorije. "&" u znakovnom tipu deklaracija-


Dim Variable&
Dim Variable As Long

Decimal Variables

Decimalne variable mogu imati pozitivne ili negativne brojeve ili nulu. Ispravnost je do 29 decimala.

Možete koristiti plus (+) ili minus (-) kao prefikse za decimalne brojeve (sa ili bez razmaka).

Ako je decimalni broj dodijeljen varijabli koja bi trebala sadržavati cijeli broj, LibreOffice Basic će zaokrušiti broj na višu ili nižu vrijednost.

Jednostavna varijabla

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

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

Valutne varijable su spremljene unutar 64-bitnih brojeva (8 byta) i su prikazane kao fiksni decimalni broj s 15 mjesta prije decimalne točke i 4 nakon decimalne točke. Vrijednost je smještena između -922337203685477.5808 i +922337203685477.5807. Valutne varijable se koriste računanje valutne vrijednosti a velikom točnošću. Znakovni tip deklaracije je "@"


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

Vrijednost znakovnog niza može sadržavati znak niza znakova do 65,535 znakova. Svaki znak je spremljen kao odgovarajuća Unicode vrijednosti. Varijable niza znakova su odgovarajuće za word procesing s programima i privremenim čuvanjem bilo kojeg znaka koji nije ispisan maksimalne veličine od 64 Kbyta. Memorija zahrjeva pohranu varijabli niza znakova ovisno o broju znakova u varijabli. Tip deklaracije znaka je"$".


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

Date Variables

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

Kada su varijable deklarirane, automatski je postavljeno na "nula" vrijednost. Bilježi sljedeću konvenciju.

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.

Polja

LibreOffice Basic zna jedan ili više dimenzionalno polje, definirano sa specificiranim tipom varijable. Polja su odgovarajuća za uređivanje listi i tabela u programima. Individualni elementi polja mogu biti adresirani kroz numeričke indekse

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.

Konstante

Konstante imaju fiksnu vrijednost. Definirane su samo jednom u programu i ne mogu biti redefinirane kasnije:


Const ConstName=Expression

Please support us!