Einzelnen Beitrag anzeigen

EdAdvokat

Registriert seit: 1. Mai 2016
Ort: Berlin
415 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#18

AW: Generics richtig verstanden?

  Alt 12. Mai 2018, 15:45
Habe postleitzahlen frei gegeben. Hoffentlich richtig.
Delphi-Quellcode:
unit uMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Contnrs, System.Generics.Collections,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    btnKey: TButton;
    edtKey: TEdit;
    btnInhalt: TButton;
    edtInhalt: TEdit;
    lblKey: TLabel;
    lblValue: TLabel;
    btnpostleitzahl: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btnKeyClick(Sender: TObject);
    procedure btnInhaltClick(Sender: TObject);
    procedure btnpostleitzahlClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

type
  TPair<TKey,TValue> = class // TKey. TValue type parameters
     FKey: TKey;
     FValue: TValue;
     function GetValue: TValue;
     function GetKey: TKey;
     procedure setValue(V:TValue);
     procedure setKey(K:TKey);
     constructor create;
     destructor Destroy;override;
     property Key:TKey read FKey write FKey;
     property Value:TValue read FValue write FValue;
   end;

type
   TSIPair = TPair<String,Integer>; // declares instantiated type
   TSSPair = TPair<String,String>; // declares with other data types
   TISPair = TPair<Integer,String>;
   TIIPair = TPair<Integer,Integer>;

type
    TCustomer = Record
    postCode : string[8];
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TPair<TKey, TValue> }

constructor TPair<TKey, TValue>.create;
begin
  inherited create;
end;

destructor TPair<TKey, TValue>.Destroy;
begin

  inherited;
end;

function TPair<TKey, TValue>.GetKey: TKey;
begin
  Result:=FKey
end;

function TPair<TKey, TValue>.GetValue: TValue;
begin
  Result := FValue;
end;

procedure TPair<TKey, TValue>.setKey(K: TKey);
begin
  FKey:=K;
end;

procedure TPair<TKey, TValue>.setValue(V: TValue);
begin
  FValue:=V;
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
  close;
end;

procedure TForm1.btnKeyClick(Sender: TObject);
var Key: TPair<string,integer>;
    TSS: TSSPair; //typ parameter zuweisen bsp. string oder integer
begin //string zugewiesen
  Key:=TPair<string,integer>.create;
  try
    Key.Key:=edtKey.Text;
    lblKey.caption:=Key.Key;
    TSS:=TSsPair.create; //string zugewiesen
    TSS.Value:='Dies ist ein Test';
    ShowMessage(TSS.Value);
    //ShowMessage(Key.Key);
  finally
    freeandNil(Key);
    FreeAndNil(TSS);
  end;
end;

procedure TForm1.btnInhaltClick(Sender: TObject);
var Inhalt: TPair<string,integer>; //integer zugewiesen
    TSI:TSIPair;
begin
  Inhalt:=TPair<string,integer>.create;
  try
    Inhalt.Value:=strtoint(edtInhalt.text);
    lblValue.caption:=inttostr(Inhalt.Value);
    TSI:=TSIPair.create; //integer zugewiesen
    TSI.Value:=523;
    ShowMessage(inttostr(TSI.Value));
    //ShowMessage(inttostr(Inhalt.Value));
  finally
    FreeAndNil(Inhalt);
    FreeAndNil(TSI);
  end;

end;

{ TDictionary<TKey, TValue> }

procedure postleitzahlen;
  type
   TPostleitzahl = string; //vorher integer aber führende Null!!!
   TOrtsName = String;
var
   postleitzahlen: TDictionary<TPostleitzahl, TOrtsName>;
begin
  try
   postleitzahlen := TDictionary<TPostleitzahl, TOrtsName>.Create();
   postleitzahlen.Add('53639', 'Königswinter');
   postleitzahlen.Add('53117', 'Bonn1');
   postleitzahlen.Add('53111', 'Bonn');

   ShowMessage('Die Postleitzahl für 53639 ist');
   ShowMessage( postleitzahlen['53639']);
  finally
    FreeAndNil(Postleitzahlen);
  end;
end;

procedure TForm1.btnpostleitzahlClick(Sender: TObject);
begin
  postleitzahlen;
end;
end.
Norbert
  Mit Zitat antworten Zitat