Dim Statement

Declares variables or arrays.

If the variables are separated by commas - for example Dim v1, v2, v3 As String - first ones get defined as Variant variables. A new line, or colon sign (:), help separate variable definitions.


  Dim text As String
  Dim pv As com.sun.star.beans.PropertyValue, d As Date
  Dim Units as Integer : Dim EULER As Double

Dim declares local variables within subroutines. Global variables are declared with the Global, Public or the Private statement.

Syntax:

Dim Statement diagram


Dim variable [(start To end)] [As typename][, variable2[char] [(start To end)] [,...]]
tip

New operator is optional when setting Option Compatible option.


Parameters:

variable: Any variable or array name.

typename: Keyword that declares the data type of a variable.

primitive data types fragment

Byte: Byte variable (0-255)

Boolean: Boolean variable (True, False)

Currency: Currency variable (Currency with 4 Decimal places)

рдорд┐рддрд┐: рдЪрд▓ рдорд┐рддрд┐

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

рдЗрдиреНрдЯрд┐рдЬрд░: рдЪрд▓ рдЗрдиреНрдЯрд┐рдЬрд░ (-рейреиренремрео - рейреиренремрен)

рд▓рд╛рдореЛ: рд▓рд╛рдореЛ рдЗрдиреНрдЯрд┐рдЬрд░ (-реи.резрекрен.рекреорей.ремрекрео - реи.резрекрен.рекреорей.ремрекрен)

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

рдПрдХрд▓: рдПрдХрд▓-рд╢реБрджреНрдзрд┐рдХрд░рдг рдЙрддреНрдкреНрд▓рд╛рд╡рди-рдмрд┐рдиреНрджреБ рдЪрд▓ (рей,рекрежреиреореирей x резрежEрейрео - рез,рекрежрезреирепрео x резрежE-рекрел)

рд╕реНрдЯреНрд░рд┐рдЩ:рд╕реНрдЯреНрд░рд┐рдЩ рдЪрд▓рд▓реЗ рдЕрдзрд┐рдХрддрдо ремрек,режрежрежASCII рдХреНрдпрд╛рд░реЗрдХреНрдЯрд░рд╣рд░реВ рд╕рдорд╛рд╡реЗрд╢ рдЧрд░рд╛рдЙрдЫ ред

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

object: Universal Network object (UNO) object or ClassModule object instance.

char: Special character that declares the data type of a variable.

Type declaration characters fragment

In LibreOffice Basic, you do not need to declare variables explicitly. However, you need to declare arrays 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 type keyword name.

Declaration character

Variable type name

%

Integer

&

Long

!

Single

#

Double

$

String

@

Currency


array: Array declaration.

array fragment

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

start and end can be numerical expressions if ReDim is applied at the procedure level.

LibreOffice рдЖрдзрд╛рд░рднреВрдд рд╕рдорд░реНрдерди рдирд┐рд░реНрджрд┐рд╖реНрдЯ рдЧрд░рд┐рдПрдХрд╛ рдЪрд▓ рдкреНрд░рдХрд╛рд░рджреНрд╡рд╛рд░рд╛ рдкрд░рд┐рднрд╛рд╖рд┐рдд рдЧрд░рд┐рдПрдХрд╛ рдПрдХрд▓ рд╡рд╛ рдмрд╣реБ -рдЖрдпрд╛рдорд┐рдд рдПрд░реЗрд╣рд░реВ ред рддрдкрд╛рдИрдБрд▓реЗ рд╕рдореНрдкрд╛рджрди рдЧрд░реНрди рдЪрд╛рд╣рдиреБрднрдПрдХреЛ рдХрд╛рд░реНрдпрдХреНрд░рдо рд╕рд╛рдорд╛рдЧреНрд░реАрд╣рд░реВрдорд╛ рд╕реВрдЪреАрд╣рд░реВ рд╡рд╛ рддрд╛рд▓рд┐рдХрд╛рд╣рд░реВ рднрдПрдорд╛ рдПрд░реЗрд╣рд░реВ рдЕрдиреБрдХреВрд▓ рд╣реБрдиреНрдЫрдиреН ред рдПрд░реЗрд╣рд░реВрдХреЛ рдлрд╛рдЗрджрд╛ рддреНрдпреЛ рд╣реБрдиреНрдЫ рдХрд┐ рдЕрдиреБрдХреНрд░рдордгрд┐рдХрд╛ рдЕрдиреБрд╕рд╛рд░ рд╣рд░реЗрдХ рддрддреНрд╡рд╣рд░реВрдХреЛ рд╕рдореНрд╡реЛрдзрди рдЧрд░реНрди рд╕рдореНрднрд╡ рд╣реБрдиреНрдЫ, рдЬрд╕рд▓рд╛рдИ рд╕рдЩреНрдЦреНрдпрд╛рддреНрдордХ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рд╡рд╛ рдЪрд▓рд╣рд░реВрдХреЛ рд░реВрдкрдорд╛ рд╕реВрддреНрд░рдмрджреНрдз рдЧрд░реНрди рд╕рдХрд┐рдиреНрдЫ ред

Arrays are declared with the Dim statement. There are multiple ways to define the index range:


  Dim text(20) As String ' 21 elements numbered from 0 to 20
  Dim value(5 to 25) As Integer ' 21 values numbered from 5 to 25
  Dim amount(-15 to 5) As Currency ' 21 amounts (including 0), numbered from -15 to 5
  REM Two-dimensional data field
  Dim table$(20,2) ' 63 items; from 0 to 20 level 1, from 0 to 20 level 2 and from 0 to 20 level 3.

You can declare an array types as dynamic if a ReDim statement defines the number of dimensions in the subroutine or the function that contains the array. Generally, you can only define an array dimension once, and you cannot modify it. Within a subroutine, you can declare an array with ReDim. You can only define dimensions with numeric expressions. This ensures that the fields are only as large as necessary.

Example:


Sub ExampleDim1
Dim sVar As String
Dim iVar As Integer
    sVar = "Office"
End Sub
 
Sub ExampleDim2
  ' рджреНрд╡рд┐-рдЖрдпрд╛рдорд┐рдХ рдбреЗрдЯрд╛ рдлрд╛рдБрдЯ
    Dim stext(20,2) As String
  рд╕реНрдЯреНрд░рд┐рдЩрдХреЛ рд░реВрдкрдорд╛ Const sDim = " рдЖрдпрд╛рдорд┐рдХ:"
  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

Please support us!