• No results found

Calling Built-In Functions

13.2 U SING B UILT IN F UNCTIONS

We have already covered several built-in functions in previous chapters. Take a couple of minutes to review these:

Function Description More Information

MESSAGE Displays a message on the screen

Chapter 3

MAXSTRLEN Returns defined length of text variable

Chapter 5

COPYSTR Returns part of a string Chapter 5 CLEAR Clears the passed in

variable

Chapter 10

ARRAYLEN Returns number of elements in an array

Chapter 10

Note that some of them, like MESSAGE and CLEAR, have no return value, so they can only be called using a function call statement. Others, like COPYSTR and MAXSTRLEN, return a value, so they can be used in an expression. Also, note that CLEAR changes the passed in parameter, so it is an example of pass by reference, while the others are examples of pass by value.

The C/AL Symbol Menu

1 In the Object Designer, open codeunit 95100 again.

2 Select View, C/AL Symbol Menu from the Menu Bar, or press the F5 key. You can also press the Symbol button on the toolbar.

The following window appears:

If this does not immediately appear, scroll the left-hand panel until the function groupings appear in it, as shown above.

3 Highlight the DIALOG choice to see some of the built-in functions that display on the screen.

4 In the right-hand panel, highlight the MESSAGE function. Note that at the bottom of the frame (above the command buttons), a line appears that looks like this:

MESSAGE(String [, Value1] …)

This is to tell you the syntax of the function. Note that there is no return value and that there is one required parameter (String) and multiple optional parameters. The square brackets indicate an optional parameter and the ellipsis indicates multiples.

5 Now, highlight the ERROR function (just above MESSAGE). As you can see, its syntax looks like this:

ERROR(String [, Value1] …)

The syntax of the ERROR function is identical to that of the MESSAGE

function (except for the function identifier and the functionality.) When an ERROR function is called in a function call statement, the

processing stops with an error condition and the message is displayed in a similar manner as the MESSAGE function.

6 In the left panel, highlight SYSTEM. Note that in the middle panel, there is now a list of various function groupings, like string functions,

numeric (mathematical) functions, and so on. As you highlight each of these choices in the middle panel, the functions listed in the right-hand panel change. As an example, highlight "Date" in the middle panel and then, in the right-hand panel, highlight DATE2DMY. The syntax of this function now appears at the bottom of the window:

Number := DATE2DMY(Date, What)

The "Number :=" listed before the function identifier indicates that this function has a return value and that this return value is a number. The first parameter, "Date" is an expression of type date. But what could "What" (the second parameter) be? To find out, now that you have highlighted the DATE2DMY function, press the F1 key.

The help screen displays for this function and you can read a complete description of what it does and what it returns. There, you will find out that "What" is an integer expression that should resolve to one of three values. If it is a 1, then this function returns the day of the month. If it is a 2, then this function returns the month (from 1 to 12). If it is a 3, then this function returns the year (the full 4 digits).

Using the DATE2DMY Function

Here is an example of this function, which extracts the month from a date type variable and displays it as human readable text. You can try it in your Workbook Exercises codeunit to see how it works.

"When Was It" := TODAY;

CASE DATE2DMY("When Was It",2) OF 1:Description := 'January'; 2:Description := 'February'; 3:Description := 'March'; 4:Description := 'April'; 5:Description := 'May'; 6:Description := 'June'; 7:Description := 'July'; 8:Description := 'August'; 9:Description := 'September'; 10:Description := 'October'; 11:Description := 'November'; 12:Description := 'December'; END;

MESSAGE('%1 is in %2',"When Was It",Description);

Note that the first line in that code uses another function, called TODAY. This function has no parameters. It always returns the current date from your computer's operating system, the "system date".

13.3 C

HAPTER

T

EST

Questions

The questions in this test relate to the following code. You should hand execute this code in order to find the answers, using the Symbol Menu and the Help system to determine what the various functions do. Note that all of these functions can be found in the String subsection of the SYSTEM section. Do not actually run this code in your test codeunit until you reach question 8.

// UserInput is a Text variable of length 100. The other variables are Integers.

UserInput := 'The colors are red, orange, yellow, green, blue and violet.';

Count := 0; REPEAT

Comma := STRPOS(UserInput,','); //Q1 IF Comma > 0 THEN BEGIN //Q2

Count := Count + 1; UserInput := DELSTR(UserInput,Comma,1); //Q3 UserInput := COPYSTR(INSSTR(UserInput, ' and',Comma),1,MAXSTRLEN(UserInput)); END; UNTIL Comma <= 0; // Display Results

MESSAGE('The sentence is "%1". Number of commas is %2.',UserInput,Count);

1 What is the value of Comma after the statement labeled Q1 is executed the first time?

2 Is the value of Comma ever less than 0 in this code (see statement labeled Q2)?

3 What is the value of UserInput after the statement labeled Q3 is executed the first time?

4 What is the value of Count when the comment line is reached the first time?

5 Suppose that UserInput was redefined so that it was a Text of length 60. Now, what is its value when the MESSAGE function after the comment line is reached?

Note that the end of the sentence can be lost under the above circumstances. Let's suppose that you modify the UNTIL clause of the REPEAT statement so that it looked like this:

UNTIL (Comma <= 0) OR (STRLEN(UserInput) + 3 > MAXSTRLEN(UserInput));

6 Now, what is the value of UserInput when the MESSAGE function is reached?

7 Although better, this was not the desired result. Why didn't it work?

8 Go ahead and run the above code in your test codeunit. Do not forget that you will have to define some new variables. Now, make as simple change as you can to address the problem you uncovered in question 7. Write your change here:

Answers

1 What is the value of Comma after the statement labeled Q1 is executed the first time?

19

2 Is the value of Comma ever less than 0 in this code (see statement labeled Q2)?

No

3 What is the value of UserInput after the statement labeled Q3 is executed the first time?

The colors are red, orange, yellow, green, blue and violet.

4 What is the value of Count when the comment line is reached the first time?

4

5 Suppose that UserInput was redefined so that it was a Text of length 60. Now, what is its value when the MESSAGE function after the comment line is reached?

The colors are red and orange and yellow and green and blue 6 Now, what is the value of UserInput when the MESSAGE function is

reached?

The colors are red and orange, yellow, green, blue and violet 7 Although better, this was not the desired result. Why didn't it work?

Because a REPEAT loop is always executed at least once and the test was done after the variable had already been changed.

8 Go ahead and run the above code in your test codeunit. Do not forget that you will have to define some new variables. Now, make as simple change as you can to address the problem you uncovered in question 7. Write your change here:

Here is one possible answer:

UserInput := 'The colors are red, orange, yellow, green, blue and violet.';

Count := 0;

WHILE (STRPOS(UserInput,',') <> 0) AND (STRLEN(UserInput) + 3 <= MAXSTRLEN(UserInput)) DO BEGIN Comma := STRPOS(UserInput,','); Count := Count + 1; UserInput := DELSTR(UserInput,Comma,1); UserInput := INSSTR(UserInput,' and',Comma); END;

MESSAGE('The sentence is "%1". Number of commas is %2.',UserInput,Count);

Note that the REPEAT loop was changed into a WHILE loop. In order to do this, the condition had to be logically negated, since a REPEAT loop continues as long as the condition is FALSE, while a WHILE loop continues as long as the condition is TRUE. To logically negate any logical expression, you could put a NOT in front of it and enclose the

entire expression in parentheses. Or you could do as we did above: Change every relational operator to its opposite ( “=" to "<>" and vice

versa, ">" to "<=" and vice versa and "<" to ">=" and vice versa), plus

Chapter 14.