Variables represent storage locations. Every variable has a type that determines what values can be stored in the variable. The Fore.NET language is a type-safe language and the compiler that guarantees that values stored in variables are always of the appropriate type. The value of a variable can be changed through the assignment of a new value.
A variable must be definitely assigned before it can be used.
The Fore.NET language determines seven variable categories: static variables, instance variables, array elements, value parameters, reference parameters, output parameters, and local variables.
Static variables. A field declared with the Shared modifier is named a static variable. A static variable comes into existence before execution of the static constructor for its containing type and ceases to exist when the associated application domain where it was created ceases to exist.
Instance variables. A field declared without the Shared modifier is named an instance variable. An instance variable of a class comes into existence when a new instance of that class is created and ceases to exist when there are no more references to that instance. The initial value of an instance variable of a class is the default value of the variable's type. An instance variable of a structure has exactly the same life cycle as the structure, to which it belongs. An instance variable of a structure is declared only when the containing structure is declared.
Array elements. Array elements come into existence when an array instance is created and cease to exist when there are no references to that array instance. The initial value of each of the array elements is the default value of the array elements' type.
Value parameters. A method parameter declared without the Var and Out modifiers is known as a value parameter. A value parameter comes into existence upon invocation of the method, to which the parameter belongs, and is initialized with the value of the argument given in the invocation. A value parameter ceases to exist upon the method return.
Reference parameters. A method parameter declared with the Var modifier is a reference parameter. A reference parameter does not create a new storage location. Instead, it provides a memory space allocated to store the variable passed as an argument when invoking the method. Thus, the value of a reference parameter is always the same as the underlying variable. Within instance structure methods (including property accessors) the Self keyword behaves exactly as a reference parameter.
Output parameters. A method parameter declared with the Out modifier is an output parameter. An output parameter does not create a new storage location. Instead, it provides a memory space allocated to store the variable passed as an argument when invoking the method. Thus, the value of an output parameter is always the same as the underlying variable. An output parameter value is initially undetermined. It must be determined before the function returns. Within instance structure methods (including property accessors) the Self keyword behaves exactly as a reference parameter.
Local variables. Local variables are declared in a local variable declaration block in functional members. A local variable comes into existence before execution of the method, in which it is declared, and ceases to exist upon the method return. The local variable's storage is released regardless of the variable lifetime.
The following variable categories are automatically initialized with default values:
Static variables.
Instance variables of class instances.
Array elements.
The default value of a variable depends on its type and is determined as follows:
If a variable has a value type, its default value corresponds with the default value for this type (0 for Integer, 0.0 for Double, False for Boolean, and so on).
If a variable has a reference type, its default value is Null.
Namespace UserApplicaton
//Variables available in entire assembly
Var
GlobalValue: double;
GlobalObject: object;
Public Class UserObject
//Static variable with default value
Shared Options: integer = 10;
//Instance variable
ObjectValues: array Of double;
Public Constructor UserObject();
Begin
ObjectValues := New Double[Options]
End Constructor UserObject;
//Parameter i is passed by value
Public Property Item[i: integer]: double
Get
Begin
//Get value of array element
Return ObjectValues[i];
End Get
Set
Begin
//Assign value to array element
ObjectValues[i] := Value;
End Set
End Property Item;
//The Summ parameter is passed by reference
Sub SummElement(Var Summ: Double);
//Local variables available only in the procedure
Var
d, d1: integer;
Begin
For Each d In ObjectValues Do
d1 := d1 + d;
End For;
Summ := d1;
End Sub;
End Class UserObject;
End Namespace;
See also: