ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

New WINDEV 28 feature!
Help / WINDEV Tutorial / Tuto - The WLanguage basics
  • In this lesson you will learn the following concepts
  • Overview
  • Date, Time and Duration variables
  • The Date type
  • The Time type
  • The Duration type
  • Array variables
  • Declaration
  • Filling an array and accessing the elements
  • Looping through an array
  • The associative array
  • Structure variables
  • The Structure type
  • Conclusion
Lesson 2.3. Overview of available variables
In this lesson you will learn the following concepts
  • The Date, Time and Duration types.
  • The Array type.
  • The Structure type.
Lesson duration

Estimated duration: 1 h
Previous LessonTable of contentsNext Lesson
Overview
In addition to simple types (boolean, integer, real, etc.), WLanguage has various types of variables used to handle many specific cases.
We will get an overview of these different types of variables with a small practical example for each one. For more details, don't hesitate to refer to the online help of each type.
Date, Time and Duration variables
WLanguage has different types of variables to manage dates, times and durations.

The Date type

For dates, WLanguage has the Date type. You can use many functions and properties to manipulate dates.
  • We will use a small code example to manipulate dates:
    1. Delete the code in the project initialization event (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      MyDate is Date
      This line of code declares a Date variable. By default, the variable is initialized with the current date.
    3. Now, let's see how to assign a value to a date.
      MyDate = Yesterday()
      MyDate = "20210929"
      This code presents the different ways to initialize a variable of type Date. The variable can be initialized:
      • via a WLanguage function. In this example, Yesterday is used. WLanguage has many functions to determine the first or last day of a month or year, ...
      • via a string containing the date in "YYYYYMMDD" format.
    4. You can also change the date parameters using WLanguage properties. Type the following code:
      // Change date parameters
      MyDate.Year = 1985
      MyDate.Month = 10
      MyDate.Day = 26
    5. <Date>.ToString allows you to define the format used to display dates. For example, enter the following code:
      Trace(MyDate.ToString())
      Trace(MyDate.ToString("Dddd Mmmm DD YYYY"))
      Trace(MyDate.ToDayInAlpha())
    6. Let's test this code: click Test project in the quick access buttons.
    7. The "Debugger trace" pane contains:
      26/10/1985
      Saturday October 26 1985
      Saturday
      • the date in a readable format.
      • the date in a readable format, using a display mask.
      • the day of the week that corresponds to the date.
    8. You can also use comparison operators between dates. Type the following code:
      IF MyDate < Today() THEN
      Trace("The date is earlier than today")
      END
    9. Let's test this code: click Test project in the quick access buttons.
    10. The "Debugger trace" pane contains:
      26/10/1985
      Saturday October 26 1985
      Saturday
      The date is earlier than today

The Time type

  • Similarly, you can manipulate times using the Time type.
    1. Delete the code in the project initialization event (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      // Declare a time
      MyTime is Time // By default, the time is set to the current time
       
      // Assign a time
      // - via a WLanguage function
      MyTime = Now()
      // - set in the code (e.g. 3:25 PM)
      MyTime = "1525"
       
      // Change time parameters
      MyTime.Hour = 12
      MyTime.Minute = 0
      MyTime.Second = 0
       
      // Display time in a readable format
      Trace(MyTime.ToString())
      // Display time in a readable format using a display mask
      Trace(MyTime.ToString("HH:mm:SS"))
       
      // Compare 2 times
      IF MyTime < Now() THEN
      Trace("The time is earlier than the current time")
      ELSE
      Trace("The time is later than the current time")
      END
      This code takes all the possibilities presented on dates, and applies them to times.
    3. Let's test this code: click Test project in the quick access buttons.
    4. The "Debugger trace" pane displays the desired information.
      12:00:00:00
      12:00:00
      The time is earlier than the current time
      • the time in a readable format.
      • the time in a readable format, using a display mask.

The Duration type

  • The same type of code can be written for durations.
    1. Delete the code in the project initialization event (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      // Declare a duration
      MyDuration is Duration
       
      // Assign a duration
      // - with a value in readable format
      MyDuration = 2 min 8 s
      // or
      MyDuration = 128 s
      // - by specifying the duration in seconds, in minutes, ...
      MyDuration.InSeconds = 128
       
      // Display duration in a readable format
      Trace(MyDuration.ToString("MM:SS"))
    3. Let's test this code: click Test project in the quick access buttons.
    4. The "Debugger trace" pane displays the desired information.
      02:08
For more details on Date, Time, and Duration variables, see the corresponding help pages:
Array variables
Arrays are a widely used type of variable.
An array is a structured type that is used to group a set of elements of the same type. Each array element can be directly accessed by its index.
Specific WLanguage functions are used to handle the arrays and their elements.

Declaration

An Array variable is declared as follows:
<Array name> is array of <Type of array elements>
For example:
MyArrayOfStrings is array of strings
MyArrayOfIntegers is array of int

Filling an array and accessing the elements

During its declaration, the array is empty. The elements are added by <Array>.Add via the following syntax:
<Array name>.Add(<Element value>)
To access the array elements, use the following syntax:
<Array name> [<Element index>]

Important

Array element indices start at 1.
  • We will use a small code example to add elements in an array:
    1. Delete the code in the project initialization event (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      // Create an array of strings
      MyArray is array of strings
       
      // Add elements
      MyArray.Add("WINDEV")
      MyArray.Add("WEBDEV")
      MyArray.Add("WINDEV Mobile")
       
      // Display the content of the third element
      Trace("Value of element 3: [%MyArray[3]%]")
    3. Let's test this code: click Test project in the quick access buttons.
    4. 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".
    5. The "Debugger trace" pane contains the following information:
      Value of element 3: WINDEV Mobile

Tip

Fast array initialization
You can also use the following syntax to initialize an array:
// Declare an array
DayArray is array of strings
// Initialization with
// the names of the days of the week
DayArray = ["Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"]

Looping through an array

WLanguage offers two methods to loop through the elements of an array:
  • A standard FOR .. TO loop.
  • A FOR EACH loop.
  • In a standard FOR .. TO .. loop, both the values of the start index (1) and end index must be specified.
    1. For example, add the following code in the "Initialization" event of the project:
      FOR Index = 1 TO MyArray.Count
      Trace("Value of element [%Index%]: [%MyArray[Index]%]")
      END

      In this code, the start index is 1 and the end index is given by the Count property of the array.
    2. Let's test this code: click Test project in the quick access buttons.
    3. The "Debugger trace" pane contains the following information:
      Value of element 1: WINDEV
      Value of element 2: WEBDEV
      Value of element 3: WINDEV Mobile
  • WLanguage offers a much simpler and just as effective type of loop: the FOR EACH loop. We have already seen it on strings. This type of loop also applies to arrays. In this case, there is no need to specify the start or end index.
    1. For example, replace the code in the FOR loop with the following code in the "Initialization" event of the project:
      FOR EACH ELEMENT AString, Index OF MyArray
      Trace("Value of element [%Index%]: [%AString%]")
      END
      In this case, we simply had to specify the name of the variable that corresponds to the array element and the variable that corresponds to the index.
    2. Let's test this code: click Test project in the quick access buttons.
    3. The "Debugger trace" pane contains the following information:
      Value of element 1: WINDEV
      Value of element 2: WEBDEV
      Value of element 3: WINDEV Mobile
For more details on Array variables, see Array.

The associative array

An associative array is an "advanced" type of array: it is used to group a set of elements of the same type. Each element of the array is indexed on any type of information (and not simply on a numeric index, as in the other types of arrays).
Associative arrays (with or without duplicates) can be manipulated in WLanguage.

  • Let's do a quick test.
    1. Delete the code in the project initialization event (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      // Create an associative array of dates
      MyArray is associative array of Date
       
      // Add elements
      MyArray["Mark"] = "19820201"
      MyArray["Anne"] = "19840604"
       
      // Display contents of the element with key "Mark"
      Trace("Value of element with key ""Mark"": " + MyArray["Mark"])
       
      // Loop through the array
      FOR EACH ELEMENT Value, Key, Index OF MyArray
      Trace("Value of element [%Index%] with key [%Key%]: [%Value%]")
      END
    3. Let's test this code: click Test project in the quick access buttons.
    4. The "Debugger trace" pane contains the following information:
      Value of element with key "Mark": 19820201
      Value of element 1 with key Mark: 19820201
      Value of element 2 with key Anne: 19840604
For more details on Associative array variables, see Associative array.
Structure variables

The Structure type

A structure is a custom type of data. Structures contain different types of variables.
  • We will use a small code example to manipulate Structures:
    1. Delete the code in the project initialization event (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      FullName is Structure
      LastName is string
      FirstName is string
      END
      This code declares a structure composed of two variables, LastName and FirstName. Variables that are part of a structure are called "Members". To manipulate members, you must declare a variable of the type as the structure.
    3. We are now going to:
      • Declare a structure variable. Add the following WLanguage code:
        Contact is FullName
      • Assign values to the members of the structure. Add the following WLanguage code:
        Contact.LastName = "POLO"
        Contact.FirstName = "MARCO"
    4. To read the contents of a member, simply use:
      <Variable name>.<Member name>
      In our example, type the following code:
      TheLastName is string
      TheLastName = Contact.LastName
      // Displays the content of TheLastName
      Trace(TheLastName)
      // Displays Contact.LastName directly (without using a variable)
      Trace(Contact.LastName)
    5. Let's test this code: click Test project in the quick access buttons.
    6. The "Debugger trace" pane displays:
      POLO
      POLO
As with the other types of variables, it is also possible to handle arrays of structures.
  • Let's complete our example to test the use of arrays of structures:
    1. Type the following code after the code already entered:
      // Declare an array of structures
      MyContacts is array of FullName
      Add(MyContacts, Contact)
      Trace(MyContacts[1].LastName, MyContacts[1].FirstName)
      In this code, an array containing the FullName structure is created, then the previously created contact is added and finally the last name and the first name of the contact are displayed.
    2. Let's test this code: click Test project in the quick access buttons.
    3. The "Debugger trace" pane now displays:
      POLO
      POLO
      POLO MARCO
For more details on Structures and Arrays of structures, see Structure.
Conclusion
In this lesson, you have discovered some of the types of variables available in WLanguage:
  • the String type,
  • the Date, Time and Duration types,
  • the Array type,
  • the Structure type.
In the next lesson, we will see how to manipulate some of these variables using conditional statements available in WLanguage.
Previous LessonTable of contentsNext Lesson
Minimum version required
  • Version 28
Comments
Click [Add] to post a comment

Last update: 06/12/2023

Send a report | Local help