Navigation: Clarion.Net (Clarion#) > Clarion# Language Extensions > Variable and Entity Attributes >====== Variable Declaration outside of the DATA section ====== | ![]() ![]() ![]() |
For declaring data elements outside of any data section, the OF clause can be used for any local variable declaration.
Syntax:
Label OF DataType = InitialValue
Scope of new variable:
The new variable has the same scope as if it were declared in the CODE section, however it is NOT available in the CODE section prior to it's declaration line. The only requirement is that the variable must be initialized at the point of declaration.
Examples:
UsingOF PROCEDURE()
CODE
A1 OF LONG = 1 !A1 is declared as a LONG
L1 OF List<;LONG> = NEW List<;LONG> !reference to generic list
SomeMethod PROCEDURE()
INLINE
CODE
Ndx OF Int32=0
Customers OF List<;Customer> = NEW List<;Customer>
Cust0 OF Customer = NEW Customer()
Cust1 OF Customer = NEW Customer(){ LastName=“Z”, FirstName=“Bob”}
Cust2 OF Customer = NEW Customer{ LastName=“Z”, FirstName=“Bob”}
Cust3 OF Customer = NULL
LOOP I OF Int32 = 1 TO 4
!do something
END
FOREACH( Cust OF Customer IN Customers)
!do something
END
END !SomeMethod
Compiler Errors can occur based on the following scenarios:
·No Initial Value:
CODE
Ndx2 OF Int32 !<;– compile error:
Compiler error: Unexpected end of line (CLA00384)
·The variable is accessed prior to declaration
CODE
Debug.WriteLine(L) !<;– compile error:
L OF LONG = 0
Compiler error: L is being used before being declared (CLA00248)
·The variable declared already exists
CODE
LOOP I OF Int32 = 1 TO 4
!do something
END
LOOP I OF Int32 = 1 TO 4 !<;– compile error:
!do something
END
J Int32
CODE
J OF Int32 = 42 !<;– compile error:
Compiler error: Identifier is already defined: 'I' (Cla00185) ' (in both examples).
See also: Generics