PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
New WEBDEV 24 feature!
  • This lesson will teach you the following concepts
  • What is a variable?
  • Declaring a variable
  • Assignment and use
  • The types of variables
  • The scope of variables
  • Global scope
  • Local scope
  • Summary scope diagram
  • Simple operations on the variables
  • Tips
  • Details of variable type: the String variables
  • The String type
  • Practical example
  • Details of another variable type: the arrays
  • Declaration
  • Filling an array and accessing the elements
  • Advanced arrays
Appendix 2. The variables
This lesson will teach you the following concepts
  • What is a variable?
  • The different types of variables.
  • The scope of variables.
  • The String type in details.
  • The Array type in details.

Estimated time: 30 mn
Previous LessonTable of contentsNext Lesson
What is a variable?
In a programming language, a variable is used to store data. These memory sections contain strings, numbers, etc.
The variables are used to perform calculations, to perform comparisons or to store information that will be used later.
A variable is represented by:
  • a name: Name given to the variable so that it can be used by the language.
  • a type: Nature of data stored in the variable (see Types of variables).
  • a value: Information stored in the variable.
  • a scope: Limit for using the variable in the program (see Scope of variables). The scope is mainly defined by the location where the variable is declared.

Declaring a variable

The variable must be declared (which means created) before it can be used.
  • Example of simple declaration:
    Price is currency
    • Price represents the variable name.
    • is is used to declare the variable. The everyday language is used in WLanguage.
    • currency corresponds to the variable type.
  • Example of multiple declaration:
    LastName, FirstName are strings
    • LastName, FirstName represent the names of variables.
    • are is used to declare a set of variables.
    • strings represents the type of variables.

Assignment and use

When the variable is declared, you have the ability to assign it (or to give it a value).
For example:
// Assign a currency variable
Price = 1256.67
// Assign a string variable
LastName = "Doe"
The = operator is used to perform this assignment.

Note

In WLanguage, the " character (double quote) is the character used to delimit a character string. In the above example, the doubles quotes are used to assign the Doe value to the LastName variable.
The variable content can be read and handled: all you have to do is use the name given to the variable to access it.
The following example is used to read and display the content of Price variable on the screen:
Info(Price)
The types of variables
The variable type is used to specify the kind of information that will be stored in the variable. The most common types are:
  • boolean (True or False),
  • string ("Doe"),
  • integer (1234),
  • currency (12,32),
  • real (7,766666),
  • etc.

Important

Use the type corresponding to the information that must be stored. Therefore, you will optimize the memory and you will avoid calculation or process errors when using variables in the WLanguage functions.
See the help about the requested type for more details (see The different types of variables).

Note

Other types are available, such as arrays, structures, dates, times, ...
Advanced variables are also available. These advanced types group all characteristics of the element currently used in a single variable.
Advanced types can be used to handle XML documents, emails, XLS files, ... This type of variable will be used later in this tutorial.
The scope of variables
The variables can be declared anywhere in the code. However, according to the position of its declaration, the variable cannot be used to perform processes or calculations. We talk of variable scope.
Two types of scope are available:
  • Global.
  • Local.

Global scope

Global means that the variable has an extended visibility in the code. The variable is visible outside the location where it was declared. Several levels are available:
  • Project and Set of procedures,
  • Window, Mobile Window, Page, Report.
A variable declared at project level has the greatest visibility in the program. The variable is visible anywhere, in all program processes. However, you should not declare too many variables with this scope ; indeed, the memory occupied by the variable is always reserved even if the variable is not used. Using a large number of global variables is not recommended in the program architecture. To pass variables to a process, we recommend that you use parameters (see "Parameters and result of a procedure") for more details.
A variable declared at Set of Procedures level and a variable declared at project level have the same visibility. The advantage of declaring a variable at Set level is to group (or classify) the variables by theme in order to make the "Initializing" project event more readable.
A variable declared at Window, Mobile Window, Page or Report level limits the variable scope to all the events of the element (Window, Mobile Window, Page or Report) and its controls. This makes it possible to encapsulate and limit the uses.

Local scope

Local means that the variable has a limited visibility in the code. The variable is visible in the process where it was declared. This makes it possible to restrict the use of variable to the process.

Summary scope diagram

A variable is global when it is declared:
  • in the initialization code of project (or in the declaration code of set of procedures). The variable is global to the project.
  • In the declaration code of global variables of window, page or report. The variable is global to the element (window, page or report) where it was declared.
In all other cases, a variable is local to the process where it is declared.
Simple operations on the variables
Several mathematical operators can be used to perform calculations on variables:
  • + to perform an addition.
  • - to perform a subtraction.
  • * to perform a multiplication.
  • / to perform a division.
Other operators can be used to perform calculations:
  • ++ to increment from 1 (add 1 to the variable).
  • - - to decrement from 1 (subtract 1 from the variable).
  • += to assign by adding a value.
  • += to assign by subtracting a value.
Examples:
// Declaration of variables
Cnt is int
V1 is int
Res is numeric

// Assignment
Cnt = 10
V1 = 3

// Use of operators
Cnt = Cnt + 3   // Cnt is equal to 13
Cnt ++ // Cnt is equal to 14
Cnt -= 8 // Cnt is equal to 6
Cnt = Cnt * V1 // Cnt is equal to 18
Res = Cnt / 5 // Res is equal to 3.6
Comparison operators are also available:
  • < less than,
  • > greater than,
  • <= less than or equal to,
  • >= greater than or equal to,
  • <> different from,
  • = equal to.
Other operators are available. See List of operators for more details.

Tips

  • It is very convenient to name the variables with long names (and to avoid short names such as i, j, k, ...). When reading the program again, you will be able to easily remember the variable purpose.
  • To define the name of variables, all Unicode characters (including the accented characters) are accepted. Meaning improved readability! Caution: some characters are not allowed: space, =, dot, comma, ...
  • It is very important to give the proper type to the variable according to its use. For example, to store several digits, you may have to:
    • use a numeric variable if this variable must be used for calculations.
    • use a string variable if this variable must be used to store digits without performing calculations (to store the social security number for example).
Details of variable type: the String variables
The String variables are the most often used types of variables.
Let's present in details some features available for this type of variable.

The String type

The String type is used to store and handle characters and character strings.
We have already seen how to initialize a string variable:
LastName is string
// Assign a string variable
LastName = "Doe"

There is no need to declare the string length: this length automatically adapts when using the variable.

Tip

To initialize a string variable with a text on several lines, use the following syntax:
<Variable name> = [
               <Text of line 1>
               <Text of line 2>
               ]
For example:
MyString is string
MyString = [
Example of
multi-line string
]
You also have the ability to assign a string variable with the content of a control handling strings. The following code is used to assign a string variable with the content of an edit control:
LastName is string
// Assign a string variable
// with the content of EDT_LastName edit control
LastName = EDT_LastName

In addition to the main comparison operators, several powerful operators are used to manage the extractions and concatenations in advanced mode.
Some examples:
  • " + ": To concatenate strings
  • " ~= ": To check the flexible equality
Specific WLanguage functions are used to perform various operations: search, extraction, size, switch to uppercase characters, ...
Examples:
str is string
str = "WEBDEV is a great tool"
// Extract a sub-string from left
Info(Left(str,6))  // Displays "WEBDEV"
// Extract a sub-string from right
Info(Right(str,4))  // Displays "tool"

Note

The different WLanguage functions can be nested. A WLanguage function can be used as parameter of another WLanguage function.
For example:
Info(Middle(Left(str,13),8,2)) // Displays "is"
See the help about the character strings and about the functions for handling character strings for more details.

Practical example

To handle the different concepts of this lesson, we are going to create different windows. These windows will be created in the "My_First_Pages" project.
  • Open the "My_First_Pages" project if necessary.
    1. Display the WEBDEV home page (Ctrl + <).
    2. Click "Tutorial" and select the project named "My first pages (Exercise)".
To handle the Character String variables, we are going to create the following page:
Test page
This page is used to:
  • find a string inside another one.
  • compare two strings.
  • Create a new blank page:
    1. Click Creation among the quick access buttons. The window for creating a new element is displayed: click "Page" then "Page". The wizard for page creation is displayed.
    2. Select "Blank".
    3. Validate. The page is automatically created in the editor. The window for saving an element is displayed.
    4. Specify the page title: "Variables". The page name ("PAGE_Variables") is automatically proposed.
    5. Click on the green button to validate the information displayed in the save window.
  • To create the edit control containing the string:
    1. On the "Creation" pane, in the "Usual controls" group, click .
    2. Click the location where the edit control will be created (at the top, in the middle of the page for example).
    3. Right-click the control and select "Description".
    4. In the "General" tab, specify:
      • The control name: "EDT_Text".
      • The control caption: "Text".
    5. Select the "Content" tab. This tab is used to define the default value of edit control. Type "WEBDEV is a great tool".
    6. Validate the description window. The text typed is directly displayed in the control.
Finding a string
  • To create the "Find a string" button:
    1. On the "Creation" pane, in the "Usual controls" group, click Button control.
    2. Click the location where the button will be created (below the edit control for example).
    3. Click the button that was just created. The text displayed in the button becomes editable. Type the caption: "Find a string".
    4. Press Enter to validate the input.
    5. Display the events associated with the button (F2).
    6. Write the following code in the server "Click" event of the button:
      sSoughtString is string = "WEBDEV"
      nPos is int
      nPos = Position(EDT_Text, sSoughtString)
      IF nPos = 0 THEN
      Info(sSoughtString + " not found in the text")
      ELSE
      Info(sSoughtString + " found at position " + nPos)
      END
      Let's study this code:
      • Two variables are declared: a String variable corresponding to the sought string and an Integer variable corresponding to the position of sought string.
      • Position is used to find a string inside another one. In our example, we search for the content of sSoughtString in the content of EDT_Text control.
      • The IF statement is used to check the result of Position. This type of statement will be presented later in this section on WLanguage.
      • If Position returns 0, it means that the sought string was not found. A message is displayed by Info.
  • Let's now run the page test:
    1. Click Page Go among the quick access buttons (or press F9).
    2. The window that was just modified is automatically saved then it is started in execution. Click on the button. The WEBDEV word is found.
      Word found
    3. Modify the content of edit control (by replacing WEBDEV by WB for example) and click the button. The WEBDEV word is not found.
    4. Close the browser.
Comparing two strings
  • To create the "Compare two strings" button:
    1. On the "Creation" pane, in the "Usual controls" group, click Button control.
    2. Click the location where the button will be created (on the right of existing button for example).
    3. Click the button that was just created. The text displayed in the button becomes editable. Type the caption: "Compare two strings".
    4. Press Enter to validate the input.
    5. Adapt the control size.
    6. Display the events associated with the button (F2).
    7. Write the following code in the event "Click on (Server)":
      sStringToCompare is string = "WEBDEV"

      IF EDT_Text ~= sStringToCompare THEN
      Info("The displayed text corresponds to " + sStringToCompare)
      ELSE
      Info("The displayed text does not correspond to " + sStringToCompare)
      END
      Let's study this code:
      • A String variable is declared. This variable contains the string to compare.
      • The ~= operator corresponds to a flexible equality. This operator is used to perform a comparison while ignoring the case, the spaces found before and after the string and the accented characters.
        Remark: to type the "~" sign, press AltGr + 2.
      • If the comparison is true, the edit control corresponds to the word found in the string to compare regardless of the case used.
  • Let's now run the page test:
    1. Click Page Go among the quick access buttons (or press F9).
    2. Click the "Compare two strings" button. The message indicates that the comparison is not correct.
      Word not found
    3. In the edit control, type "WEBDEV" in uppercase characters and click the "Compare two strings" button. The WEBDEV word is found.
    4. Modify the content of edit control (by replacing "WEBDEV" by "WEBDEV" for example) and click the button. The WEBDEV word is also found.
    5. Close the browser.
Details of another variable type: the arrays
The arrays are a common type of variable.
An array is a structured type that is used to group a set of elements of the same type. Each array element can be directly accessed by its subscript.
Specific WLanguage functions are used to handle the arrays and their elements. These functions start with "Array".

Declaration

The declaration of an Array variable is performed as follows:
<Array name> is array of <Type of array elements>
For example:
arrString is array of strings
arrInt is array of int

Filling an array and accessing the elements

During its declaration, the array is empty. The Add function is used to add elements via the following syntax:
Add(<Array name>, <Element value>)
To access the array elements, use the following syntax:
<Array name> [<Element subscript>]

Important

The subscripts of array elements start from 1.
Example:
// Create an array of strings
MyArray is array of strings
// Add elements
Add(MyArray, "WINDEV")
Add(MyArray, "WEBDEV")
Add(MyArray, "WINDEV Mobile")
// Display the content of third element
Trace(MyArray[3])  // Displays "WINDEV Mobile"

tip

Fast array initialization
To initialize an array, you also have the ability to use the following syntax:
// Declares an array
arrDay is array of strings
// Initialization with
// the names of the days of week
arrDay = ["Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"]
See Array for more details.

Advanced arrays

Advanced arrays are also available: array with several dimensions, array of arrays, associative array, array of structures, array of objects, ...
For more details, see Array, Associative array, etc.
Previous LessonTable of contentsNext Lesson
Minimum version required
  • Version 24
Comments
Click [Add] to post a comment