Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Key/Value Pairs in Arrays (https://www.delphipraxis.net/115745-key-value-pairs-arrays.html)

slemke76 17. Jun 2008 11:48


Key/Value Pairs in Arrays
 
Hallo zusammen,

ich habe eine Datenbank mit einem Wert und einem Primärschlüssel:

mandant / Bezeichnung
1 / Musterfirma
98 / Firma TEST

Ich lese diese Tabelle mit "Select mandant, Bezeichnung from mandaten" aus, habe diese nun in einem AdoQuery-Objekt stehen und möchte gerne eine CheckBox füllen. Das mache ich derzeit so:

Delphi-Quellcode:
  if AdoQuery.Recordcount>0 then begin
    // Treffer !
    for i := 0 to AdoQuery.RecordCount - 1 do begin
      cb_Mandant.Items.Add(AdoQuery.Fields.Fields[1].Asstring);
      // für spätere Zuordnung ItemIndex der ComboBox - Primärindex
      AvailMand.Values[IntToStr(i)] := AdoQuery.Fields.Fields[0].AsString;
      AdoQuery.Next;
    end;
    cb_Mandant.ItemIndex:=0;
    cb_Mandant.Update;
  end;

Wenn ich nach Auswahl des Users jetzt mit dem CheckBox.ItemIndex den Primärindex (mandant) bestimmen will, mache ich das so:

Delphi-Quellcode:
  // für spätere Zuordnung ItemIndex der ComboBox - Primärindex
  pri_id := AvailMand.Values[IntToStr(cb_Mandant.ItemIndex)];
  ShowMessage(pri_id);
Klappt soweit auch alles, aber geht das auch noch "schöner" ?

lg
Sebastian

automatix 17. Jun 2008 12:37

Re: Key/Value Pairs in Arrays
 
Vielleicht so?
Delphi-Quellcode:
  while not ADOQuery1.Eof do begin
    ComboBox1.AddItem(ADOQuery1.FieldByName('BEZEICHNUNG').AsString,
                      TObject(ADOQuery1.FieldByName('MANDANT').AsInteger));
    ADOQuery1.Next;
  end;

  ...

  ShowMessage(Format('%d', [Integer(ComboBox1.Items.Objects[ComboBox1.ItemIndex])]);
Ist Mandant wirklich ein String? Dann würde ich es so implementieren (vielleicht nicht gerade einfacher, aber IMHO sauberer und verständlicher).

Delphi-Quellcode:
  TMandant = class(TObject)
  private
    FMandant: string;
    FBezeichnung: string;
  public
     constructor Create(const aMandant: string;
                       const aBezeichnung: string); reintroduce;

    property Mandant: string read FMandant;
    property Bezeichnung: string read FBezeichnung;
  end;

  ...

  while not ADOQuery1.Eof do begin
    ComboBox1.AddItem(ADOQuery1.FieldByName('BEZEICHNUNG').AsString,
                      TMandant.Create(ADOQuery1.FieldByName('MANDANT').AsString,
                                      ADOQuery1.FieldByName('BEZEICHNUNG').AsString)));
    ADOQuery1.Next;
  end;

  ...

  ShowMessage(TMandant(ComboBox1.Items.Objects[ComboBox1.ItemIndex]).Mandant);
Grüße

slemke76 17. Jun 2008 16:03

Re: Key/Value Pairs in Arrays
 
Hallo,

der Primärindex ist natürlich Integer, nicht String. Deine erste Lösung ist *perfekt*, 1000 Dank !
Ich verstehe allerdings noch nicht so ganz den Zusammenhang TObject und ComboBox. Kannst du ein oder zwei Worte dazu verlieren ?

vg
Sebastian

mquadrat 17. Jun 2008 16:07

Re: Key/Value Pairs in Arrays
 
Die Items einer ComboBox sind vom Typ TStrings. Bei diesem Typ kann man zu jedem String ein dazugehöriges Objekt dazupacken. Damit er das machen kann braucht er erstmal eine entsprechende Klasse, die die Datenfelder aufnimmt. Dann erstellt er für jeden Datensatz in der Ergebnismenge ein entsprechendes Objekt und packt das zusammen mit dem String, der in der ComboBox stehen soll in die ComboBox. Das Objekt kannst du dann wieder bequem auslesen und hast alle Informationen, die du brauchst zur Hand.

marabu 17. Jun 2008 16:54

Re: Key/Value Pairs in Arrays
 
Hallo,

bei einfachen Lookup-Funktionen (String -> Integer) ist eine eigene Klassendefinition nicht notwendig:

Delphi-Quellcode:
// generisch: PK in Feld0, DisplayString in Feld1
procedure LoadItems(s: TStrings; ds: TDataSet);
begin
  s.BeginUpdate;
  try
    s.Clear;
    ds.First;
    while not ds.Eof do
    begin
      s.AddObject(ds.Fields[1].AsString, Pointer(ds.Fields[0].AsInteger));
      ds.Next;
    end;
  finally
    s.EndUpdate;
  end;
end;
// getippt und nicht getestet.
Wenn die Klasse TMandant keinen deutlichen Mehrwert generiert, dann besser weglassen.

Freundliche Grüße

slemke76 18. Jun 2008 08:39

Re: Key/Value Pairs in Arrays
 
Hallo,

wow - genial - vielen Dank ! Schön kurz und 1a - das Problem hat man ja eingentlich fast immer bei Datenbanken und ComboBoxen zur Auswahl...

lg
Sebastian


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:17 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz