Aide:Dim.txt

De Wiki AutoIt Français
Aller à : navigation, rechercher

Document original V3.3.6.1 :

###Keyword###
Dim / Global / Local / Const

###Description###
Declare a variable, a constant, or create an array.

###Syntax###
<b>Dim</b> [Const] $variable [ = initializer ]
<b>Dim</b> [Const] $array<b>[</b>subscript 1<b>]</b>...<b>[</b>subscript n<b>]</b> [ = initializer ]

###Parameters###
@@ParamTable@@
const
	[optional] If present, the Const keyword creates a constant rather than a variable.
$variable
	The name of the variable/constant to declare.
initializer
	The value that will be initially assigned to the variable.  A Const must include the initializer.  The initializer can be a function call.
subscript
	The number of elements to create for the array dimension, indexed 0 to n-1.
@@End@@


###Remarks###
The Dim/Local/Global keywords perform similar functions:
1. Declare a variable before you use it (similar to VBScript)
2. Create an array

<b>Note</b>:  In AutoIt you can create a variable simply by assigning a value ($myvar = 0) but many people like to explicitly declare them.  If <b><a href="../functions/AutoItSetOption.htm#MustDeclareVars">AutoItSetOption("MustDeclareVars", 1)</a></b> is active, then variables must be declared prior to use.

You can also declare multiple variables on a single line:
<p class="code">Dim $a, $b, $c</p>
And initialize the variables:
<p class="code">Dim $a = 2, $b = 10, $c = 20</p>

Creating constants can be done in a similar way:
<p class="code">Const $a = 2, $b = 10, $c = 20
Dim Const $d = 21, $e = Exp(1)
Local Const $f = 5, $g = 7, $h = -2
</p>
Once created, you cannot change the value of a constant.  Also, you cannot change an existing variable into a constant.
<br>

To initialize an array, specify the values for each element inside square brackets, separated by commas.  For multiple dimensions, nest the initializers.  You can specify fewer elements in the initializer than declared, but not more.  Function calls can also be placed in the initializers of an array.  If the function call returns an array, then the one array element will contain that returned array.
<p class="code">Dim $Array1[12]=[3, 7.5, "string"], $array[5] = [8, 4, 5, 9, 1]
Dim $Grid[2][4]=[["Paul", "Jim", "Richard", "Louis"], [485.44, 160.68, 275.16, 320.00]]
Dim $Test[5] = [3, 1, StringSplit("Abe|Jack|Bobby|Marty", "|"), Cos(0)]
</p>

The difference between Dim, Local and Global is the scope in which they are created:
<b>Dim</b> = Local scope if the variable name doesn't already exist globally (in which case it reuses the global variable!)
<b>Global</b> = Forces creation of the variable in the Global scope
<b>Local</b> = Forces creation of the variable in the Local/Function scope

You should use Local or Global, instead of Dim, to explicitly state which scope is desired for a variable/constant/array.

When using variables, the local scope is checked first and then the global scope second.

When creating arrays you are limited to up to 64 dimensions and/or a total of 16 million elements.

A unique feature in AutoIt is the ability to copy arrays like this:
$mycopy = $myarray
In this case $mycopy will be an exact copy of $myarray and will have the same dimensions - no Dim statement is required to size the array first.  If <b><a href="../functions/AutoItSetOption.htm#MustDeclareVars">AutoItSetOption("MustDeclareVars", 1)</a></b> is active then the variable $mycopy would still need to be declared first, but would not need to be sized.  If the variable $mycopy was already an array or single value it will be erased before the copy takes place.

To erase an array (maybe because it is a large global array and you want to free the memory), simply assign a single value to it:
$array = 0
This will free the array and convert it back to the single value of 0.

Declaring the same variable name again will erase all array values and reset the dimensions to the new definition.  Declaring a variable with a simple value in the same scope will not change the value in the variable.

If you declare a variable with the same name as a parameter, using <b>Local</b> inside a user function, an error will occur.  <b>Global</b> can be used to assign to global variables inside a function, but if a local variable (or parameter) has the same name as a global variable, the local variable will be the only one used.  It is recommended that local and global variables have distinct names.


###Related###
UBound, ReDim, Static, <a href="../functions/AutoItSetOption.htm#MustDeclareVars">AutoItSetOption</a></b> 


###Example###
@@IncludeExample@@


Document traduit V3.3.6.1 :

###Keyword###
Dim / Global / Local / Const

###Description###
Déclare une variable, une constante ou crée un tableau.


###Syntax###
<b>Dim</b> [Const] $variable [ = initialisation ]
<b>Dim</b> [Const] $array<b>[</b>indice 1<b>]</b>...<b>[</b>indice n<b>]</b> [ = initialisation ]


###Parameters###
@@ParamTable@@
const
	[optionnel] Si présent, le mot-clé Const crée une constante plutôt qu'une variable.
$variable
	Le nom de la variable/constante à déclarer.
initialisation
	La valeur qui sera initialement assignée à la variable.  Un const doit inclure l'initialisation.  L'initialisation peut être un appel de fonction.
indice
	Le nombre d'éléments à créer pour la dimension du tableau, indexé de 0 à n-1.
@@End@@


###Remarks###
Les mots-clés Dim/Local/Global effectuent des fonctions similaires :
1. Déclare une variable avant de l'utiliser (similaire au VBScript)
2. Crée un tableau

<b>Note</b>:  Dans AutoIt, vous pouvez créer une variable simplement en lui assignant une valeur ($mavariable = 0) mais beaucoup de personnes aiment les déclarer explicitement.  Si <b><a href="../functions/AutoItSetOption.htm#MustDeclareVars">AutoItSetOption("MustDeclareVars", 1)</a></b> est actif, alors les variables doivent être déclarées avant d'être utilisées.

Vous pouvez aussi déclarer de multiples variables sur une même ligne :
<p class="code">Dim $a, $b, $c</p>
Et initialiser les variables :
<p class="code">Dim $a = 2, $b = 10, $c = 20</p>

Créer des constantes peut être fait de manière similaire :
<p class="code">Const $a = 2, $b = 10, $c = 20
Dim Const $d = 21, $e = Exp(1)
Local Const $f = 5, $g = 7, $h = -2
</p>
Une fois créée, vous ne pouvez plus changer la valeur d'une constante.  Aussi, vous ne pouvez pas changer une variable déjà existante en constante.
<br>

Pour initialiser un tableau, spécifiez les valeurs de chaque élément à l'intérieur de crochets et séparées d'une virgule.  Pour plusieurs dimensions, imbriquez les initialisations. Vous pouvez déclarer moins d'éléments dans l'initialisation que déclaré, mais pas plus. Les appels de fonctions peuvent aussi être placées dans l'initialisation d'un tableau. Si une fonction retourne un tableau, alors cet élément du tableau contiendra le tableau retourné.
<p class="code">Dim $Array1[12]=[3, 7.5, "string"], $array[5] = [8, 4, 5, 9, 1]
Dim $Grid[2][4]=[["Paul", "Jim", "Richard", "Louis"], [485.44, 160.68, 275.16, 320.00]]
Dim $Test[5] = [3, 1, StringSplit("Abe|Jack|Bobby|Marty", "|"), Cos(0)]
</p>

La différence entre Dim, Local and Global, est la portée dans laquelle elle est crée :
<b>Dim</b> = Portée locale si le nom de la variable n'existe pas déjà en Global (dans ce cas, la variable Global est réutilisée !)
<b>Global</b> = Force la création de la variable dans la portée globale
<b>Local</b> = Force la création de la variable dans la portée locale/d'une fonction 

Vous devriez utiliser Local ou Global à la place de Dim pour mentionner explicitement quelle portée est désirée pour une variable/constante/tableau.

Quand des variables sont utilisées, la portée Local est vérifiée en premier puis la portée Global en second.

Quand des tableaux sont créés, vous êtes limités à 64 dimensions et/ou un total de 16 millions d'éléments.

Une fonctionnalité unique dans AutoIt est la capacité de copier un tableau de cette manière :
$mycopy = $myarray
Dans ce cas, $mycopy sera une copie exacte de $myarray et aura les mêmes dimensions - aucune déclaration Dim est requise pour dimensionner le tableau en premier.  Si <b><a href="../functions/AutoItSetOption.htm#MustDeclareVars">AutoItSetOption("MustDeclareVars", 1)</a></b> est actif, alors la variable $mycopy doit toujours être déclarée en premier, mais n'aura pas le besoin d'être dimensionnée.  Si la variable $mycopy était déjà un tableau ou une valeur quelconque, AutoIt l'écrasera d'abord avant que la copie ait lieu.

Pour écraser un tableau (peut-être parce que c'est un gros "tableau global" et que vous voulez libérer la mémoire), assignez lui une valeur simple :
$array = 0
Cela libèrera le tableau et le convertira en la simple valeur de 0.

Déclarer le même nom de variable à nouveau écrasera toutes les valeurs du tableau et réinitialisera les dimensions à la nouvelle définition.  Déclarer une variable avec une valeur simple dans la même portée ne changera pas la valeur de la variable.

Si vous déclarez une variable avec le même nom qu'un paramètre en utilisant <b>Local</b> dans une fonction utilisateur, une erreur se produira.  <b>Global</b> peut être utilisé pour assigner les variables globales à l'intérieur d'une fonction, mais si une variable locale (ou un paramètre) a le même nom que la variable globale, alors la variable locale sera la seule utilisée. Il est recommandé que les variables locales et globales utilisent des noms différents.


###Related###
<a href="UBound.htm">UBound</a>, <a href="ReDim.htm">ReDim</a>, <a href="Static.htm">Static</a>, <a href="../functions/AutoItSetOption.htm#MustDeclareVars">AutoItSetOption</a></b> 


###Example###
@@IncludeExample@@


Traducteur : jbnh

Contributeurs (pour les modifications avant le Wiki) : Tlem