ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

This content has been translated automatically.  Click here  to view the French version.
Help / WLanguage / WLanguage syntax / Declaring variables
  • Overview
  • How to declare a variable?
  • Declaring a variable
  • Declaring a variable and inializing it
  • Declaring several variables
  • Detailed syntaxes
  • Examples
  • Attributes that can be used when declaring an Variable
  • Attribute "useful"
  • Attribute "immutable"
  • Attribute "color"
  • Critical section" attribute
  • How to access the properties of a variable?
  • Syntaxes
  • Example
  • Rule for variable scope
  • Rule
  • Exception
  • Special case: Reports & Queries
  • Name conflict: ambiguous use of a type
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Overview
A variable is defined by name and type.
The variable type defines the values that can be taken by the variable, its memory footprint and the available operations.
Reminder: Each type is identified by a WLanguage keyword. These keywords are reserved words.
This help page presents:
To simplify the declaration of variables, you also have the ability to use the type inference.
How to declare a variable?

Declaring a variable

<Nom de la variable> est un(e) <Type>

Declaring a variable and inializing it

<Nom de la variable> est un(e) <Type> = <Valeur initiale>

Declaring several variables

<Nom de la variable 1>, <Nom de la variable 2> sont des <Type>

Detailed syntaxes

<Variable name>:Name of the variable to declare.
<Type>:Type of the variable or type common to the declared variables (see Available types of variables).
<Initial value>: Initial value of the variable (optional). This initial value depends on the type of variable.
Remarks:
  • The a and an keywords are optional: they provide better readability.
  • When several variables of the same type are declared and initialized on the same line, only the last variable is initialized.
    To declare and initialize several variables at the same time, use the multiple initialization.
  • It is forbidden to use several variables with the same name in an event/process (regardless of the type of these variables).

Examples

  • Declaring simple variables:
    NomClient is string
    Compteur is int
    Prix is real
    i,j,k are int

    The type inference allows you to use the following syntaxes:
    let Montant = 1500.69 // type réel
    let Ville = "Montpellier" // type chaîne
  • Declaration of an Variable type Border:
    MonCadre is Border
Declaring several variables and inializing them
<Nom de la variable 1>, <Nom de la variable 2>, ..., <Nom de la variable N> sont <Type>
= (<Valeur initiale 1>, <Valeur initiale 2>, ..., <Valeur initiale M>)
Details of syntax
<Variable name>:Name of the variable to declare.
N represents the number of declared variables.
<Type>:Type common to the declared variables (see Available types of variables).
<Initial value>: Initial value of each variable.
M represents the number of values to assign.
Remarks:
  • The assignments are performed from left to right.
  • If the number of variables is less than the number of values (N less than M), a compilation error will occur.
  • If the number of variables is greater than the number of values (N greater than or equal to M), only the first variables will be assigned.
  • You also have the ability to assign several variables with different values in a single line of code. For more details, see Multiple assignment.
Examples:
x, y, z are int = (1, 10, 4)
// x vaut 1, y vaut 10, z vaut 4

s, t, p are strings = ("A", "B001")
// s vaut "A", t vaut "B001", p vaut ""
Attributes that can be used when declaring an Variable

Attribute "useful"

The "useful" attribute can be used when declaring a variable to specify that it is used in the project. This notation avoids displaying the warning indicating that a local variable is unused. In this case, the following syntax must be used:
<Nom de la variable> est un(e) <Type>, utile [ = "Raison"]

<Nom de la variable> est un(e) <Type> <utile [ = "Raison"]>
"Reason" is a note for the developer.
Example:
n1 is int, useful = "Variable nécessaire pour le Webservice" 

n2 is int <useful = "Variable nécessaire pour le Webservice">
New in version 2024

Attribute "immutable"

The "immutable" attribute can be used when declaring a variable to indicate that the variable is immutable, i.e. that its value will not change once assigned. Unlike a constant, an immutable value can be defined by the result of a procedure/function. The following syntax must be used:
<Nom de la variable> est un(e) <Type> = <Valeur>, immuable

<Nom de la variable> est un(e) <Type> <immuable> = <Valeur>
where Value is the mandatory value of the variable.
Example:
n1 is int = 5, immuable

n2 is int <immutable> = 6

Remark: The "immutable" attribute is only available for simple types (string, numeric, boolean and immutable member).
New in version 2024

Attribute "color"

The "color" attribute can be used when declaring an Variable to define the color of this Variable in the code editor. The following syntax must be used:
<Nom de la variable> est un(e) <Type> <couleur = <Valeur>>
where Value corresponds to the new color of the Variable. This color can correspond to:
  • a pre-defined WLanguage color (constants LightRed, DarkGreen, etc.). For example:
    MaCouleur
  • a keyword corresponding to the element coloring defined in the Code Editor themes (see Code editor themes. These keywords are:
    ConstantWLanguagecolor used by Default for WLanguage constants.
    FunctionWLanguagecolor used by Default for WLanguage functions.
    Numbercolor used by Default for numbers.
    Stringcolor used by Default for character strings.
    VariableWLanguagecolor used by Default for variables.
    Warningcolor corresponding to warnings.

    Example:
    MaChaîneColorée is string <color = Warning>
  • an color in string form. Example:
    MonParamètre est une chaîne <couleur = #FF00CC>

Critical section" attribute

When declaring a variable, this variable can be associated with a critical section by using the critical section attribute.
The syntax is as follows:
VariableName is VariableType <critical section>

or

VariableName is VariableType, critical section
The code sections that use these variables must be found between CriticalSectionStart and CriticalSectionEnd.
Special case: A critical section is automatically associated with the variables on which simple operations are performed, such as:
  • assigning a value.
  • retrieving a value.
  • incrementat, decrement (+, -, ++, --, +=, -= operators +, -, ++, --, += -=).
Example:
// Global declarations of WIN_STAT window
gcySum is currency, critical section
gcyMax is currency, critical section
...
// Code run by several threads
cyOrderAmount is currency 
...
// atomic operation, the critical section is automatically managed by WLanguage
gcySum += cyAmountOrder

// multiple operation, it is necessary to implement the critical section explicitly
utiliser CriticalSection(gcyMax) IN
	IF cyAmountOrder > gcyMax THEN
		gcyMax = cyAmountOrder
	END
END

Remarks:
  • The critical section attribute is allowed for:
    • the global project variables, set of procedures, window, page and report.
    • the local variables.
    • the members of classes.
    • the arrays: in this case, the attribute is associated with the array and not with the array elements.
  • The queues and the stacks are protected by default: the critical section attribute is not required.
How to access the properties of a variable?

Syntaxes

The following syntaxes can be used if the type of variable includes properties:
  • Assigning a property of the variable:
    <Nom de la variable>.<Propriété> = <Valeur>
  • Reading a property of the variable:
    <Nom de la variable>.<Propriété>

Example

Using properties on a Border type:
// Définition des caractéristiques du cadre
MonCadre is Border
MonCadre.Color = LightRed
MonCadre.Thickness = 5
Rule for variable scope

Rule

The rule for variable scope is as follows:
  • If a variable "global" to the project and a variable "global" to a window have the same name:
    • the variable "global" to the window will be used in all the events or processes of the window and window controls as well as in its "local" procedures.
    • the variable "global" to the project will be used in all the other processes.
  • If a variable "global" to the project and a variable "local" to a process have the same name:
    • the "local" variable will only be used in the process where this variable was declared.
    • the variable "global" to the project will be used in all the other processes.
  • If a variable "global" to a window and a variable "local" to a process of this window have the same name:
    • the "local" variable will only be used in the process where this variable was declared.
    • the variable "global" to the window will be used in all other events or processes of the window and its controls (as well as in its "local" procedures).
    • none of these two variables can be used in the rest of the project.
Remark: The variables are specific to the executable, the web service or the WEBDEV session where they have been declared.
Compilation option
A compilation option is used to manage the scope of the local variables: Scope of local variables limited to the current block.
If this option is selected, the local variables will be specific to the block. You cannot use a local variable outside the block in which it is declared. The ending of the variable is run at the end of block (destructors and freeing memory).
You have the ability to redeclare a variable with the same name in two distinct sub-blocks but you cannot redeclare a variable with the same name in a child sub-block.
This option is selected by default for the new projects.
To modify this option:
  1. Open the project description window: on the "Project" tab, in the "Project" group, click "Description".
  2. On the "Compilation" tab, check or uncheck "Scope of local variables limited to the current block".

Exception

The rule for variable scope does not apply to the constants and to the Data Source variables.

Special case: Reports & Queries

  • The variables global to the project can be used in the reports and queries created and/or modified in "Reports & Queries".
  • The items for which "Visible to the end user in "Reports and Queries"" is checked in the data model editor can be used in Reports and Queries. If this option is unchecked, the field can only be used indirectly (the user does not have the option of completing the field, and the field is not available in Wizards, description windows, etc.).

Name conflict: ambiguous use of a type

A WLanguage type can have the same name as another type added to the project by an imported element (.NET assemby classes, structured Web service types, etc.).. In this case, use the WL prefix to force the WLanguage type when declaring a variable.
Example:
// Force l'utilisation du type XMLDocument du WLangage
// si le type XMLDocument existe par ailleurs
MaVariable is WL.XMLDocument
Minimum version required
  • Version 14
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 05/24/2024

Send a report | Local help