AGB  ·  Datenschutz  ·  Impressum  







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

Generics richtig verstanden?

Ein Thema von EdAdvokat · begonnen am 11. Mai 2018 · letzter Beitrag vom 13. Mai 2018
 
EdAdvokat

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

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
 


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 16:16 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