To create a new Class Definition file, from the VB menu, select Project / Add Class Module.
Help Topics
| Only one Construction | Class_Initialize | |
| Only one Destructor | Class_Terminate |
Private HiddenVariable As Integer
Public Property Get PropertyName() As Integer
PropertyName = HiddenVariable
End Property
Public Property Let PropertyName(ByVal x As Integer)
If x > 5 and x < 8 then
HiddenVariable = x
End If
End Property
To set a default property,
use Tools / Procedure Attributes... / Advanced>>
and set Procedure ID to (Default).
Be very sure that only one method or property is defined
as the default since the compiler does not check for multiples.
You cannot implement true static class data which is available to all objects of the same class. Instead, you have to fake it by defining a public variable in another module (.bas file) and use class methods to access it.
Instantiating an Object
Set x = Nothing
TField = class
private
X, Y, Len: Integer;
FName: string;
FStr: string;
public
constructor Copy(F: TField);
constructor Create(FX, FY, FLen: Integer; FName: string);
destructor destroy; override;
procedure Display; virtual;
procedure Edit ; dynamic;
protected
function GetStr: string; virtual;
function SetStr(S: string): Boolean; virtual;
procedure Something; virtual; abstract;
private
procedure DisplayStr(X, Y: Integer; S: string);
public
property Str: String read GetStr write SetStr; // Methods must be defined
// before they are used
// in a property
published
property Cnt: Integer read GetCnt write SetCnt default 30;
end;
Properties that are defined as published are displayed
in the Object Inspector at design time and can be modified
by the developer.
In a property, the read directive must come before the write directive.
The default directive is used with numbers, if the value in the object inspector is different from the default value, then the value is saved in the *.dfm file. It does not actually set the value of the property. You must assign all data fields to their default values in the component's constructor(s). String properties can not have a default directive, and all strings <>'' are stored in the *.dfm file. ref
type
PTRectangle = ^TRectangle; // Pointers to a type can be defined before the type
TRectangle = class(TFigure)
procedure Draw; override;
:
end;
TEllipse = class(TFigure)
procedure Draw; override;
:
end;
The following section of code illustrates the effect of calling
a virtual method through a class type variable whose actual type
varies at run-time.
var
Figure: TFigure; // This does not allocate any memory
begin
Figure := TRectangle.Create; // This allocates memory
Figure.Draw; { Invokes TRectangle.Draw }
Figure.Destroy;
Figure := TEllipse.Create;
Figure.Draw; { Invokes TEllipse.Draw }
Figure.Destroy;
end;
The allowed subroutine (method) types are
| constructor | Called when the object is crearted |
| destructor | Called when the object goes out of scope |
| function | Returns a value |
| procedure | Does not return a value |
| property | Redirects reads and writes to properties to method calls |
Abstract virtual methods do not have any code defined. These must be overridden before they are called.
inherited - Calling inherited with an abstract virtual method will generate a run time exception.
type
TMyType = record
a: string;
b: string;
end;
var
MyArray : Tlist;
MyType : Tmytype;
PMyType : ^Tmytype;
implementation
procedure TForm1.FormCreate(Sender: TObject);
begin
MyArray:= tlist.create;
new (Pmytype); // This allocates memory and initializes the pointer
MyArray.add(PMyType);
end;
When designing components,
use this to keep methods from running at design time.
if csDesigning in ComponentState then begin Exit; end;
Multiple inheritance is partially supported using interfaces - special classes which contain only abstract methods.
Methods (function and procedures) can be defined as class methods which can be accessed without specifying an instance (object).
class function SetStr(S: string): Boolean; virtual; class procedure Something; virtual; tMyType.something; // This will call the procedure
C++ Builder
struct SomeName{
char str1[20];
char str2[4];
short x;
short y;
}; // The semicolon is required
// Instances can be created in the definition by placing
// variable names before the final semicolon
SomeName var1; // Declare a variable of that type
SomeName varArray[4]; // An array of structures
strcpy(var1.str1, "A string");
var1.x = 35;
varArray[2].y = 66 ;
A class is just like a structure except that the default access is private.
Allows multiple inheritence.
public class NewName extends ExistingClass{
public integer someValue = 5;
static int someCounter = 0; // defines a "Class Variable" who's value
// is available in all instances of this class
public void method1() {
this.someValue = 8 ; // Variable in this class
super.anotherValue = 3 ; // Variable in parent class
}
public NewName (int a) { // Constructors have the same name as the class
this(null, a) ; // "this" calls another constructor in this class
// (see JTextField.java for examples)
}
public NewName (int a, int b) { // There can be many constructors
// as long as each one has different parameters
}
}
NewName ClassInstance = new NewName() ; // allocate some memory
NewName.someCounter++ ; // "Class Variables" are referenced using the class name
ClassInstance.someValue = 4 ;
Java 1.1 lacks property definitions.
Instead, you simply make the attribute private and
define 2 public methods -
one to read and another to write.
By convention, most development systems treat
getName and setName
are interpreted as the parameter name.
static is used to define Class Variables and Methods. These allocate space once regardless of how many instances are allocated. These can be referenced using either the class name or the name of a variable that references a specific instance. You can use Class Methods to read and write Class Variables.
Constructors do not return a value and
have the same name of the class.
Classes frequently have multiple constructors.
One of the advertised advantages of java is that multiple inheritance is NOT supported. (A dubious advantage at best.) Then, in standard doublespeak, you are told that multiple inheritance IS supported when interface classes are inherited.
BTW, Java does not support events, it uses interfaces instead. (Same idea, different name.)