ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

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
  • String variables in detail
  • 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 code is the very first code executed when the project is tested: we can therefore carry out 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 in our tests: display the WLanguage events linked to the project. To do this:
  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 handled by the language.
  • a type: nature 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: Limit for using 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 can observe the specific coloring of the different elements of the declaration: a specific color is used for the variable name and another 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, display the "Debugger trace" pane to see the result: under the "Home" pane, in the "Environment" group, pull down "Panes", select "Panes" then "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 you want to store. 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. Write 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). You might as well make the most of it! Please note, however, that certain characters are forbidden: space, =, period, comma, 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).
String variables in detail
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.
  • it is not necessary to declare the length of the string: WLanguage automatically adapts this length when the variable is used.

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:
MaChaîneConstruite is string
NomProduit 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:
    MaChaîneConstruite is string
    NomProduit is string = "WEBDEV"
    
    // Concaténation
    MaChaîneConstruite = "Mon outil de développement, c'est " + NomProduit + "!"
    Trace("Concaténation: " + MaChaîneConstruite)
    
    // Saisie directe de la variable (syntaxe [% %])
    MaChaîneConstruite = "Mon outil de développement, c'est [%NomProduit%]!"
    Trace("Saisie directe: " + MaChaîneConstruite)

    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: direct use of thevariable . This method combines all the advantages: simplicity of translation and direct understanding of the message.. 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 development tool!
    Direct input: WEBDEV is my 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. Write 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 analyze 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, there's no need to worry about upper or lower case when entering instructions in the code editor: the correct 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 you 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:
    TexteInitial is string = "Mon outil de développement, c'est WEBDEV!"
    
    // Formate la chaîne initiale sans accent et en minuscule
    TexteInitial = TexteInitial.Formate(ccIgnoreAccent + ccLowCase)
    Trace(TexteInitial)
    // Formate la chaîne obtenue sans espace et en majuscule
    TexteInitial = TexteInitial.Formate(ccUpCase + ccIgnoreInsideSpace)
    Trace(TexteInitial)
  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!
To find out more: Please visit our help pages 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:
    ChaîneARechercher is string = "WEBDEV"
    
    TexteOriginal is string = "
    Outils de développement:
    - WINDEV
    - WEBDEV
    - WINDEV Mobile
    "
    
    Pos is int
    Pos = Position(TexteOriginal, ChaîneARechercher)
    IF Pos = 0 THEN
    	Trace("La chaîne [%ChaîneARechercher%] n'a pas été trouvée dans le texte")
    ELSE
    	Trace("La chaîne [%ChaîneARechercher%] a été trouvée à la 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

Another common string operation is string comparison: finding out whether two strings are identical..
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 ~= (loose 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 ~= (loose 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: Many comparison operators are 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: 11/19/2024

Send a report | Local help