<!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 - 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="val+en">Previous</a>&nbsp;
<a href="wait+en">Next</a>&nbsp;
</td></tr></table>
<div class="notab">
<h1>
Variable Declaration
</h1>
<div class="black"><font size="-2"><b>Syntax</b></font></div>
<pre class="syntax">[ <b>STATIC</b> ] { <b>PUBLIC</b> | <b>PRIVATE</b> } <u>Identifier</u> [ <u>Static array declaration</u> ] <b>AS</b> <u>Datatype</u> [ <b>=</b> <u>Expression</u> ]</pre><p>

This declares a class global variable.
<p>
<h2>Access</h2>
<p>
This variable is accessible everywhere in the class it is declared.
<p>
<ul>
<li>If the <a href="public+en">PUBLIC</a> keyword is specified, it is also accessible to the other    classes having a reference to an <a href="../def/object+en">object</a> of this class.
<p>
<li>If the  <a href="static+en">STATIC</a> keyword is specified, the same variable will be shared with every <a href="../def/object+en">object</a> of this class.
<p>
</ul>

<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">STATIC PUBLIC GridX AS Integer
STATIC PRIVATE bGrid AS Boolean
PUBLIC Name AS String
PRIVATE Control AS Object</pre>
<p>
<h2>Initialization</h2>
<p>
The variable can be initialized with any <u><a href="../comp/gb.eval/expression+en">Expression</a></u>.
<p>
<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">PRIVATE Languages AS String[] = [ &quot;fr&quot;, &quot;it&quot;, &quot;es&quot;, &quot;de&quot;, &quot;ja&quot; ]
PRIVATE 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>STATIC</b> ] { <b>PUBLIC</b> | <b>PRIVATE</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">STATIC PRIVATE Tasks AS NEW List
PRIVATE MyCollection AS NEW Collection(gb.Text)</pre>
<p>
Or you can initialize the variable with a native <a href="../def/dynamicarray+en">dynamic array</a>.
<p>
<div class="black"><font size="-2"><b>Syntax</b></font></div>
<pre class="syntax">[ <b>STATIC</b> ] { <b>PUBLIC</b> | <b>PRIVATE</b> } <u>Identifier</u> <b>AS NEW</b> <u>Native Datatype</u> <b>[</b> <u>Array dimensions</u> ... <b>]</b></pre><p>

Note that you can use any expression for specifying array dimensions.
<p>
<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">PUBLIC CONST WORD_MAX AS Integer = 12
PRIVATE Words AS NEW String[WORD_MAX * 2]
PUBLIC Matrix AS NEW Float[3, 3]</pre>
<p>
<hr><b>See also</b><br>
<a href="localdecl+en">Local Variable Declaration</a>&nbsp; <a href="methoddecl+en">Method Declaration</a>&nbsp; <a href="arraydecl+en">Array Declaration</a>&nbsp; <a href="type+en">Datatypes</a>&nbsp;

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

