Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#6

AW: Neue Klasse erstellen mit 2 Klassen als Attribute

  Alt 8. Dez 2011, 10:26
Man kann sich natürlich einiges an Tipparbeit sparen, wenn es viele gleiche DatenTypen gibt:
Delphi-Quellcode:
unit uMyClass;

interface

type
  TMyBase1 = class
  private
    FMyStr : string;
    FMyInt : Integer;
  public
    property MyStr : string read FMyStr write FMyStr;
    property MyInt : Integer read FMyInt write FMyInt;
  end;

  TMyBase2 = class
  private
    FMyStr : string;
    FMyInt : Integer;
  public
    property MyStr : string read FMyStr write FMyStr;
    property MyInt : Integer read FMyInt write FMyInt;
  end;

  TMyClass = class
  private
    fMyBase1 : TMyBase1;
    fMyBase2 : TMyBase2;
    function GetString( const Index : Integer ) : string;
    procedure SetString( const Index : Integer; const Value : string );
    function GetInteger( const Index : Integer ) : Integer;
    procedure SetInteger( const Index, Value : Integer );
  public
    constructor Create;
    destructor Destroy; override;

    property FirstName : string index 1 read GetString write SetString;
    property LastName : string index 2 read GetString write SetString;

    property NameReadOnly : string index 1 read GetString;
    property NameWriteOnly : string index 1 write SetString;

    property Age : Integer index 1 read GetInteger write SetInteger;
    property Value : Integer index 2 read GetInteger write SetInteger;
  end;

implementation

{ TMyClass }

constructor TMyClass.Create;
begin
  inherited;
  fMyBase1 := TMyBase1.Create;
  fMyBase2 := TMyBase2.Create;
end;

destructor TMyClass.Destroy;
begin
  fMyBase1.Free;
  fMyBase2.Free;
  inherited;
end;

function TMyClass.GetInteger( const Index : Integer ) : Integer;
begin
  case Index of
    1 :
      Result := fMyBase1.MyInt;
    2 :
      Result := fMyBase2.MyInt;
  end;
end;

function TMyClass.GetString( const Index : Integer ) : string;
begin
  case Index of
    1 :
      Result := fMyBase1.MyStr;
    2 :
      Result := fMyBase2.MyStr;
  end;
end;

procedure TMyClass.SetInteger( const Index, Value : Integer );
begin
  case Index of
    1 :
      fMyBase1.MyInt := Value;
    2 :
      fMyBase2.MyInt := Value;
  end;
end;

procedure TMyClass.SetString( const Index : Integer; const Value : string );
begin
  case Index of
    1 :
      fMyBase1.MyStr := Value;
    2 :
      fMyBase2.MyStr := Value;
  end;
end;

end.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat