ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

New WEBDEV 2024 feature!
Help / WEBDEV Tutorial / Tutorial - WLanguage basics
  • Lesson 2 - Variables
  • Practical example
  • What is a variable?
  • Declaring a variable
  • Assignment and use
  • Types of variables
  • Simple operations on variables
  • Tips
  • Variable type details: String variables
  • The String type
  • Building a string
  • Operations on strings
  • Finding a string
  • Comparing two strings
  • To sum up

Tutorial - WLanguage basics

Lesson 2 - Variables
We will cover the following topics:
  • What is a variable?
  • The different types of variables.
  • The String type in detail.
Durée de la leçon 20 mn
Practical example
To follow the steps in this lesson, we will use the "WLanguage" project that we created in the previous lesson (see A project to discover WLanguage in lesson 1).
We will write the code directly in the project initialization code. This is the first section of code executed when the project is tested: we can do tests very simply, without any interface.
To view the results of our operations, we will use Trace, which lets us write the desired information both to a trace window and to the debugger pane at the bottom of the editor. In this tutorial, we will use the debugger pane.
Trace window
Trace window
"Debugger trace" pane

This pane is not displayed by default. The "Debugger trace" pane will be displayed automatically when you test the example.


First step for our tests: open the WLanguage events of the project. To do so:
  1. Right-click the "P" button next to the open element tabs. The context menu appears.
    Context menu of the project
  2. Select "Element code".
  3. The code editor displays the different WLanguage 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: Type of the data stored in the variable. In this tutorial, we will look at a few types of variable in detail.
  • a value: Information stored in the variable.
  • a scope: Scope of the variable in the program. The scope is mainly defined by the location where the variable is declared. For more details on this topic, see Scope of variables.

Declaring a variable

Before using a variable, it must first be declared (i.e. created).
First, let's write a simple declaration:
  • In the initialization event of the "WLanguage" project, write the following code:
    Price is currency
  • In this code:
    • Price represents the variable name.
    • is is used to declare the variable. WLanguage uses common, readable language.
    • currency corresponds to the variable type.
You can also declare multiple elements:
  • In the initialization event of the "WLanguage" project, write the following code:
    LastName, FirstName are strings
  • In this code:
    • LastName, FirstName represent the names of variables.
    • are is used to declare a set of variables.
    • strings represents the type of variable.
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 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" tab, in the "Environment" group, expand "Panes", select "Panes", and then select "Debugger trace".
    Warning
    Don't confuse the "Debugger" pane with the "Debugger trace" pane.
  4. The "Debugger trace" pane shows the content of the 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.
Further information:
  • WLanguage also includes "advanced" variables, which group all the characteristics of the current element into a single variable. These advanced types can be used to manipulate XML documents, emails, XLS files and more.
    For more details, see the help about the desired type (see The different types 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.
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. 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).
Variable type details: 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"
In WLanguage:
  • the " (double quote) character is used to delimit a character string. In the above example, 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.

You can also use multiline text. For example:
MyString is string
MyString = "
Example of
multiline string
"
Can the content of a control be assigned to a string?
Yes. You can also assign the content of a control that handles 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 = "WEBDEV"
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 = "WEBDEV"

    // Concatenation
    MyBuiltString = ProductName + " is my go-to development tool" + "!"
    Trace("Concatenation: " + 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 2 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: 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.

Now, 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. The "Debugger trace" pane contains our concatenation examples:
    Concatenation: WEBDEV is my go-to development tool!
    Direct input: WEBDEV is my go-to development tool!
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 the second.

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).
      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 = "WEBDEV 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:
    webdev is my go-to development tool!
    WEBDEVISMYGO-TODEVELOPMENTTOOL!
Further information: To learn more, see Manipulating character strings and String functions.

Finding a string

Searching is one of the most common operations performed on strings. At some point, everyone has tried to search for a word in a string, determine its position, etc. WLanguage simplifies these operations.

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 = "WEBDEV"

    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:
    WEBDEV was found at position 33

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.
Further information: There are many comparison operators available. For more details, see comparison operators.
To sum up
In this lesson, we learnt what a variable is, how to declare it and how to use it. We took a closer look at string variables.
In the next lesson, we will learn more about variables by manipulating dates, times and durations, arrays and structures.
Previous LessonTable of contentsNext Lesson
Minimum version required
  • Version 2024
Comments
Click [Add] to post a comment

Last update: 02/05/2024

Send a report | Local help