Declaring Variables

Navigation:  Macros > Macro Language > Variables >

Declaring Variables

Previous pageReturn to chapter overviewNext page

You must declare any variable that you use.

 

To declare a variable, place it in the var section of a macro, as shown in the example below.

var
   nWeekDay sDayName bIsWeekEnd
   nNumDays bIsLeapYear
begin
   // use these variable in some code
end

Some additional remarks.

 

Variable names must follow certain naming conventions.

You can declare many variables on the same line, each variable separated from the others by a space (do not use separators such as ",").

The order in which you declare variables is not important.

You can insert comments anywhere in the var block; they are ignored by the compiler.

 

Why Declare Variables ?

 

Declaring variables helps the macro compiler to verify your code and warn you if you are using variables that you have not declared. Why would this be a problem ?

 

Suppose the compiler did not check that every variable you use was previously declared, and that it just added any new variable it encounters to the list of variables it keeps in memory. Then consider then the following code.

nDayOfSundayBeforeChristmas = DayOf(nSomeDate)
bIsThe23rd = EqualN(nDateOfSundayBeforeChristmas, 23

The above code looks like it calculates the day (1-31) on which the Sunday before Christmas falls, and then checks if that is equal to 23.

 

But the first line uses the variable nDayOfSundayBeforeChristmas while the second line uses the similar looking, but different, variable nDateOfSundayBeforeChristmas. Since you did not declare variables, when the compiler encountered nDateOfSundayBeforeChristmas, in the second line, it assumed that it was a new integer variable, initialized it to 0 and then compared it to 23. Not surprisingly, bIsThe23rd turns out to be always false and you cannot figure out why.


Topic 108184 updated on 11-Feb-2019.
Topic URL: https://www.qppstudio.net/webhelp_xv4/index.html?declaringvariables.htm