Field is a class member that represents a variable associated with an object or class.
When a field declaration includes the Shared modifier, the fields introduced by the declaration are static fields. When no Shared modifier is present, the fields introduced by the declaration are instance fields. Static and instance fields are two different types of variables.
A static field is not part of a specific instance; instead, it identifies exactly one storage location. No matter how many instances of a class are created, there is only one copy of a static field for the associated application domain.
An instance field belongs to a specific instance. Every instance of a class contains a separate set of all the instance fields of that class.
Class SharedField
//Static field
Shared Public a: string;
//Instance field
Public b: string;
End Class;
Sub Test();
Var
Obj: SharedField = New SharedField();
Begin
SharedField.a := "Class";
Obj.b := "Object";
End Sub;
If a field declaration includes the ReadOnly modifier, this field is named a read-only field. Direct assignments to read-only fields can only occur as part of that field declaration or in a class constructor (instance or static constructor depending on the field type).
An attempt to assign a value to a read-only field or pass it as a reference or output parameter in any other context results in a compilation error.
Class ReadonlyField
Readonly Public Ver: string;
Readonly Public Id: string;
Public Constructor ReadonlyField();
Begin
Ver := "1.0.0";
Id := "ReadonlyField";
End Constructor;
End Class;
Sub Test();
Var
Obj: ReadonlyField = New ReadonlyField();
s: string;
Begin
s := Obj.Id + " " + Obj.Ver;
End Sub;
See also: