ReDim utasítás

Declares or redefines variables or arrays.

Szintaxis:

ReDim Statement diagram


ReDim [Preserve] variable [(start To end)] [As type-name][, variable2 [(start To end)] [As type-name][,...]]

Optionally, add the Preserve keyword to preserve the contents of the array that is redimensioned. ReDim can only be used in subroutines.

Paraméterek:

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)

Date (dátum): Date (dátum) változó

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

Integer (egész szám): Integer (egész szám) változó (-32768 - 32767)

Long (hosszú egész): Long integer (hosszú egész szám) (-2147483648 - 2147483647)

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

Single (egyszeres pontosságú): Egyszeres pontosságú lebegőpontos változó (3.402823 x 10E38 - 1.401298 x 10E-45).

String (karakterlánc): String (karakterlánc) változó, amely maximum 64000 ASCII-karaktert tartalmazhat.

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

A LibreOffice Basicben nem kell explicit módon deklarálni a változókat. A tömböt használat előtt mindig deklarálni kell. Egy változót a Dim utasítással deklarálhat, több deklarációt vesszővel (,) kell elválasztani. Változótípus deklarálásához a név után adja meg a típusdeklarációs karaktert, vagy használja a megfelelő kulcsszót.

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.

A LibreOffice Basic az egy- vagy többdimenziós tömböket támogatja, amelyeket egy adott változótípus definiál. A tömböket akkor érdemes használni, ha a program szerkeszteni kívánt listákat vagy táblázatokat tartalmaz. A tömbök előnye, hogy az egyedi elemeket az indexekkel megcímezheti, amelyek numerikus kifejezések vagy változók lehetnek.

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.

A tömbtípusokat dinamikusként deklarálhatja, ha a ReDim utasítás megadja a dimenzió számát a tömböt tartalmazó szubrutinban vagy a függvényben. A tömbdimenziót általában csak egyszer lehet megadni, és nem lehet módosítani. Egy szubrutinon belül a tömböt ReDimmel deklarálhatja. A dimenziókat csak numerikus kifejezésekkel definiálhatja. Ez biztosítja, hogy a mezők csak olyan nagyok legyenek, amennyire szükséges.

Példa:


Sub ExampleRedim
    Dim iVar() As Integer, iCount As Byte
    ReDim iVar(5) As Integer
    For iCount = 1 To 5
        iVar(iCount) = iCount
    Next iCount
    ReDim iVar(10) As Integer
    For iCount = 1 To 10
        iVar(iCount) = iCount
    Next iCount
End Sub

Támogasson minket!