PC SOFT

ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Home | Sign in | English EN
New WINDEV 26 feature!
  • In this lesson you will learn the following concepts
  • Practical example
  • What is a variable?
  • Declaring a variable
  • Assignment and use
  • Types of variables
  • Scope of variables
  • Global scope
  • Local scope
  • Summary scope diagram
  • Simple operations on variables
  • Tips
  • Details of variable type: String variables
  • The String type
  • Building a string
  • Operations on strings
  • Finding a string
  • Comparing two strings
Lesson 2.2. Variables
In this lesson you will learn the following concepts
  • What is a variable?
  • The different types of variables.
  • Scope of variables.
  • The String type in detail.
Lesson duration

Estimated time: 1 h
Previous LessonTable of contentsNext Lesson
Practical example
To follow the steps in this lesson, we will use the "WLanguage" project that we created in the previous lesson.

  • We will write the code directly in the project initialization code. As this is the very first code executed in the project test, we can do tests very simply, without any interface. To view the result of our actions, we will use the Trace function, which allows us to write the desired information in an output window as well as in the debugger pane (we will use the second option).
  • To view WLanguage events of the project:
    1. Right-click the "P" button next to the open element tabs. The context menu appears.
    2. Select "Element code".
    3. The code editor displays the different events associated with the project.
What is a variable?
In a programming language, a variable is used to store data. These memory sections contain strings, numbers, etc.
Variables are used to perform calculations, comparisons, or to store information that will be used later.
Declaring and initializing a variable
A variable is represented by:
  • a name: Name given to the variable so that it can be manipulated in the code.
    Remark: The code style is applied by default. A prefix is automatically added to identify the type of variable.
  • 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

Before using a variable, it must first be declared (i.e. created).
  • Example of simple declaration:
    cyPrice is currency
    • cyPrice represents the variable name. The cy prefix identifies Currency variables. The code style adds the prefix automatically, if it has not been specified.
    • is is used to declare the variable. WLanguage uses common, readable language.
    • currency corresponds to the variable type.
  • Example of multiple declaration:
    sLastName, sFirstName are strings
    • sLastName, sFirstName represent the variable names. The s prefix identifies String variables.
    • are is used to declare a set of variables.
    • strings represents the type of variables.

Note

Depending on where the variable is declared, this variable can be global to the element (we will see this concept in "Scope of variables" later in this lesson). In this case, the prefix 'g' (global) is added to the variable name. For example, the declaration of the sLastName variable becomes:
gsLastName is string
In the rest of the tutorial, all declared variables will be prefixed by 'g', because they are declared in the "Initializing" event of the project.
We will see the different types of variables (local and global) in the following paragraphs.


  • We will write these statements in the code editor. In the "Initializing" event of the "WLanguage" project, write the following code:
    gcyPrice is currency
    gsLastName, gsFirstName are strings

    You will notice the specific coloring of the different elements in the declaration: a specific color is used for the variable name, and another one for its type. These colors make it possible to quickly identify the different elements of your code.

Assignment and use

When you declare a variable, you can assign it a value. The = operator allows you to assign values.

  • We will test this operator. In the "Initializing" event of the "WLanguage" project, add the following code:
    // Assign a value to a currency variable
    gcyPrice = 1256.67
    // Display the contents of the variable
    Trace(gcyPrice)
    // Assign a value to a string variable
    gsLastName = "Doe"
    // Display the contents of the variable
    Trace(gsLastName)

    In this code, a value is assigned to the gcyPrice and gsLastName variables. To read and manipulate the contents of the variable, simply use its name. In this example, the contents of the variables are displayed in the "Debugger trace" pane via the Trace function.
  • Let's test our code:
    1. Test the project by clicking Test project in the quick access buttons.
    2. The project is run and the code editor reappears.
    3. If necessary, open the "Debugger trace" pane to see the result: on the "Home" pane, in the "Environment" group, expand "Panes" and select "Debugger trace".
    4. The "Debugger trace" pane contains the contents of our variables:
      1256.67
      Doe
Types of variables
The variable type specifies 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. You will optimize memory and avoid calculation or process errors when using variables in WLanguage functions.

Most of these types of variables will be used in this tutorial. We will also see types such as Array, date, time, structure, and more, ...
Advanced variables are also available. These advanced types group all characteristics of the element currently used in a single variable. These advanced types can be used to manipulate XML documents, emails, XLS files and more, ... This type of variable will also be used later in this tutorial.
For more details, see the help about the desired type (see The different types of variables).
Scope of variables
Variables can be declared anywhere in the code. However, depending on where they are declared, variables cannot be used to perform processes or calculations. This is referred to as scope of variables.
Two types of scope are available:
  • Global.
  • Local.

Global scope

Global means that the variable is visible from anywhere 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,
  • Control.
A variable declared at the project level has the greatest visibility in the program. The variable is visible everywhere, in all the events and processes of the program. However, it is not recommended to declare too many variables with this scope: the memory used 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 an event or a process, it is recommended to use parameters (for more details, see "Parameters and result of the procedure").
A variable declared at the set of procedures and project level have the same visibility. The advantage of declaring a variable at the 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 the Window, Mobile Window, Page or Report level limits the scope of the variable to all the events or processes of that element and its controls. This makes it possible to encapsulate and limit their use.
A global variable declared at the control level limits the scope of the variable to all events of that control. This makes it possible to group the variables by control and to make the "Initializing" event of the window more readable.

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 the variable to the process or event.

Summary scope diagram

Scope of variables
By default, variables are global when they are declared:
  • in the "Initializing" event of the project (or in the "Declaration" event of the set of procedures). The variable is global to the project.
  • in the "Global declarations" event of the 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 or event where it is declared.
Caution: Declare a variable using the "GLOBAL" keyword in the "Initializing" event of a control to make the variable global to that control.
Simple operations on 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 by 1 (add 1 to the variable).
  • - - to decrement by 1 (subtract 1 from the variable).
  • += to assign values to a variable by adding a value.
  • - = to assign values to a variable by subtracting a value.
  • To get an overview of the operations that can be performed, we will do a small test:
    1. Delete the code in the "Initializing" event of the project (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      // Declaration of variables
      gnCpt is int
      gnV1 is int
      gxRes is numeric
       
      // Assignment
      gnCpt = 10
      gnV1 = 3
       
      // Use of operators
      gnCpt = gnCpt + 3   // gnCpt is set to 13
      Trace(gnCpt)
      gnCpt ++ // gnCpt is set to 14
      Trace(gnCpt)
      gnCpt -= 8 // gnCpt is set to 6
      Trace(gnCpt)
      gnCpt = gnCpt * gnV1 // gnCpt is set to 18
      Trace(gnCpt)
      gxRes = gnCpt / 5 // gxRes is set to 3.6
      Trace(gxRes)
      This code allows you to perform different operations and displays their result in the trace window.
  • Let's test our code:
    1. Test the project by clicking Test project in the quick access buttons.
    2. If necessary, open the "Debugger trace" pane to see the result: on the "Home" pane, in the "Environment" group, expand "Panes" and select "Debugger trace".
    3. The "Debugger trace" pane contains the value of the gnCpt variable for each operation:
      13
      14
      6
      18
      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. For more details on operators, see List of operators.

Tips

  • It is recommended to use long names for variables (and avoid names such as i, j, k, etc.). When reading the program again, you will be able to easily remember the variable purpose.
  • For variable names, WLanguage accepts all Unicode characters (including accented characters). Meaning improved readability! Caution: some characters are not allowed: spaces, equals sign, dots, commas, etc.
  • 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 it must be used for calculations.
    • use a string variable if it must be used to store digits without performing calculations (to store the social security number for example).
Details of variable type: String variables
String variables are one of the most commonly used variable types.
Let's take a look at some of the 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:
gsLastName is string
// Assign a value to a string variable
gsLastName = "Doe"

Note

In WLanguage, the " character (double quote) is the character used to delimit a character string. In the above example, double quotes are used to assign the Doe value to the sLastName variable.
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:
gsMyString is string
gsMyString = [
Example of
multi-line string
]
You can also assign the content of a control handling strings to a string variable. The following code is used to assign the content of an Edit control a string variable:
gsLastName is string
// Assign a value to a string variable
// the content of EDT_LastName edit control
gsLastName = EDT_LastName

Building a string

As we have seen previously, it is very easy to declare and assign a value to a string. For example:
gsBuiltString is string
gsProductName is string = "WINDEV"
However, in many cases a string can be composed of variable elements, e.g., the name of a product. In this case, there are several methods for building strings. We will test these methods, and see their advantages and disadvantages.

  • Let's go back to our "WLanguage" example:
    1. Delete the code in the "Initializing" event of the project (press Ctrl + A to select all and then "Delete").
    2. In the "Initializing" event of the "WLanguage" project, write the following code:
      gsBuiltString is string
      gsProductName is string = "WINDEV"
       
      // Concatenation
      gsBuiltString = gsProductName + " is my go-to development tool!"
      Trace("Concatenation: " + gsBuiltString)
       
      // StringBuild
      gsBuiltString = StringBuild("%1 is my go-to development tool!", gsProductName)
      Trace("StringBuild: " + gsBuiltString)
       
      // Direct input of the variable ([% %] syntax)
      gsBuiltString = "[%gsProductName%] is my go-to development tool!"
      Trace("Direct input: " + gsBuiltString)

      In this code, the gsBuiltString variable is built using 3 different methods:
      • 1st method: simple concatenation using the '+' operator . Strings are simply joined together. This is the most common method. However, this method has serious limitations if the message is to be displayed to the user in a multilingual application. Depending on the syntax of the language used, the variable may have to be at a different position, which will not be possible in this case.
      • 2nd method: Using StringBuild. This function is used to write an entire message. The variable part is identified by '%1', '%2'... The value of these identifiers corresponds to the parameters that follow the function. This method is much simpler for a translation, but remains difficult to read (the content of the %x are function parameters).
      • 3rd method: Directly using the variable. This method combines all the advantages: simplicity for translation and readability. Simply write the name of the desired variable between the [% and %] tags.
      For each method used to build the string, Trace displays the result in the "Debugger trace" pane.

  • Let's test our code:
    1. Test the project by clicking Test project in the quick access buttons. The project is run and the code editor reappears.
    2. If necessary, open the "Debugger trace" pane to see the result: on the "Home" pane, in the "Environment" group, expand "Panes" and select "Debugger trace".
    3. The "Debugger trace" pane contains our concatenation examples:
      Concatenation: WINDEV is my go-to development tool!
      StringBuild: WINDEV is my go-to development tool!
      Direct input: WINDEV is my go-to development tool!

Operations on strings

WLanguage offers many operators and functions to extract and manipulate strings.

  • To get an idea of the power and simplicity of WLanguage for manipulating strings, let's do a quick test to iterate through a string and extract its substrings.
    1. Delete the code in the "Initializing" event of the project (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      gsProductList is string = [
      WINDEV
      WEBDEV
      WINDEV Mobile
      ]
       
      FOR EACH STRING sAProduct OF gsProductList SEPARATED BY CR
      Trace(sAProduct)
      END
      gsProduct is string = gsProductList.ExtractString(2, CR)
      Trace(gsProduct)
    3. This code allows both to iterate through the multiline string and to extract an element from it. Let's take a look at this code:
      • gsProductList is a multiline string.
      • The "FOR EACH STRING" statement is used to iterate through the different elements of the string. These elements are separated by an CR (Carriage Return).
      • In our case, <String>.ExtractString extracts the second element of the string ("WEBDEV").
    4. Let's test this code : click Test project in the quick access buttons.
    5. The "Debugger trace" pane contains the following elements:
      WINDEV
      WEBDEV
      WINDEV Mobile
      WEBDEV
  • You can also extract parts of a string by using the [[ and ]] operators. These operators are used to extract the desired part of the text, by specifying the position of the start and/or end character.
    1. Delete the code in the "Initializing" event of the project (press Ctrl + A to select all and then "Delete").
    2. Write the following code:
      gsText is string = "San Francisco"
      // Display the fifth character in the trace: "F"
      Trace(gsText[[5]])
      // Display characters 5 to 10 in the trace: "Franci"
      Trace(gsText[[5 TO 10]])
      // Display characters from the fifth position in the trace: "Francisco"
      Trace(gsText[[5 TO]])
      // Display first 10 characters in the trace: "San Franci"
      Trace(gsText[[TO 10]])
      // Display characters from the 10th position in the trace: "isco"
      Trace(gsText[[10 ON 4]])
    3. Let's test this code : click Test project in the quick access buttons.
    4. The "Debugger trace" pane contains the following information:
      F
      Franci
      Francisco
      San Franci
      isco
  • WLanguage also makes it possible to format strings with the <String>.Format function. This allows to specify that a string should be displayed without accents, in uppercase or lower case, ... Here's a quick example:
    1. Delete the code in the "Initializing" event of the project (press Ctrl + A to select all and then "Delete").
    2. Write the following code:
      gsInitialText is string = "WINDEV is my go-to development tool!"
       
      // Remove accents from string and set to lowercase
      gsInitialText = gsInitialText.Format(ccIgnoreAccent + ccLowCase)
      Trace(gsInitialText)
      // Remove spaces from string and set to uppercase
      gsInitialText = gsInitialText.Format(ccUpCase + ccIgnoreInsideSpace)
      Trace(gsInitialText)
    3. Let's test this code : click Test project in the quick access buttons.
    4. The "Debugger trace" pane contains the following information:
      windev is my go-to development tool!
      WINDEVISMYGO-TODEVELOPMENTTOOL!
For more details on how to manipulate strings, see Handling character strings and Character string management functions.

Finding a string

Searching is one of the most common operations performed on strings. At some point, everyone has tried to know if a word is in a string, what its position is, etc, ... This is easier than ever in WLanguage.

  • We will search for a string within another string:
    1. Delete the code in the "Initializing" event of the project (press Ctrl + A to select all and then "Delete").
    2. Write the following WLanguage code:
      gsSearchString is string = "WINDEV"
       
      gsOriginalText is string = [
      Development tools:
      - WINDEV
      - WEBDEV
      - WINDEV Mobile
      ]
       
      gnPos is int
      gnPos = Position(gsOriginalText, gsSearchString)
      IF gnPos = 0 THEN
      Trace("[%gsSearchString%] was not found in the text")
      ELSE
      Trace("[%gsSearchString%] was found at position [%gnPos%]")
      END

      Let's take a look at this WLanguage code:
      • Several variables are declared:
        • a String variable corresponding to the string to be searched for.
        • a String variable corresponding to the text in which the search will be performed.
        • an Integer variable corresponding to the position of the search string.
      • Position is used to search for a string within another string. In our example, we search for the contents of gsSearchString in the contents of gsOriginalText.
      • If Position returns 0, it means that the sought string was not found.
      • A message is displayed by Trace, indicating if the string was found, as well as its position.
    3. Let's test this code: click Test project in the quick access buttons.
    4. The "Debugger trace" pane contains the following information:
      WINDEV was found at position 23

Comparing two strings

Comparisons are another common operation performed on strings: find out if two strings are equal.
  • We will compare two strings:
    1. Delete the code in the "Initializing" event of the project (press Ctrl + A to select all and then "Delete").
    2. Write the following WLanguage code:
      // Strings to compare
      gsString1 is string = "WINDE"
      gsString2 is string = "WINDEV"
       
      // The 2 strings are compared with the ~= (flexible equality) operator
      IF gsString1 ~= gsString2 THEN
      Trace("The 2 strings are equal")
      ELSE
      Trace("The 2 strings are different")
      END

      Let's take a look at this WLanguage code:
      • Two String variables are declared. They contain the strings to be compared.
      • The comparison is performed using the ~= (flexible equality) operator. This operator is used to perform comparisons ignoring the case, accented characters as well as leading and trailing spaces.
      • The result of the comparison is displayed.
    3. Let's test this code : click Test project in the quick access buttons.
    4. The "Debugger trace" pane contains the following information:
      The 2 strings are different
    5. Go back to the code and modify the declaration of gsString1:
      gsString1 is string = "WINDEV"
    6. Let's test this code : click Test project in the quick access buttons.
    7. The "Debugger trace" pane contains the following information:
      The 2 strings are equal
      Even if the strings contain characters in different case, they are equal.
Remark: There are different comparison operators.
Previous LessonTable of contentsNext Lesson
Minimum version required
  • Version 26
Comments
Click [Add] to post a comment