unit Unit25;
interface
uses
System.Classes;
type
THouse =
packed record
FHeight: double;
FLength: double;
FName:
string;
FID: integer;
end;
THouses =
array of THouse;
TOnAddEvent =
procedure(
const Sender: TObject;
const AHouse: THouse)
of object;
THouseClass =
class(TObject)
strict private
FHouses: THouses;
// interne verwaltung
FIndex: Integer;
// interne verwaltung
FCount: Integer;
// interne verwaltung
FOnAddEvent: TOnAddEvent;
// interne verwaltung
private
procedure SetHouse(
const AHouse: THouse);
// schreibe das index element
function GetHouse: THouse;
// hole das index element hervor
procedure SetIndex(
const AIndex: Integer);
// versuche gewünschten index zu setzen
public
constructor Create;
// um die interne verwaltung zu initialisieren
procedure Add(
const AHeight, ALength: Double;
const AName:
string;
const AID: Integer);
overload;
// haupt methode zum simplen adden
procedure Add(
const AHouse: THouse);
overload;
// neben methode die intern die haupt methode aufruft
procedure Remove;
// löscht aktuellen index vom array
public
property Houses: THouses
read FHouses
write FHouses;
// direkter zugriff aufs interne array (ich würde es entfernen)
property House: THouse
read GetHouse
write SetHouse;
// zugriff auf ein element basierend vom index
property Index: Integer
read FIndex
write SetIndex;
// steuerung für einzel array zugriffe
property Count: Integer
read FCount;
// sagt wieviel elemente wir haben
property OnAddEvent: TOnAddEvent
read FOnAddEvent
write FOnAddEvent;
// feuert ein Event
end;
implementation
constructor THouseClass.Create;
begin
inherited Create;
FOnAddEvent :=
nil;
FIndex := -1;
FCount := 0;
end;
procedure THouseClass.SetHouse(
const AHouse: THouse);
begin
if ((FIndex > -1)
and (FIndex < FCount))
then
FHouses[FIndex] := AHouse;
end;
function THouseClass.GetHouse: THouse;
begin
if ((FIndex > -1)
and (FIndex < FCount))
then
Result := FHouses[FIndex];
end;
procedure THouseClass.SetIndex(
const AIndex: Integer);
begin
if ((AIndex > -1)
and (AIndex < FCount))
then
FIndex := AIndex;
end;
procedure THouseClass.Add(
const AHeight, ALength: Double;
const AName:
string;
const AID: Integer);
var
i: Integer;
begin
i := Length(FHouses);
SetLength(FHouses, i + 1);
FHouses[i].FHeight := AHeight;
FHouses[i].FLength := ALength;
FHouses[i].FName := AName;
FHouses[i].FID := AID;
FIndex := i;
FCount := Length(FHouses);
if Assigned(FOnAddEvent)
then
FOnAddEvent(Self, FHouses[i]);
end;
procedure THouseClass.Add(
const AHouse: THouse);
begin
Self.Add(AHouse.FHeight, AHouse.FLength, AHouse.FName, AHouse.FID);
end;
procedure THouseClass.Remove;
begin
if ((FIndex > -1)
and (FIndex < FCount))
then
begin
Delete(FHouses, FIndex, 1);
FCount := Length(FHouses);
if FIndex >= FCount
then
FIndex := Pred(FIndex);
end;
end;
end.