#### Declaring variables without immediately specifying their type

In general, the type of a variable does not need to be explicitly given when declaring it. In such cases, the variable's type is determined by the first assignment that is made to it. This is similar to the special variable formula. This is different from in Visual Basic where a variable whose type is not given at declaration automatically has the Variant type. However, in practice, it means that you can write formulas in a similar style to what you would do if using a Variant in Visual Basic.

    Dim p 'The type of p is not known yet
    p = "bye" 'The type of p is now set to be String
    Dim q 'The type of q is not known yet
    q = Array ("hello", p) 'q is a String Array
    'Error- p is a String variable and cannot hold a Number
    p = 25
    Dim r
    'r is a Number variable, and holds the value 5
    r = (10 + 5) / 3
    'The types of a and c are not known yet
    Dim a, b As Boolean, c
    b = False
    'The type of a is now set to Boolean
    'and its value is False
    a = b
    'The type of c is now set to Number and its value is 17
    c = 2 + 3 * 5

##### Examples of declaring and initializing range variables

    Dim gradeA, quarter
    'The type of gradeA is set to Number Range
    gradeA = 90 To 100
    'The type of quarter is set to Date Range
    quarter = CDate (1999, 10, 1) To CDate (1999, 12, 31)

### Variable Scope

Variable scopes are used to define the degree to which variables in one formula are made available to other formulas. There are three levels of scope in Seagate Crystal Reports: local, global and shared. Every variable has a scope, and this scope is specified when the variable is declared.

#### Local Variables

Variables with local scope, also known as local variables, are declared using either the Dim or Local keywords. For example, all the declarations in the previous section using Dim were declaring local variables. Another example:

`Local x As Number 'equivalent to Dim x As Number`

Local variables are restricted to a single formula and a single evaluation of that formula. This means that you cannot access the value of a local variable in one formula from a different formula.

##### Example

    Rem Formula A
    Local x as Number
    x = 10
    formula = x
    Rem Formula B
    EvaluateAfter ({@Formula A})
    Local x as Number
    formula = x + 1

The function call EvaluateAfter ({@Formula A}) ensures that Formula B will be evaluated after Formula A is evaluated. Formula A returns a value of 10 and Formula B returns a value of 1. Formula B does not have access to Formula A's x and thus cannot use the value of 10 and add 1 to it; instead, it uses the default value for the uninitialized local variable x found in Formula B, which is 0, and adds 1 to it to get 1.

You can also create local variables with the same name but different types in different formulas. For example, the type declarations in formulas A and B do not conflict with:

    Rem Formula C
    Local x as String
    x = "hello"
    formula = x

Local variables are the most efficient of the three scopes. In addition, they do not interfere with one another in different formulas. For these reasons, it is best to declare variables to be local whenever possible.

Seagate Software, Inc. http://www.seagatesoftware.com Please send comments to: techpubs@seagatesoftware.com