PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
This content comes from an automatic translation.
  • NOTE1
  • 1410087510
  • The scope is mainly defined by the location where the variable is declared
  • //############
  • The variable content can be read and handled
  • These advanced types group all characteristics of the element currently used in a single variable
  • We talk of variable scope.
  • A variable declared at the level of an Procedure Collection has the same visibility as a variable declared at the project level.
  • Local scope
  • The variable is global to the project.
  • Other operators are available
  • space
  • NOTE6
  • Info(Left(str,6)) // Displays "WEBDEV"
  • Info("The displayed text corresponds to " + sStringToCompare): ELSE
  • This operator is used to perform a comparison while ignoring the case, the spaces found before and after the string and the accented characters
  • Run the test of a page
  • Add(, )
Appendix 2. The variables
] ]
NOTE1
  • NOTE1_1
  • 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.
Previous LessonTable of contentsNext Lesson
1410087510
NOTE2. NOTE2_1
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: Declaring and initializing a variable
  • a type: Nature of data stored in the variable (see %00000029% %00000029%).
  • NOTE3_1: The types of variables. a value.
[%00000036%])]]

The scope is mainly defined by the location where the variable is declared

NOTE4_1
  • The scope of variables
    Price is currency
    • Declaring a variable
    • The variable must be declared (which means created) before it can be used.. Example of simple declaration:.
    • //############.
  • Price is currency
    LastName, FirstName are strings
    • is is used to declare the variable
    • The everyday language is used in WLanguage
    • currency corresponds to the variable type.

//############

LastName, FirstName are strings.
LastName, FirstName represent the names of variables.
// Assign a currency variable
Price = 1256.67
// Assign a string variable
LastName = "Doe"
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"
Info(Price)
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)
  • NOTE3
  • NOTE3_1
  • 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),
Important

Use the type corresponding to the information that must be stored

Therefore, you will optimize memory and avoid calculation or process errors when using variables in WLanguage functions., ...
For more details, see the help about the desired type (see %00000083% %00000083%).. The different types of variables.
See the online help regarding the relevant type for more details., ... Remark
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.. NOTE4
NOTE4_1
  • The scope of variables
  • The variables can be declared anywhere in the code

We talk of variable scope.

Two types of scope are available:. Global.. Local.
  • NOTE4_2
  • 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..
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 %00000114% %00000114% for more details. NOTE5_1

A variable declared at the level of an Procedure Collection has the same visibility as a variable declared at the project level.

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.
  • NOTE4_4. Summary scope diagram
Scope of variables
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.
  • NOTE5
  • NOTE5_1
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.
// 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
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,

Other operators are available

  • See %00000168% %00000168% for more details.. List of operators
  • Other operators are available. See the online help for more details (keyword: "Operators").: NOTE5_2
  • 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
space
For example, to store several digits, you may have to:
use a numeric variable if this variable must be used for calculations.

NOTE6

NOTE6_1
Details of variable type
LastName is string
// Assign a string variable
LastName = "Doe"

NOTE6_2: 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.:
<Variable name> = [
<Text of line 1>
. ]
LastName is string
// Assign a string variable
// with the content of EDT_LastName edit control
LastName = EDT_LastName

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
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"
To check the flexible equality

Specific WLanguage functions are used to perform various operations

search, extraction, size, switch to uppercase characters, .... Examples:
//############:
Info(Middle(Left(str,13),8,2)) // Displays "is"
str = "WEBDEV is a great tool"

Info(Left(str,6)) // Displays "WEBDEV"

// Extract a sub-string from right. Info(Right(str,4)) // Displays "tool"
  • Remark
    1. The different WLanguage functions can be nested.
    2. A WLanguage function can be used as parameter of another WLanguage function.
For example: //############
  • See the help about the character strings and about the functions for handling character strings for more details.
  • See the following help pages for more details
  • Managing character strings
    1. , . Functions for handling character strings: .. NOTE6_3
    2. To handle the different concepts of this lesson, we are going to create different windows
    3. These windows will be created in the "My_First_Pages" project.. Open the "My_First_Pages" project if necessary.. Open the WEBDEV home page (Ctrl + <)
    4. Click "Tutorial", then "My first pages (Exercise)".. To handle the Character String variables, we are going to create the following page
    5. This page is used to:
  • Test page
    1. find a string inside another one.
    2. compare two strings.
    3. Create a new blank page:
    4. Click among the quick access buttons:
      • The element creation window appears: click "Page" then "Page"
      • The page creation wizard appears.: Create an element.
    5. Select "Blank".. Validate. The page is automatically created in the editor
    6. The window for saving an element is displayed.. Specify the page title: "Variables"
Creation
  • Usual controls
    1. Creating an edit control
    2. Right-click the control and select "Description".
    3. In the "General" tab, specify. The control name. "EDT_Text".
    4. The control caption
    5. "Text".
    6. Select the "Content" tab
      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
      Finding a string
      • To create the "Find a string" Button control:: Under the "" section, in the "" group, click on .
      • Creation. Usual controls
      • Click the location where the Button control will be created (below the edit control for example).. Click the control that was just created.
      • Type the caption: "Find a string".. Press Enter to validate the input.
  • //############
    1. sSoughtString is string = "WEBDEV"
    2. nPos = Position(EDT_Text, sSoughtString). IF nPos = 0 THEN. Info(sSoughtString + " not found in the text"). ELSE
    3. END. Let's study this code:
    4. Two variables are declared
a String variable corresponding to the sought string and an Integer variable corresponding to the position of sought string.
  • is used to find a string inside another one
    1. In our example, we search for the content of sSoughtString in the content of EDT_Text control.
    2. The IF statement is used to check the result of
    3. This type of statement will be presented later in this section on WLanguage. Position. If returns 0, it means that the sought string was not found
    4. A message is displayed by .
    5. Position.
    6. Info
    7. Close the code window (click X at the top right corner of code editor).
      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
      • Word found. Modify the content of edit control (by replacing WEBDEV by WB for example) and click the button
      • The WEBDEV word is not found.. Close the browser..
        Comparing two strings: To create the "Compare two strings" Button control:.
      • Under the "" section, in the "" group, click on .
  • Creation
    1. Usual controls
    2. Click the location where the Button control will be created (for example, to the right of the existing Button control).. Click the control that was just created. The text displayed in the control becomes editable
    3. Press Enter to validate the input.. Adapt the control size
    4. Display the events associated with the Button control (press F2).. Write the following code in the event "Click on (Server)":
    5. //############
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.

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.
arrString is array of strings
arrInt is array of int

Run the test of a page

Click the "Compare two strings" button. The message indicates that the comparison is not correct
Word not found
In the edit control, type "WEBDEV" in uppercase characters and click the "Compare two strings" button
The WEBDEV word is found.

Modify the content of edit control (by replacing "WEBDEV" by "WEBDEV" for example) and click the button

The WEBDEV word is also found.
Close the browser.
// 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"
These functions start with "Array".

NOTE7_2

Declaration
The declaration of an Array variable is performed as follows:
// 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"]
NOTE7_3

Add(, )

To access the array elements, use the following syntax:: //############, ...
]
Previous LessonTable of contentsNext Lesson
Minimum version required
  • Version 24
Comments
Click [Add] to post a comment