• No se han encontrado resultados

APROPAMENT ALS JOVES LLATINS 5 I LA SEVA ESTIGMATITZACIÓ

UN REPTE PER A TOTHOM.

4.2 APROPAMENT ALS JOVES LLATINS 5 I LA SEVA ESTIGMATITZACIÓ

Remember that if you try to assign too many characters to a string variable, you will get a run-time error. How can this be prevented, since the error does not occur until the program is running? One way is to design your program such that this error could never happen. This is how it is handled in most of Financials. In some cases, though, this is impossible.

Fortunately, there is another method.

MAXSTRLEN Function

There is a function available that will tell you (at run-time) the maximum length of a string that can fit in a variable. This function is called

MAXSTRLEN. It has one parameter, which is the variable in question. The return value is of type integer.

Enter the following two code lines into codeunit 95100, after the lines entered above:

LoopNo := MAXSTRLEN(Description);

MESSAGE('The value of %1 is %2','LoopNo',LoopNo);

The result will be that LoopNo will be set to 30, which is the length that you used when you created this variable.

Now that we know how long a string we can put into this variable, how do we put only that many characters into it? First let's look at the result if we do not do this. Type in the following lines after the ones above:

Description := 'The message is: ' + CodeB;

MESSAGE('The value of %1 is %2','Description',Description);

Once you save and run this, you will get the expected run-time error. The problem is that the assignment statement transfers the entire value of the expression on its right to the variable on its left and there is not enough space in this case. What we need is something that will only get part of a text.

That something is a function called COPYSTR. This function has three parameters.

The first is the string that is being copied from.

· ·

·

The second is the position of the first character to copy. If we wanted to start copying from the fifth character, this second parameter would be 5. If we want to start from the first character, the second parameter would be 1.

The third parameter is the number of characters to copy. This third parameter is optional and if it is not there, then all the characters of the string are copied from whatever the second parameter says to the last character. If you try to copy more characters than are in the string, there is no harm done. The maximum number of characters that will be copied is the number in the original string. The value of the COPYSTR function, once it has been evaluated, is a copy of the first parameter, but only including those characters designated by the second and third parameters. If the first parameter is a Text, the type of the COPYSTR function is Text. If the first parameter is a Code, the type of the COPYSTR function is Code.

Now, in your codeunit, find the line you typed that looks like this:

Description := 'The message is: ' + CodeB;

Change it to look like this:

Description := COPYSTR('The message is: ' + CodeB,1,MAXSTRLEN(Description));

When you save and run this, you will no longer get a run-time error. Instead, the value of Description will only include the characters that would fit.

How Did it Work?

Let us evaluate this expression "by hand" and see how it worked.

The expression is what is to the right of the assignment statement. Here is the original expression:

COPYSTR('The message is: ' + CodeB,1,MAXSTRLEN(Description))

First, to evaluate a function like COPYSTR, we must evaluate each of its parameters. The first step, to evaluate the first parameter, is to get the value of CodeB and concatenate it (note the plus sign) with the string constant. The result is this:

COPYSTR('The message is: HELLO THERE. HOW ARE YOU?',1,MAXSTRLEN(Description))

The next step is to evaluate the second parameter. Since this is a constant, it is easy. Next, we must evaluate the third parameter. This parameter is a function that returns the defined length of its parameter (which must be a variable, not an expression). The result is:

COPYSTR('The message is: HELLO THERE. HOW ARE YOU?',1,30)

Finally, the COPYSTR function itself can be evaluated. In this case, it copies characters from the text in the first parameter, starting with the first character (the second parameter) and copying up to 30 characters (the third parameter). The result is:

'The message is: HELLO THERE. '

Now that the expression has been evaluated, the assignment can be performed:

Description := 'The message is: HELLO THERE. ';

5.5 C

HAPTER

T

EST

Questions

1 Underline the expression in this assignment statement: TextA := TextB;

2 In mathematics, the things that operators operate on are called "operands". What are these things called in programming expressions?

3 What does the plus operator (+) do to text variables or constants?

4 Underline the expression in this assignment statement: TextA := 'The ' + TextB;

5 Underline the operator(s) in the expression: TextA := 'The ' + TextB;

6 Underline the term(s) in the expression: TextA := 'The ' + TextB;

Answers

1 Underline the expression in this assignment statement: TextA := TextB;

2 In mathematics, the things that operators operate on are called "operands". What are these things called in programming expressions?

Terms

3 What does the plus operator (+) do to text variables or constants? It concatenates them.

4 Underline the expression in this assignment statement: TextA := 'The ' + TextB;

5 Underline the operator(s) in the expression: TextA := 'The ' + TextB;

6 Underline the term(s) in the expression: TextA := 'The ' + TextB;

Chapter 6.