PC SOFT

ONLINE HELP
FOR WINDEV, WEBDEV AND WINDEV MOBILE

Home | Sign in | English US
  • Overview
  • How to declare a variable?
  • Declaring a variable
  • Declaring a variable and inializing it
  • Declaring several variables
  • Detailed syntaxes
  • Examples
  • Associating a critical section with a variable
  • How to access the properties of a variable?
  • Syntaxes
  • Example
  • Rule for variable scope
  • Rule
  • Exception
  • Special case: Reports & Queries
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadApple WatchUniversal Windows 10 AppWindows Mobile
Others
Stored procedures
Overview
A variable is defined by its name and by its 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:
Versions 18 and later
To simplify the declaration of variables, you also have the ability to use the type inference.
New in version 18
To simplify the declaration of variables, you also have the ability to use the type inference.
To simplify the declaration of variables, you also have the ability to use the type inference.
How to declare a variable?

Declaring a variable

<Variable Name> is <Type>

Declaring a variable and inializing it

<Variable Name> is <Type> = <Initial Value>

Declaring several variables

<Name of Variable 1>, <Name of Variable 2> are <Type>

Detailed syntaxes

<Variable Name>:Name of variable to declare.
<Type>:Type of 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.
Notes:
  • 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.
    Versions 19 and later
    To declare and initialize several variables at the same time, use the multiple initialization.
    New in version 19
    To declare and initialize several variables at the same time, use the multiple initialization.
    To declare and initialize several variables at the same time, use the multiple initialization.
  • Several variables with the same name cannot be used in the same process (regardless of the type of these variables).
  • Versions 15 and later
    The "useful" attribute can be used when declaring a variable to specify that the variable 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:
    <Variable Name> is <Type>, useful[ = "reason"]

    <Variable Name> is <Type> <useful [ = "reason"]>
    "Reason" is a note for the developer.
    Example:
    n1 is int, useful = "Variable required by the Webservice"

    n2 is int <useful = "Variable required by the Webservice">
    New in version 15
    The "useful" attribute can be used when declaring a variable to specify that the variable 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:
    <Variable Name> is <Type>, useful[ = "reason"]

    <Variable Name> is <Type> <useful [ = "reason"]>
    "Reason" is a note for the developer.
    Example:
    n1 is int, useful = "Variable required by the Webservice"

    n2 is int <useful = "Variable required by the Webservice">
    The "useful" attribute can be used when declaring a variable to specify that the variable 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:
    <Variable Name> is <Type>, useful[ = "reason"]

    <Variable Name> is <Type> <useful [ = "reason"]>
    "Reason" is a note for the developer.
    Example:
    n1 is int, useful = "Variable required by the Webservice"

    n2 is int <useful = "Variable required by the Webservice">

Examples

  • Declaring simple variables:
    CustomerName is string
    Counter is int
    Price is real
    i,j,k are int

    Versions 18 and later
    The type inference allows you to use the following syntaxes:
    let Amount = 1500.69 // real type
    let City = "Montpellier" // string type
    New in version 18
    The type inference allows you to use the following syntaxes:
    let Amount = 1500.69 // real type
    let City = "Montpellier" // string type
    The type inference allows you to use the following syntaxes:
    let Amount = 1500.69 // real type
    let City = "Montpellier" // string type
  • Declaring a Border variable:
    MyBorder is Border
Versions 19 and later
Declaring several variables and inializing them
<Name of Name> 1>, <Name of Name> 2>, ..., <Name of Name> N> are <Type>
= (<Initial Value 1>, <Initial Value 2>, ..., <Initial Value M>)
Details of syntax
<Variable Name>:Name of 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.
Notes:
  • 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 code line. See Multiple assignment for more details.
Examples:
x, y, z are int = (1, 10, 4)
// x is set to 1, y to 10 and z to 4

s, t, p are strings = ("A", "B001")
// s is set to "A", t to "B001" and p to ""
New in version 19
Declaring several variables and inializing them
<Name of Name> 1>, <Name of Name> 2>, ..., <Name of Name> N> are <Type>
= (<Initial Value 1>, <Initial Value 2>, ..., <Initial Value M>)
Details of syntax
<Variable Name>:Name of 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.
Notes:
  • 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 code line. See Multiple assignment for more details.
Examples:
x, y, z are int = (1, 10, 4)
// x is set to 1, y to 10 and z to 4

s, t, p are strings = ("A", "B001")
// s is set to "A", t to "B001" and p to ""
Declaring several variables and inializing them
<Name of Name> 1>, <Name of Name> 2>, ..., <Name of Name> N> are <Type>
= (<Initial Value 1>, <Initial Value 2>, ..., <Initial Value M>)
Details of syntax
<Variable Name>:Name of 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.
Notes:
  • 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 code line. See Multiple assignment for more details.
Examples:
x, y, z are int = (1, 10, 4)
// x is set to 1, y to 10 and z to 4

s, t, p are strings = ("A", "B001")
// s is set to "A", t to "B001" and p to ""
Versions 21 and later
AndroidAndroid Widget Not available in Android and in Android Widget

Associating a critical section with a variable

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.
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
...
CriticalSectionStart(gcySum)
gcySum += cyAmountOrder
CriticalSectionEnd(gcySum)

CriticalSectionStart(gcyMax)
IF cyAmountOrder > gcyMax THEN
gcyMax = cyAmountOrder
END
CriticalSectionEnd(gcyMax)
Notes:
  • 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 in native mode: the critical section attribute is not required.
New in version 21
AndroidAndroid Widget Not available in Android and in Android Widget

Associating a critical section with a variable

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.
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
...
CriticalSectionStart(gcySum)
gcySum += cyAmountOrder
CriticalSectionEnd(gcySum)

CriticalSectionStart(gcyMax)
IF cyAmountOrder > gcyMax THEN
gcyMax = cyAmountOrder
END
CriticalSectionEnd(gcyMax)
Notes:
  • 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 in native mode: the critical section attribute is not required.
AndroidAndroid Widget Not available in Android and in Android Widget

Associating a critical section with a variable

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.
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
...
CriticalSectionStart(gcySum)
gcySum += cyAmountOrder
CriticalSectionEnd(gcySum)

CriticalSectionStart(gcyMax)
IF cyAmountOrder > gcyMax THEN
gcyMax = cyAmountOrder
END
CriticalSectionEnd(gcyMax)
Notes:
  • 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 in native mode: 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:
    <Variable Name>..<Property> = <Value>
  • Reading a property of the variable:
    <Variable Name>..<Property>

Example

Using properties on a Border type:
// Define the border characteristics
MyBorder is Border
MyBorder..Color = LightRed
MyBorder..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 processes of the window and window controls as well as in the "local" procedures of the window.
    • 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 the processes of the window and window controls (including the "local" procedures of the window).
    • none of these two variables can be used in the rest of the project.
Note: The variables are specific to the executable, the Webservice or the WEBDEV session where they have been declared.
Versions 18 and later
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 localized 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 the 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. Display the description window of project: on the "Project" pane, in the "Project" group, click "Description".
  2. In the "Compilation" tab, check or uncheck "Scope of local variables limited to the current block".
New in version 18
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 localized 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 the 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. Display the description window of project: on the "Project" pane, in the "Project" group, click "Description".
  2. In the "Compilation" tab, check or uncheck "Scope of local variables limited to the current block".
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 localized 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 the 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. Display the description window of project: on the "Project" pane, in the "Project" group, click "Description".
  2. In 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" is checked in the data model editor can be used in "Reports and Queries". If this option is not checked, the item can only be used by indirection (the auto-complete feature is not available to the user, the item is not available in the wizards, the description windows, ...).
Minimum required version
  • Version 14
This page is also available for…
Comments
Click [Add] to post a comment