Einzelnen Beitrag anzeigen

bernhard_LA

Registriert seit: 8. Jun 2009
Ort: Bayern
1.138 Beiträge
 
Delphi 11 Alexandria
 
#1

Duplicate Values in TDictionary

  Alt 22. Apr 2015, 09:32
hier ein kurzer Testcode

Delphi-Quellcode:
unit Unit1xxx;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, generics.collections,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TElement = class
    name: string;
    id: integer;
    value: string;
    constructor create(aname: string; aid: integer);
  end;

  TFrequencyList = class
    data: array of integer;
    constructor create(n: integer);
  end;

type
  TForm1 = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
  dict: TDictionary<TElement, TFrequencyList>;
  Myelement: TElement;
  FCount: TFrequencyList;
begin

  dict := TDictionary<TElement, TFrequencyList>.create;

  /// code version #1
  Myelement := TElement.create('maier', 0);
  FCount := TFrequencyList.create(6);
  FCount.data[1] := 99;
  dict.Add(Myelement, FCount);

  /// code version #2
  dict.Add(Myelement.create('mueller', 1), FCount.create(5));

  // Find some key
  Myelement := TElement.create('maier', 99);
  if dict.TryGetValue(Myelement, FCount) then
  begin
    ShowMessage('Found it!: ' + IntToStr(FCount.data[1]));
  end;

  Myelement := TElement.create('maier', 0);
  if dict.TryGetValue(Myelement, FCount) then
  begin
    ShowMessage('Found it!: ' + IntToStr(FCount.data[1]));
  end;

end;

{ TFrequencyList }

constructor TFrequencyList.create(n: integer);
begin
  setlength(data, n);
end;

{ TElement }

constructor TElement.create(aname: string; aid: integer);
begin
  self.name := aname;
  self.id := aid;
  self.value := 'dummy data';
end;

end.

Delphi behauptet beim Einfügen des zweiten Datensatzes "Duplicate nicht zulässig" , ich sehe den Fehler nicht ....
  Mit Zitat antworten Zitat