Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#8

AW: Klasse mit Oberfläche verheiraten

  Alt 26. Jul 2022, 16:10
Delphi-Quellcode:
type
  THouse = packed record
    FHeight: double;
    FLength: double;
    FName: string;
    FID: integer;
  emd;
  THouses = array of THouse;

 THouseClass = class
   strict private
     FHouses: THouses; // interne verwaltung
     FIndex: Integer; // interne verwaltung
     FCount: Integer; // 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 FIndex und FCount 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
   end;

implementation

constructor THouseClass.Create;
begin
  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);
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;
Wäre es so nicht sinnvoller?
Gruß vom KodeZwerg

Geändert von KodeZwerg (26. Jul 2022 um 17:24 Uhr)
  Mit Zitat antworten Zitat