<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../style.css">
<title>
Gambas Documentation - Local Variable Declaration
</title>
</head>
<table class="none" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td align="left">
<font size="-1">
<a href="../../help+en"><img class="flag" alt="Home" border="0" src="../../img/lang/en.png" align="center"></a>&nbsp;
<a href="../lang+en">Up</a>&nbsp;
<a href="link+en">Previous</a>&nbsp;
<a href="lock+en">Next</a>&nbsp;
</td></tr></table>
<div class="notab">
<h1>
Local Variable Declaration
</h1>
<div class="black"><font size="-2"><b>Syntax</b></font></div>
<pre class="syntax"><b>DIM</b> <u>Identifier</u> <b>AS</b> <u>Datatype</u> [ <b>=</b> <u>Expression</u> ]</pre><p>

Declare a local variable in a procedure or function.
<p>
This variable is only accessible to the procedure or function where it is declared.
<p>
<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">DIM iVal AS Integer
DIM sName AS String
DIM hObject AS Object
DIM hCollection AS Collection</pre>
<p>
<h2>Initialization</h2>
<p>
The variable can be initialized with any expression.
<p>
<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">DIM bCancel AS Boolean = TRUE
DIM Languages AS String[] = [ &quot;fr&quot;, &quot;it&quot;, &quot;es&quot;, &quot;de&quot;, &quot;ja&quot; ]
DIM DefaultLanguage AS String = Languages[1]</pre>
<p>
Alternatively, you can initialize the variable with a newly instanciated <a href="../def/object+en">object</a>.
<p>
<div class="black"><font size="-2"><b>Syntax</b></font></div>
<pre class="syntax"><b>DIM</b> <u>Identifier</u> <b>AS NEW</b> <u>Class</u> <b>(</b> <u>Arguments</u> ... <b>)</b></pre><p>

<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">DIM aTask AS NEW String[]
DIM aCollection AS NEW Collection(gb.Text)</pre>
<p>
Or you can initialize the variable with a native <a href="../def/dynamicarray+en">dynamic array</a>.
See <a href="arraydecl+en">Array Declaration</a> for more information.
<p>
<h2>Multiple Declarations</h2>
<p>
You can declare several variables on the same line:
<ul>
<li>Each declaration must be separated by a comma.
<li>You can specify the <a href="../def/identifier+en">identifier</a> only. It will have the same declaration as the first next <a href="../def/identifier+en">identifier</a> having a complete declaration.
<p>
</ul>

<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">DIM Text AS String, Matrix AS NEW Float[3, 3]
DIM X, Y, W, H AS Integer</pre>
<p>

<hr><b>See also</b><br>
<a href="arraydecl+en">Array Declaration</a>&nbsp; <a href="vardecl+en">Variable Declaration</a>&nbsp; <a href="type+en">Datatypes</a>&nbsp; <a href="../doc/naming+en">Naming Conventions</a>&nbsp;

</div>
</body>
</html>

