DECLARE Verb
Syntax
DECLARE {AUTO} type variable
Description
In BBj 6.0 and higher, the DECLARE verb is used to indicate that a variable may only contain values of the specified type. The DECLARE verb introduces type-safety to the BBj language and allows detection of incompatible assignments at compile time when the type checker (bbjcpl -t) is used.
Parameter |
Description |
---|---|
AUTO |
In BBj 8.0 and higher, the AUTO modifier signifies that assignments to the variable will automatically do a CAST() to the variable if the value (as opposed to the type) of the expression on the right hand side of an assignment is assignment compatible with the variable on the left hand side. |
type |
A BBj Object type, Custom Object type, or a Java class or primitive. Examples of BBj Objects include BBjString and BBjDataBoundGrid. Custom Objects are those created using the CLASS verb. Custom Objects must either be defined in the same program file, fully qualified, or locatable by a USE verb or config.bbx directive. Java types include classes such as java.lang.String and com.keypoint.PngEncoder. Java Objects must either be fully qualified or locatable by a USE verb or config.bbx directive. In BBj 8.0 and higher, an @ suffix following a type name indicates a ClientObject reference. In BBj 18.0 and higher, the DECLARE verb can declare a Java array object by appending one or more [] after a Java class or primitive type name. |
variable |
A variable that will carry values as specified bytype. Valid variable names and suffixing based on type is explained in BBj Data Types. |
Remarks
If a variable is declared to be a specific type, then expressions that do not produce a type that is assignable to the declared type may not be assigned to the variable. Any attempt to assign an expression that is not of the correct type will result in a runtime error. In addition, the TypeChecker will generate an error when the code is type checked if the code includes a statement that assigns a value of incorrect type to the variable.
An exception to this rule is when the DECLARE statement includes an AUTO modifier. This allows assignments of expressions that produce a value with a type that is assignment compatible to the attached type of the variable.
A DECLARE statement that appears within a method body (see Method Verb) only affects the use of the declared variable within the method body. A DECLARE statement that occurs in 'open' code (not within a class) affects all usage of the variable within the program in which the DECLARE statement is found except for code that is within a method (i.e. the DECLARE statement only affects the region in which it is defined).
A DECLARE statement is similar to a DATA statement in that it does not need to be 'executed' in order to take effect. The presence of a DECLARE statement will attach a type to a variable just by being present in a program.
Example 1
|
Example 2
|
Example 3 (BBj 8.0 and higher)
|
Example 4 (BBj 18.0 and higher)
|