PC SOFT

ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Home | Sign in | English EN
This page concerns the 270056D version of WINDEV 27. Update your version.
  • In this lesson you will learn the following concepts
  • Overview
  • Array variables
  • Declaration
  • Filling an array and accessing the elements
  • Looping through an array
  • The associative array
  • Date / Time and Duration variables
  • The Date type
  • The Time type
  • The Duration type
  • Queue and Stack variables
  • The Queue type
  • The Stack type
  • Enumeration and Combination variables
  • The Enumeration and Combination types
  • Structure variables
  • The Structure type
  • Conclusion
Lesson 2.3. Overview of available variables
In this lesson you will learn the following concepts
  • The Array type.
  • The Date, Time and Duration types.
  • The Queue and Stack types.
  • The Enumeration and Combination types.
  • The Structure type.
Lesson duration

Estimated time: 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.
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 "Initializing" event of the project (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" and 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 "Initializing" 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 "Initializing" 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 "Initializing" event of the project (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.
Date / Time and Duration variables
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.

The Date type

WLanguage has different types of variables to manage dates, times and durations.
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 "Initializing" event of the project (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      gdDate 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. Type the following code:
      MyDate = Yesterday()
      MyDate = "20210929"
      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 "Initializing" event of the project (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.Time = 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 "Initializing" event of the project (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:
Queue and Stack variables

The Queue type

The Queue and Stack types are available in WLanguage. These types of variables are structured types that make it possible to group series of elements together.

  • We will use a small code example to manipulate Queues:
    1. Delete the code in the "Initializing" event of the project (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      gMyQueue is Queue of string
      This line of code declares a Queue variable that contains strings.
    3. Now, let's see how to add data. Type the following code:
      // Add data
      // The element is added at the end of the queue
      MyQueue.Enqueue("WINDEV")
      MyQueue.Enqueue("WEBDEV")
      MyQueue.Enqueue("WINDEV Mobile")
      <Queue variable>.Enqueue adds the data to the queue. Items are added at the end of the queue.
    4. To retrieve the data from the Queue, simply use <Queue variable>.Dequeue. Type the following code:
      // Retrieve value at the beginning of the queue
      Data is string
      MyQueue.Dequeue(Data)
      Trace(Data)
    5. You can also use properties on Queue variables. For example, the Count property is used to determine how many elements are in the Queue.
      // Count
      Trace("The queue contains [%MyQueue.Count%] elements")
    6. Let's test this code: click Test project in the quick access buttons.
    7. The "Debugger trace" pane displays the following text:
      WINDEV
      The queue contains 2 elements
For more details on Queue variables, see Queue.

The Stack type

  • You can use the same type of code to manipulate Stacks:
    1. Delete the code in the "Initializing" event of the project (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      // Declare a stack
      MyStack is Stack of string
       
      // Add data
      // Add element at the top of the stack
      MyStack.Push("WINDEV")
      MyStack.Push("WEBDEV")
      MyStack.Push("WINDEV Mobile")
       
      // Retrieve value from the top of the stack in a variable
      Data is string
      MyStack.Pop(Data)
      Trace(Data)
       
      // Count
      Trace("The stack contains [%MyStack.Count%] elements")
      Elements are added at the top of the stack. In this code, <Stack variable>.Push and <Stack variable>.Pop are used to manipulate the elements.
    3. Let's test this code: click Test project in the quick access buttons.
    4. The "Debugger trace" pane displays the following text:
      WINDEV Mobile
      The stack contains 2 elements
For more details on Stack variables, see Stack.
Enumeration and Combination variables

The Enumeration and Combination types

Enumerations and combinations are types of variables that allow you to easily manipulate sets of values.
  • Only one value from the enumeration can be assigned to an Enumeration variable.
  • One or multiple values of the combination can be assigned to a Combination variable.
Regardless of the type, you must declare the variable and assign one or multiple values to it in order to use it.
  • We will use a small code example to manipulate Enumerations:
    1. Delete the code in the "Initializing" event of the project (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      // Declare the enumeration
      Color is Enumeration
      Green
      Blue
      Red
      Yellow
      END
       
      // Declare an enumeration variable
      LineColor is Color
       
      // Assign a value
      LineColor = Blue
       
      // Compare value
      IF LineColor = Yellow THEN
      Trace("The line is yellow")
      ELSE
      Trace("The line is not yellow")
      END
    3. Let's test this code: click Test project in the quick access buttons.
    4. The "Debugger trace" pane displays:
      The line is not yellow
For more details on this type of variable, see Enumeration.
  • We will use a small code example to manipulate Combinations:
    1. Delete the code in the "Initializing" event of the project (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      // Declaring the combination
      Options is Combination
      HeatedSeat
      SunRoof
      LeatherSeats
      AutomaticTransmission
      END
       
      // Declare a combination variable
      CarOptions is Options
       
      // Assign a value
      CarOptions = HeatedSeat + LeatherSeats + AutomaticTransmission
       
      // Check if a value is active
      IF CarOptions[HeatedSeat] = True THEN
      Trace("The heated seats options is active")
      END
    3. Let's test this code: click Test project in the quick access buttons.
    4. The "Debugger trace" pane displays:
      The heated seats options is active
For more details on this type of variable, see Combination.
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 "Initializing" event of the project (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
      Trace(TheLastName)
    5. Let's test this code: click Test project in the quick access buttons.
    6. The "Debugger trace" pane displays:
      POLO
As with the other types of variables, it is also possible to handle arrays of structures.
  • We will use a small code example to manipulate arrays of structures:
    1. Delete the code in the "Initializing" event of the project (press Ctrl + A to select all and then "Delete").
    2. Type the following code:
      // Declare the structure
      Address is Structure
      CompanyName is string
      ContactName is string
      Address is string
      PostalCode is string
      City is string
      Country is string
      END
       
      // Declare a structure variable
      AnAddress is Address
       
      // Fill a structure
      AnAddress.CompanyName = "PC SOFT"
      AnAddress.ContactName = "Paul Doe"
      AnAddress.Address = "142 Avenue des Champs Élysées"
      AnAddress.PostalCode = "75008"
      AnAddress.City = "PARIS"
      AnAddress.Country = "FRANCE"
       
      // Declare an array of structures
      MyAddresses is array of Address
      Add(MyAddresses, AnAddress)
      Trace(MyAddresses[1].OrganizationName)
    3. Let's test this code: click Test project in the quick access buttons.
    4. The "Debugger trace" pane displays:
      PC SOFT
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 Array type,
  • the Date, Time and Duration types,
  • the Queue and Stack types,
  • the Enumeration and Combination types,
  • 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 27
Comments
Click [Add] to post a comment