AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Generische Array-Propertys

Ein Thema von zappa2 · begonnen am 23. Feb 2021 · letzter Beitrag vom 23. Feb 2021
 
TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.074 Beiträge
 
Delphi 10.4 Sydney
 
#6

AW: Generische Array-Propertys

  Alt 23. Feb 2021, 10:20
Delphi-Quellcode:
program Project2;

{$APPTYPE CONSOLE}

{$R *.res}


uses
  System.SysUtils,
  System.Math;

type
  TValue<T> = class
  protected
    a: TArray<T>;
    function GetT(const Index: Integer): T;
    procedure SetT(const Index: Integer; const Value: T);
  end;

  TAStr = class(TValue<string>)
  public
    property s0: string index 0 read GetT write SetT;
  end;

  TAInt = class(TValue<Integer>)
  public
    property i0: Integer index 0 read GetT write SetT;
  end;

  TTest = class
  private
    aStr: TAStr;
    aInt: TAInt;
  public
    constructor Create();
    destructor Destroy; override;
  public
    property MyStrings: TAStr read aStr;
    property MyIntegers: TAInt read aInt;
  end;

function TValue<T>.GetT(const Index: Integer): T;
begin
  if InRange(Index, System.Low(a), System.High(a)) then
  begin
    Result := a[Index];
  end
  else
  begin
    Result := System.Default(T);
  end;
end;

procedure TValue<T>.SetT(const Index: Integer; const Value: T);
begin
  if Index >= Length(a) then
  begin
    SetLength(a, Index + 1);
  end;
  a[Index] := Value;
end;

constructor TTest.Create;
begin
  inherited;
  aStr := TAStr.Create;
  aInt := TAInt.Create;
end;

destructor TTest.Destroy;
begin
  aStr.Free;
  aInt.Free;
  inherited;
end;

procedure TestStrings;
var
  MyStringValue: TAStr;
begin
  MyStringValue := TAStr.Create;
  MyStringValue.s0 := 'Hello World';
  Writeln(MyStringValue.s0);
  MyStringValue.Free;
end;

procedure TestIntegers;
var
  MyIntegerValue: TAInt;
begin
  MyIntegerValue := TAInt.Create;
  MyIntegerValue.i0 := 0815;
  Writeln(MyIntegerValue.i0);
  MyIntegerValue.Free;
end;

procedure TestTest;
var
  MyTest: TTest;
begin
  MyTest := TTest.Create;
  MyTest.aStr.s0 := 'Huhu';
  MyTest.aInt.i0 := 123;
  Writeln(MyTest.aStr.s0);
  Writeln(MyTest.aInt.i0);
  MyTest.Free;
end;

begin
  ReportMemoryLeaksOnShutdown := True;
  try
    TestStrings;
    TestIntegers;
    TestTest;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;

end.
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 18:23 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz