1<?xml version="1.0" encoding="UTF-8"?>
2
3
4<!--***********************************************************
5 *
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements.  See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership.  The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License.  You may obtain a copy of the License at
13 *
14 *   http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied.  See the License for the
20 * specific language governing permissions and limitations
21 * under the License.
22 *
23 ***********************************************************-->
24
25
26
27<helpdocument version="1.0">
28<meta>
29<topic id="textsbasicshared03102100xml" indexer="include" status="PUBLISH">
30<title id="tit" xml-lang="en-US">Dim Statement [Runtime]</title>
31<filename>/text/sbasic/shared/03102100.xhp</filename>
32</topic>
33<history>
34<created date="2003-10-31T00:00:00">Sun Microsystems, Inc.</created>
35<lastedited date="2004-12-09T11:37:40">converted from old format - fpe</lastedited>
36</history>
37</meta>
38<body>
39<section id="dimtext">
40<bookmark xml-lang="en-US" branch="index" id="bm_id3149812"><bookmark_value>Dim statement</bookmark_value>
41<bookmark_value>arrays; dimensioning</bookmark_value>
42<bookmark_value>dimensioning arrays</bookmark_value>
43</bookmark>
44<paragraph role="heading" id="hd_id3149812" xml-lang="en-US" level="1" l10n="U" oldref="1"><link href="text/sbasic/shared/03102100.xhp" name="Dim Statement [Runtime]">Dim Statement [Runtime]</link></paragraph>
45<paragraph role="paragraph" id="par_id3143271" xml-lang="en-US" l10n="U" oldref="2">Declares a variable or an array.</paragraph>
46</section>
47<paragraph role="paragraph" id="par_id3154686" xml-lang="en-US" l10n="U" oldref="3">If the variables are separated by commas (for example, DIM sPar1, sPar2, sPar3 AS STRING), only Variant variables can be defined. Use a separate definition line for each variable.</paragraph>
48<paragraph role="code" id="par_id3156422" xml-lang="en-US" l10n="U" oldref="4">DIM sPar1 AS STRING</paragraph>
49<paragraph role="code" id="par_id3159252" xml-lang="en-US" l10n="U" oldref="5">DIM sPar2 AS STRING</paragraph>
50<paragraph role="code" id="par_id3153142" xml-lang="en-US" l10n="U" oldref="6">DIM sPar3 AS STRING</paragraph>
51<paragraph role="paragraph" id="par_id3152576" xml-lang="en-US" l10n="U" oldref="7">Dim declares local variables within subroutines. Global variables are declared with the PUBLIC or the PRIVATE statement.</paragraph>
52<paragraph role="heading" id="hd_id3156443" xml-lang="en-US" level="2" l10n="U" oldref="8">Syntax:</paragraph>
53<paragraph role="paragraph" id="par_id3149412" xml-lang="en-US" l10n="U" oldref="9">[ReDim]Dim VarName [(start To end)] [As VarType][, VarName2 [(start To end)] [As VarType][,...]]</paragraph>
54<paragraph role="heading" id="hd_id3147397" xml-lang="en-US" level="2" l10n="U" oldref="10">Parameters:</paragraph>
55<paragraph role="paragraph" id="par_id3154730" xml-lang="en-US" l10n="U" oldref="11">
56<emph>VarName:</emph> Any variable or array name.</paragraph>
57<paragraph role="paragraph" id="par_id3147125" xml-lang="en-US" l10n="CHG" oldref="12">
58<emph>Start, End:</emph> Numerical values or constants that define the number of elements (NumberElements=(end-start)+1) and the index range.<comment>UFI: deleted the range numbers, #i36558#</comment></paragraph>
59<paragraph role="paragraph" id="par_id3153877" xml-lang="en-US" l10n="U" oldref="13">Start and End can be numerical expressions if ReDim is applied at the procedure level.</paragraph>
60<paragraph role="paragraph" id="par_id3153510" xml-lang="en-US" l10n="U" oldref="14">
61<emph>VarType:</emph> Key word that declares the data type of a variable.</paragraph>
62<paragraph role="paragraph" id="par_id3154015" xml-lang="en-US" l10n="U" oldref="15">
63<emph>Keyword:</emph> Variable type</paragraph>
64<paragraph role="paragraph" id="par_id3153949" xml-lang="en-US" l10n="U" oldref="16">
65<emph>Bool:</emph> Boolean variable (True, False)</paragraph>
66<paragraph role="paragraph" id="par_id3156275" xml-lang="en-US" l10n="U" oldref="17">
67<emph>Currency:</emph> Currency-Variable (Currency with 4 Decimal places)</paragraph>
68<paragraph role="paragraph" id="par_id3156057" xml-lang="en-US" l10n="U" oldref="18">
69<emph>Date:</emph> Date variable</paragraph>
70<paragraph role="paragraph" id="par_id3148405" xml-lang="en-US" l10n="U" oldref="19">
71<emph>Double:</emph> Double-precision floating-point variable (1,79769313486232 x 10E308 - 4,94065645841247 x 10E-324)</paragraph>
72<paragraph role="paragraph" id="par_id3148916" xml-lang="en-US" l10n="U" oldref="20">
73<emph>Integer:</emph> Integer variable (-32768 - 32767)</paragraph>
74<paragraph role="paragraph" id="par_id3150045" xml-lang="en-US" l10n="U" oldref="21">
75<emph>Long:</emph> Long integer variable (-2.147.483.648 - 2.147.483.647)</paragraph>
76<paragraph role="paragraph" id="par_id3149255" xml-lang="en-US" l10n="U" oldref="22">
77<emph>Object:</emph> Object variable (Note: this variable can only subsequently be defined with Set!)</paragraph>
78<paragraph role="paragraph" id="par_id3155937" xml-lang="en-US" l10n="U" oldref="23">
79<emph>Single:</emph> Single-precision floating-point variable (3,402823 x 10E38 - 1,401298 x 10E-45).</paragraph>
80<paragraph role="paragraph" id="par_id3151251" xml-lang="en-US" l10n="U" oldref="24">
81<emph>String:</emph> String variable consisting of a maximum of 64,000 ASCII characters.</paragraph>
82<paragraph role="paragraph" id="par_id3154704" xml-lang="en-US" l10n="U" oldref="25">
83<emph>[Variant]:</emph> 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.</paragraph>
84<paragraph role="paragraph" id="par_id3146316" xml-lang="en-US" l10n="U" oldref="26">In $[officename] 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.</paragraph>
85<paragraph role="paragraph" id="par_id3149924" xml-lang="en-US" l10n="U" oldref="27">$[officename] 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.</paragraph>
86<paragraph role="paragraph" id="par_id3148488" xml-lang="en-US" l10n="U" oldref="28">Arrays are declared with the Dim statement. There are two methods to define the index range:</paragraph>
87<paragraph role="paragraph" id="par_id3154662" xml-lang="en-US" l10n="U" oldref="29">DIM text(20) as String REM 21 elements numbered from 0 to 20</paragraph>
88<paragraph role="paragraph" id="par_id3155604" xml-lang="en-US" l10n="U" oldref="30">DIM text(5 to 25) as String REM 21 elements numbered from 5 to 25</paragraph>
89<paragraph role="paragraph" id="par_id3151274" xml-lang="en-US" l10n="U" oldref="31">DIM text(-15 to 5) as String REM 21 elements (including 0)</paragraph>
90<paragraph role="paragraph" id="par_id3152774" xml-lang="en-US" l10n="U" oldref="32">REM numbered from -15 to 5</paragraph>
91<paragraph role="paragraph" id="par_id3150829" xml-lang="en-US" l10n="U" oldref="33">Two-dimensional data field</paragraph>
92<paragraph role="paragraph" id="par_id3149529" xml-lang="en-US" l10n="U" oldref="34">DIM text(20,2) as String REM 63 elements; form 0 to 20 level 1, from 0 to 20 level 2 and from 0 to 20 level 3.</paragraph>
93<paragraph role="paragraph" id="par_id3159239" xml-lang="en-US" l10n="U" oldref="35">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.</paragraph>
94<paragraph role="heading" id="hd_id3150344" xml-lang="en-US" level="2" l10n="U" oldref="36">Example:</paragraph>
95<paragraph role="paragraph" id="par_id3150206" xml-lang="en-US" l10n="U" oldref="37">Sub ExampleDim1</paragraph>
96<paragraph role="paragraph" id="par_id3154201" xml-lang="en-US" l10n="U" oldref="38">Dim sVar As String</paragraph>
97<paragraph role="paragraph" id="par_id3146134" xml-lang="en-US" l10n="U" oldref="39">Dim iVar As Integer</paragraph>
98<paragraph role="paragraph" id="par_id3154657" xml-lang="en-US" l10n="CHG" oldref="40">sVar = "Office"</paragraph>
99<paragraph role="paragraph" id="par_id3148459" xml-lang="en-US" l10n="U" oldref="41">End Sub</paragraph>
100<paragraph role="paragraph" id="par_id3166424" xml-lang="en-US" l10n="U" oldref="43">Sub ExampleDim2</paragraph>
101<paragraph role="paragraph" id="par_id3149036" xml-lang="en-US" l10n="U" oldref="44">REM Two-dimensional data field</paragraph>
102<paragraph role="paragraph" id="par_id3149737" xml-lang="en-US" l10n="U" oldref="45">Dim stext(20,2) as String</paragraph>
103<paragraph role="paragraph" id="par_id3153782" xml-lang="en-US" l10n="U" oldref="46">Const sDim as String = " Dimension:"</paragraph>
104<paragraph role="paragraph" id="par_id3150518" xml-lang="en-US" l10n="U" oldref="48">for i = 0 to 20</paragraph>
105<paragraph role="paragraph" id="par_id3166428" xml-lang="en-US" l10n="U" oldref="49">for ii = 0 to 2</paragraph>
106<paragraph role="paragraph" id="par_id3152994" xml-lang="en-US" l10n="U" oldref="50">stext(i,ii) = str(i) &amp; sDim &amp; str(ii)</paragraph>
107<paragraph role="paragraph" id="par_id3150202" xml-lang="en-US" l10n="U" oldref="51">next ii</paragraph>
108<paragraph role="paragraph" id="par_id3154370" xml-lang="en-US" l10n="U" oldref="52">next i</paragraph>
109<paragraph role="paragraph" id="par_id3156166" xml-lang="en-US" l10n="U" oldref="54">for i = 0 to 20</paragraph>
110<paragraph role="paragraph" id="par_id3148815" xml-lang="en-US" l10n="U" oldref="55">for ii = 0 to 2</paragraph>
111<paragraph role="paragraph" id="par_id3146981" xml-lang="en-US" l10n="U" oldref="56">msgbox stext(i,ii)</paragraph>
112<paragraph role="paragraph" id="par_id3155125" xml-lang="en-US" l10n="U" oldref="57">next ii</paragraph>
113<paragraph role="paragraph" id="par_id3154528" xml-lang="en-US" l10n="U" oldref="58">next i</paragraph>
114<paragraph role="paragraph" id="par_id3155087" xml-lang="en-US" l10n="U" oldref="59">End Sub</paragraph>
115</body>
116</helpdocument>
117