|

You place a value into a variable by assigning the value to it with an equals sign. For example, the following script assigns the value 10 to a variable called "amount."


amount = 10;


You can change this value at any time by assigning a different value to the variable. (The new value simply replaces the old one.) For example, the following script assigns 45 to the amount variable, replacing the number 10:


amount = 45;


...and the following script assigns the string "Woohoo!" to the variable, replacing the number 45:


amount = "Woohoo!";


Note that you can easily replace a numeric value with a string in a variable. Having a number or a string in a variable doesn't "lock" it into only accepting that type of value-variables don't care what kind of data they hold.


This ability to hold changeable information is what makes variables so useful.


1 ) D oubl e - c li ck on t h e bu tt on obj ec t . I n t h e On C li ck s cr ip t , re pl ace t h e " He llo Wo r ld !" s t r ing wi t h a var i a bl e n ame d s t r M s g.

Just edit the script on the button object's On Click event so that it looks like this instead:


Dialog.Message("Every day I wake up and say...", strMsg);


This will make the Dialog.Message action display the current value of the strMsg variable when it is performed. Before we try it out, though, we need to assign a value to that variable somewhere.


image

Note: A common practice among programmers is to give their variables names with prefixes that help them remember what the variables are supposed to contain. One such prefix is "str," which is used to indicate a variable that contains a string. Other common prefixes are "n" for a numeric value (e.g. nCount, nTotal) and "b" for a Boolean true/false value (e.g. bLoaded, bReadyToStart).


2 ) C li ck O K t o c lo s e t h e P r op er t i e s di a log.

Once you've modified the On Click script, click OK to accept your changes and close the Properties dialog.


3 ) D oubl e - c li ck on t h e p a g e a nd c li ck on t h e S cr ip t t a b. On t h e f i r s t lin e o f t h e On P re lo a d s cr ip t , t y p e :


s t r M s g = " He llo Wo r ld !";


The script should look like this when you're done:


image


This will assign the string "Hello World!" to the variable named strMsg during the page's On Preload event. The On Preload event is triggered as soon as the page has been created in memory, just before it actually appears on the screen. This makes it a good place to put any

initialization script, such as setting up default values or preparing variables that will be used once the page is shown.


4 ) P re ss F 5 t o p rev i e w t h e p r oj ec t . Wh e n t h e p rev i e w op e n s , c li ck on t h e bu tt on t o s ee t h e me ss a g e .

The Dialog.Message action displays the message box, just like before.


image


Note that the variable's name, strMsg, is nowhere to be found...instead, the value that is currently in the variable is displayed. In this case, it's the value that was assigned to the variable in the page's On Preload event.


5 ) E x i t t h e p rev i e w. I n t h e p a g e ' s On P re lo a d s cr ip t , c h a ng e t h e va lu e t h a t i s a ss ign e d t o t h e s t r M s g var i a bl e t o " Good M o r ning !" .

Double-click on the page, and edit the script in the On Preload event so it looks like this instead:


strMsg = "Good Morning!";


6 ) P re ss F 5 t o p rev i e w t h e p r oj ec t . Wh e n t h e p rev i e w op e n s , c li ck on t h e bu tt on t o s ee t h e me ss a g e .

This time, the message looks like this:


image


As you can see, you've just changed the message without even touching the Dialog.Message action.


Now imagine if you had such Dialog.Message actions on fifty pages throughout your project, and you decided you wanted to change the text. With variables and a little planning, such changes are a piece of cake.


Adding an If Statement

The if statement gives your scripts the ability to make decisions, and do one thing or another in different circumstances.


Each if statement consists of a condition (or "test"), followed by a block of script that will only be performed if the condition is found to be true.

The basic syntax is: if condition then

do something here

end


For example:


if age < 18 then

Dialog.Message("Sorry!", "You must be 18 to access this CD."); Application.Exit();

end


The above script checks to see if the value in a variable named "age" is less than 18. If it is, it puts up a message saying "You must be 18 to access this CD," and then immediately exits from the application.


For example, if we set age to 17 first:


age = 17;

if age < 18 then

Dialog.Message("Sorry!", "You must be 18 to access this CD."); Application.Exit();

end


...the block of script between the "then" and "end" keywords will be performed, because 17 is less than 18. In this case, we say that the if statement's condition "passed."


However, if we set age to 20:


age = 20;

if age < 18 then

Dialog.Message("Sorry!", "You must be 18 to access this CD."); Application.Exit();

end


...the block of script between the "then" and the "end" isn't performed, because 20 isn't less than

18. This time, we say that the if statement's condition "failed."


Note: An if statement's condition can be any expression that evaluates to true or false.


image

|