ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

New WINDEV Mobile 28 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 3.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.
  • 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.

Note

In the first lessons, we saw that we can use a code style.
This code style is automatically applied to the elements of your project: controls, windows, pages, reports, etc.
When creating the project, in the "Code style" step, you can check the "Automatically add prefixes to new variables" option. If this option is checked, a prefix will be automatically added to identify the type of variable.
Thus, the following declaration:
Price is currency
will become:
cyPrice is currency
where "cy" is the prefix of the "Currency" variables.

This option can be changed in the "Options" tab of the project description window, "Add prefixes automatically" option.

Declaring a variable

Before using a variable, it must first be declared (i.e. created).
  • Example of simple declaration:
    Price is currency
    • Price represents the variable name.
    • is is used to declare the variable. WLanguage uses common, readable language.
    • 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 variable.

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).

If you enable the option to automatically prefix variables, the 'g' letter (for global) will be added before the variable name. For example, the declaration of the LastName variable becomes:
gsLastName is string
We will not use automatic prefixes in the rest of this tutorial.

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 initialization event of the "WLanguage" project, write the following code:
    Price is currency
    LastName, FirstName 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 initialization event of the "WLanguage" project, add the following code:
    // Assign a value to a currency variable
    Price = 1256.67
    // Display the contents of the variable
    Trace(Price)
    // Assign a value to a string variable
    LastName = "Doe"
    // Display the contents of the variable
    Trace(LastName)

    In this code, a value is assigned to the Price and LastName 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 in simulator mode:
      • Expand in the quick access buttons.
      • Click Test project to test the project in simulator mode.
      • Validate the warning window if necessary.
    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" tab, in the "Environment" group, expand "Panes", select "Panes", and then 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, etc.
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 event (or process) 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. In this case, the variable is declared in the project initialization event. 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 to make the project initialization event easier to read.
A variable declared at the Window, Mobile Window, Page or Report level limits the scope of the variable to 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 the events of that control. This allows you to group the variables by control and to make the window initialization event easier to read.

Note

By default, global variables are colored in blue in the code editor. To identify global variables and their scope, it is recommended to follow a standard.

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 initialization 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 initialization 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 project initialization event (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      // Declaration of variables
      Counter is int
      V1 is int
      Res is numeric
       
      // Assignment
      Counter = 10
      V1 = 3
       
      // Use of operators
      Counter = Counter + 3   // Counter is set to 13
      Trace(Counter)
      Counter ++ // Counter is set to 14
      Trace(Counter)
      Counter -= 8 // Counter is set to 6
      Trace(Counter)
      Counter = Counter * V1 // Counter is set to 18
      Trace(Counter)
      Res = Counter / 5 // Res is set to 3.6
      Trace(Res)
      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" tab, in the "Environment" group, expand "Panes", select "Panes", and then select "Debugger trace".
    3. The "Debugger trace" pane contains the value of the Counter 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:
LastName is string
// Assign a value to a string variable
LastName = "Doe"

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.
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
multiline 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:
LastName is string
// Assign a value to a string variable
// the content of EDT_LastName edit control
LastName = 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:
MyBuiltString is string
ProductName is string = "WINDEV Mobile"
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 project initialization event (press Ctrl + A to select all and then "Delete").
    2. In the initialization event of the "WLanguage" project, write the following code:
      MyBuiltString is string
      ProductName is string = "WINDEV Mobile"
       
      // Concatenation
      MyBuiltString = ProductName + " is my go-to development tool" + "!"
      Trace("Concatenation: " + MyBuiltString)
       
      // StringBuild
      MyBuiltString = StringBuild("%1 is my go-to development tool!", ProductName)
      Trace("StringBuild: " + MyBuiltString)
       
      // Direct input of the variable ([% %] syntax)
      MyBuiltString = "[%ProductName%] is my go-to development tool!"
      Trace("Direct input: " + MyBuiltString)

      In this code, the MyBuiltString 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', etc. 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" tab, in the "Environment" group, expand "Panes", select "Panes", and then select "Debugger trace".
    3. The "Debugger trace" pane contains our concatenation examples:
      Concatenation: WINDEV Mobile is my go-to development tool!
      StringBuild: WINDEV Mobile is my go-to development tool!
      Direct input: WINDEV Mobile is my go-to development tool!

Note

The method used to concatenate strings can improve readability and make translation easier, but it can also have an impact on performance. Thus, the first method takes about twice as long as any of the other two methods.

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 over a string and extract its substrings.
    1. Delete the code in the project initialization event (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      ProductList is string = "
      WINDEV
      WEBDEV
      WINDEV Mobile
      "
       
      FOR EACH STRING AProduct OF ProductList SEPARATED BY CR
      Trace(AProduct)
      END
      Product is string = ProductList.ExtractString(2, CR)
      Trace(Product)
    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:
      • ProductList 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).

        Note

        In WLanguage, you don't need to worry about matching upper or lowercase characters when writing in the code editor: the right case will be automatically applied as you type.
      • In our case, <String>.ExtractString extracts the second element of the string ("WEBDEV").
    4. Test the project by clicking 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 project initialization event (press Ctrl + A to select all and then "Delete").
    2. Write the following code:
      Text is string = "San Francisco"
      // Display the fifth character in the trace: "F"
      Trace(Text[5])
      // Display characters 5 to 10 in the trace: "Franci"
      Trace(Text[5 TO 10])
      // Display characters from the fifth position in the trace: "Francisco"
      Trace(Text[5 TO])
      // Display first 10 characters in the trace: "San Franci"
      Trace(Text[TO 10])
      // Display 3 characters from the 10th character in the trace: "isc"
      Trace(Text[10 ON 3])
    3. Test the project by clicking Test project in the quick access buttons.
    4. The "Debugger trace" pane contains the following information:
      F
      Franci
      Francisco
      San Franci
      isc
  • 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, etc. Here's a quick example:
    1. Delete the code in the project initialization event (press Ctrl + A to select all and then "Delete").
    2. Write the following code:
      InitialText is string = "WINDEV Mobile is my go-to development tool!"
       
      // Remove accents from string and set to lowercase
      InitialText = InitialText.Format(ccIgnoreAccent + ccLowCase)
      Trace(InitialText)
      // Remove spaces from string and set to uppercase
      InitialText = InitialText.Format(ccUpCase + ccIgnoreInsideSpace)
      Trace(InitialText)
    3. Test the project by clicking Test project in the quick access buttons.
    4. The "Debugger trace" pane contains the following information:
      windev mobile is my go-to development tool!
      WINDEVMOBILEISMYGO-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 project initialization event (press Ctrl + A to select all and then "Delete").
    2. Write the following WLanguage code:
      SearchString is string = "WINDEV Mobile"
       
      OriginalText is string = "
      Development tools:
      - WINDEV
      - WEBDEV
      - WINDEV Mobile
      "
       
      Pos is int
      Pos = Position(OriginalText, SearchString)
      IF Pos = 0 THEN
      Trace("[%SearchString%] was not found in the text")
      ELSE
      Trace("[%SearchString%] was found at position [%Pos%]")
      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 SearchString in the contents of OriginalText.
      • 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. Test the project by clicking Test project in the quick access buttons.
    4. The "Debugger trace" pane contains the following information:
      WINDEV Mobile was found at position 43

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 project initialization event (press Ctrl + A to select all and then "Delete").
    2. Write the following WLanguage code:
      // Strings to compare
      String1 is string = "DEVELOPMENT"
      String2 is string = "DEV"
       
      // The 2 strings are compared with the ~= (flexible equality) operator
      IF String1 ~= String2 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. Test the project by clicking 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 change the declaration of String2:
      String2 is string = "Development"
    6. Test the project by clicking 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 28
Comments
Click [Add] to post a comment