Dim Statement

Declara unha variábel ou unha matriz.

Se as variábeis están separadas por comas (por exemplo, DIM sPar1, sPar2, sPar3 AS STRING), só poden definirse as variábeis variantes. Use unha liña de definición separada para cada variábel.


Dim sPar1 As String
Dim sPar2 As String
Dim sPar3 As String

Dim declara as variábeis locais dentro de métodos. As variábeis globais decláranse coa instrución PUBLIC ou PRIVATE.

Sintaxe:


[ReDim]Dim VarName [(start To end)] [As VarType][, VarName2 [(start To end)] [As VarType][,...]]

Parámetros:

VarName: Any variable or array name.

Start, End: Numerical values or constants that define the number of elements (NumberElements=(end-start)+1) and the index range.

Inicio e Fin poden ser expresións numéricas se se aplica ReDim a nivel de procedemento.

VarType: Key word that declares the data type of a variable.

Keyword: Variable type

Bool: Boolean variable (True, False)

Currency: Currency-Variable (Currency with 4 Decimal places)

Date: Date variable

Double: Double-precision floating-point variable (1,79769313486232 x 10E308 - 4,94065645841247 x 10E-324)

Integer: Integer variable (-32768 - 32767)

Long: Long integer variable (-2.147.483.648 - 2.147.483.647)

Object: Object variable (Note: this variable can only subsequently be defined with Set!)

Single: Single-precision floating-point variable (3,402823 x 10E38 - 1,401298 x 10E-45).

String: String variable consisting of a maximum of 64,000 ASCII characters.

[Variant]: Variant variable type (contains all types, specified by definition). If a key word is not specified, variables are automatically defined as Variant Type, unless a statement from DefBool to DefVar is used.

In LibreOffice Basic, you do not need to declare variables explicitly. However, you need to declare an array before you can use them. You can declare a variable with the Dim statement, using commas to separate multiple declarations. To declare a variable type, enter a type-declaration character following the name or use a corresponding key word.

LibreOffice Basic supports single or multi-dimensional arrays that are defined by a specified variable type. Arrays are suitable if the program contains lists or tables that you want to edit. The advantage of arrays is that it is possible to address individual elements according to indexes, which can be formulated as numeric expressions or variables.

As matrices decláranse coa instrución Dim. Existen dous métodos para definir o intervalo do índice:

DIM texto(20) as String REM 21 elementos numerados de 0 a 20

DIM texto(5 to 25) as String REM 21 elementos numerados de 5 a 25

DIM texto(-15 to 5) as String REM 21 elementos (incluído 0)

REM numerados de -15 a 5

Campo de datos bidimensionais

DIM texto(20,2) as String REM 63 elementos; de 0 a 20 no nivel 1, de 0 a 20 no nivel 2 e de 0 a 20 no nivel 3.

Pode declarar un tipo de matriz como dinámico se unha instrución ReDim define o número de dimensións no método ou función que contén a matriz. Normalmente a dimensión da matriz só se define unha vez e non é modificábel. Nun método, pode declarar unha matriz con ReDim. Só é posíbel definir as dimensións con expresións numéricas. Isto garante que os campos non excedan o tamaño necesario.

Exemplo:


Sub ExampleDim1
Dim sVar As String
Dim iVar As Integer
    sVar = "Office"
End Sub
 
Sub ExampleDim2
Campo de datos bidimensionais
Dim stext(20,2) As String
Const sDim as String = " Dimensión:"
For i = 0 To 20
    For ii = 0 To 2
        stext(i,ii) = str(i) & sDim & str(ii)
    Next ii
Next i
For i = 0 To 20
    For ii = 0 To 2
        MsgBox stext(i,ii)
    Next ii
Next i
End Sub

Precisamos da súa axuda!