|
|
|
|
|
- Lesson 3 - Dates, arrays and structures
- 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
- To sum up
Tutorial - WLanguage basicsLesson 3 - Dates, arrays and structures We will cover the following topics: - The Date, Time and Duration types.
- The Array type.
- The Structure type.
20 mn 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, see 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: - Delete the code in the project initialization event (press Ctrl + A to select all and then "Delete").
- Write the following code:
This line of code declares a Date variable. By default, the variable is initialized with the current date. - 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. Initialization can be performed: - 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.
- You can also change the date parameters using WLanguage properties. Write the following code:
MyDate.Year = 1985
MyDate.Month = 10
MyDate.Day = 26
- <Date>.ToString allows you to define the format used to display dates. You can also use <Date>.ToDayInAlpha. For example, enter the following code:
Trace(MyDate.ToString())
Trace(MyDate.ToString("Dddd Mmmm DD YYYY"))
Trace(MyDate.ToDayInAlpha())
- Let's test this code right away: click on among the quick access buttons.
- 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 corresponding to the date.
- You can also use comparison operators between dates. Write the following code:
IF MyDate < Today() THEN
Trace("The date is earlier than today")
END
As its name suggests, Today gets the date of the current day. - Let's test this code right away: click on among the quick access buttons.
- 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. - Delete the code in the project initialization event (press Ctrl + A to select all and then "Delete").
- Write the following code:
MyTime is Time
MyTime = Now()
MyTime = "1525"
MyTime.Hour = 12
MyTime.Minute = 0
MyTime.Second = 0
Trace(MyTime.ToString())
Trace(MyTime.ToString("HH:mm:SS"))
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. - Let's test this code right away: click on among the quick access buttons.
- 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. - Delete the code in the project initialization event (press Ctrl + A to select all and then "Delete").
- Write the following code:
MyDuration is Duration
MyDuration = 2 min 8 s
MyDuration = 2 min 8 s
MyDuration.InSeconds = 128
Trace(MyDuration.ToString("MM:SS"))
- Let's test this code right away: click on among the quick access buttons.
- The "Debugger trace" pane displays the desired information.
Arrays are a widely used type of variable. An array is a structured type used to group together 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>]
Warning
Array element indexes start at 1.
We will use a small code example to add elements in an array: - Delete the code in the project initialization event (press Ctrl + A to select all and then "Delete").
- Write the following code:
MyArray is array of strings
MyArray.Add("WINDEV")
MyArray.Add("WEBDEV")
MyArray.Add("WINDEV Mobile")
Trace("Value of element 3: [%MyTable[3]%]")
- Let's test this code right away: click on among the quick access buttons.
- If necessary, display the "Debugger trace" pane to see the result: under the "Home" pane, in the "Environment" group, pull down "Panes" and select "Panes" then "Debugger trace".
- The "Debugger trace" pane contains the following information:
Value of item 3: WINDEV Mobile
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.
Standard FOR .. TO loop In a standard FOR .. TO loop, both the values of the start index (1) and end index must be specified. - For example, add the following code in the project initialization event:
FOR Index = 1 TO MyArray.Count
Trace("Value of element [%Index%]: [%MyTable[Index]%]")
END
In this code, the start index is 1 and the end index is given by the Count property of the array. - Let's test this code right away: click on among the quick access buttons.
- The "Debugger trace" pane contains the following information:
Value of element 1: WINDEV Value of item 2: WEBDEV Value of item 3: WINDEV Mobile
FOR EACH loop WLanguage offers a much simpler type of loop that is just as powerful: the FOR ALL 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. - 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%]: [%string%]")
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. - Let's test this code right away: click on among the quick access buttons.
- The "Debugger trace" pane contains the following information:
Value of element 1: WINDEV Value of item 2: WEBDEV Value of item 3: WINDEV Mobile
For more details on Array variables, see Array. The associative array An associative array is an "advanced" type of array: it can be used to group a series 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. - Delete the code in the project initialization event (press Ctrl + A to select all and then "Delete").
- Write the following code:
MyArray is associative array of Date
MyArray["Mark"] = "19820201"
MyArray["Anne"] = "19840604"
Trace("Value of element with key ""Marc"": " + MyFile["Marc"])
FOR EACH ELEMENT Value, Key, Index OF MyArray
Trace("Value of element [%Index%] with key [%Key%]: [%Value%]")
END
- Let's test this code right away: click on among the quick access buttons.
- 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
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: - Delete the code in the project initialization event (press Ctrl + A to select all and then "Delete").
- Write 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. - We are now going to:
- Declare a structure variable. Add the following WLanguage code:
- Assign values to the members of the structure. Add the following WLanguage code:
Contact.LastName = "POLO"
Contact.FirstName = "MARCO"
- 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)
Trace(Contact.LastName)
- Let's test this code right away: click on among the quick access buttons.
- The "Debugger trace" pane displays:
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: - Type the following code after the code already entered:
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. - Let's test this code right away: click on among the quick access buttons.
- The "Debugger trace" pane now displays:
For more details on Structures and Arrays of structures, see Structure. In this lesson, you have discovered some of the types of variables available in WLanguage: - 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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|