PC SOFT

ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Home | Sign in | English EN
New WINDEV Mobile 26 feature!
  • 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 3.4. 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:
arrString is array of strings
arrInt 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
      garrMyArray is array of string
       
      // Add elements
      garrMyArray.Add("WINDEV")
      garrMyArray.Add("WEBDEV")
      garrMyArray.Add("WINDEV Mobile")
       
      // Display the content of third element
      Trace("Value of element 3: [%garrMyArray[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
garrDay is array of string
// Initialization with
// the names of the days of week
garrDay = ["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 nIndex = 1 TO garrMyArray.Count
      Trace("Value of element [%nIndex%]: [%garrMyArray[nIndex]%]")
      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 sAString, nIndex OF garrMyArray
      Trace("Value of element [%nIndex%]: [%sAString%]")
      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
      gaaMyArray is associative array of Date
       
      // Add elements
      gaaMyArray["Mark"] = "19820201"
      gaaMyArray["Anne"] = "19840604"
       
      // Display contents of the element with key "Mark"
      Trace("Value of element with key ""Mark"": " + gaaMyArray["Mark"])
       
      // Loop through the array
      FOR EACH ELEMENT dValue, sKey, nIndex OF gaaMyArray
      Trace("Value of element [%nIndex%] with key [%sKey%]: [%dValue%]")
      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 "Marc": 19820201
      Value of element 1 with key Marc: 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:
      gdDate = Yesterday()
      gdDate = "20200929"
      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
      gdDate.Year = 1985
      gdDate.Month = 10
      gdDate.Day = 26
    5. <Date>.ToString allows you to define the format used to display dates. For example, enter the following code:
      Trace(gdDate.ToString())
      Trace(gdDate.ToString("Dddd Mmmm DD YYYY"))
      Trace(gdDate.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 gdDate < 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
      gtTime is Time // By default, the time is set to the current time
       
      // Assign a time
      // - via a WLanguage function
      gtTime = Now()
      // - set in the code (e.g. 3:25 PM)
      gtTime = "1525"
       
      // Change time parameters
      gtTime.Hour = 12
      gtTime.Minute = 0
      gtTime.Second = 0
       
      // Display time in a readable format
      Trace(gtTime.ToString())
      // Display time in a readable format using a display mask
      Trace(gtTime.ToString("HH:mm:SS"))
       
      // Compare 2 times
      IF gtTime < 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
      gduDuration is Duration
       
      // Assign a duration
      // - with a value in readable format
      gduDuration = 2 min 8 s
      // or
      gduDuration = 128 s
      // - by specifying the duration in seconds, in minutes, ...
      gduDuration.InSeconds = 128
       
      // Display duration in a readable format
      Trace(gduDuration.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
      gMyQueue.Enqueue("WINDEV")
      gMyQueue.Enqueue("WEBDEV")
      gMyQueue.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
      gsData is string
      gMyQueue.Dequeue(gsData)
      Trace(gsData)
    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 [%gMyQueue.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
      gMyStack is Stack of string
       
      // Add data
      // Add element at the top of the stack
      gMyStack.Push("WINDEV")
      gMyStack.Push("WEBDEV")
      gMyStack.Push("WINDEV Mobile")
       
      // Retrieve value from the top of the stack in a variable
      gsData is string
      gMyStack.Pop(gsData)
      Trace(gsData)
       
      // Count
      Trace("The stack contains [%gMyStack.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
      EColor is Enumeration
      Green
      Blue
      Red
      Yellow
      END
       
      // Declare an enumeration variable
      geLineColor is EColor
       
      // Assign a value
      geLineColor = Blue
       
      // Compare value
      IF geLineColor = 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
      COOptions is Combination
      HeatedSeat
      SunRoof
      LeatherSeats
      AutomaticTransmission
      END
       
      // Declare a combination variable
      gcoCarOptions is COOptions
       
      // Assign a value
      gcoCarOptions = HeatedSeat + LeatherSeats + AutomaticTransmission
       
      // Check if a value is active
      IF gcoCarOptions[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:
      STFullName is Structure
      sLastName is string
      sFirstName is string
      END
      This code declares a structure composed of two variables, sLastName and sFirstName. 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:
        gstContact is STFullName
      • Assign values to the members of the structure. Add the following WLanguage code:
        gstContact.sLastName = "POLO"
        gstContact.sFirstName = "MARCO"
    4. To read the contents of a member, simply use:
      <Variable name>.<Member name>
      In our example, type the following code:
      gsTheLastName is string
      gsTheLastName = gstContact.sLastName
      Trace(gsTheLastName)
    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
      STAddress is Structure
      sCompanyName is string
      sContactName is string
      sAddress is string
      sPostalCode is string
      sCity is string
      sCountry is string
      END
       
      // Declare a structure variable
      gstAnAddress is STAddress
       
      // Fill a structure
      gstAnAddress.sCompanyName = "PC SOFT"
      gstAnAddress.sContactName = "Paul Doe"
      gstAnAddress.sAddress = "142, New Valley Avenue"
      gstAnAddress.sPostalCode = "TR14 7QK"
      gstAnAddress.sCity = "PORTSMOUTH"
      gstAnAddress.sCountry = "ENGLAND"
       
      // Declare an array of structures
      garrMyAddresses is array of STAddress
      Add(garrMyAddresses,gstAnAddress)
      Trace(garrMyAddresses[1].sCompanyName)
    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 26
This page is also available for…
Comments
Click [Add] to post a comment