Mainīgo lietošana

Sekojošais apraksta mainīgo pamata lietojumu iekš LibreOffice Basic.

Nosaukšanas vienošanās mainīgo identifikatoriem

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.

Mainīgo identifikatoru piemēri:


    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'

Mainīgo deklarēšana

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.

Piemēri mainīgo deklarēšanām:


    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'
Brīdinājuma ikona

Once you have declared a variable as a certain type, you cannot declare the variable under the same name again as a different type!


Uzspiest mainīgo deklarēšanas

Lai uzspiestu mainīgo deklarēšanu, lietojiet sekojošu komandu:


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.

Mainīgo tipi

LibreOffice Basic atbalsta četras mainīgo klases:

Vesela skaitļa mainīgie

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

Garie vesela skaitļa mainīgie

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

Decimālie mainīgie

Decimālie mainīgie var pieņemt pozitīvus vai negatīvus skaitļus vai nulli. Precizitāte ir līdz 29 cipariem.

Jūs varat lietot pluss (+) vai mīnuss (-) zīmes kā priedēkļus decimālajiem skaitļiem (ar vai bez atstarpēm).

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

Vienkāršie mainīgie

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

Dubultie mainīgie

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

Valūtas mainīgie

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

Virknes mainīgie

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

Būla mainīgie

Būla mainīgie uzglabā tikai vienu no divām vērtībām: TRUE vai FALSE. Skaitlis 0 novērtējas uz FALSE, jebkura cita vērtība novērtējas uz TRUE.


Dim Variable As Boolean

Datuma mainīgie

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

Sākotnējās mainīgo vērtības

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

Skaitliskajiem mainīgajiem automātiski tiek piešķirta vērtība "0", tiklīdz tie tiek deklarēti.

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.

Virknes mainīgajiem tiek piešķirta tukša virkne (""), kad tie tiek deklarēti.

Masīvi

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'

Indeksa diapazons var iekļaut pozitīvus, kā arī negatīvus skaitļus.

Konstantes

Konstantēm ir fiksēta vērtība. Tās tiek definētas tikai vienreiz programmā un nevar tikt pārdefinētas vēlāk:


Const ConstName=Expression

Please support us!