Is it possible to store multiple items in TDictionary? I want a simple solution for next
My table looks like:
I want to store all the fields from the table and after search items in it.. Is TDictionary is capable for it? I have a Paradox Table, Query is a simple TQuery
I did a small example with 2 field
var
stSearch: string;
vPDX: TDictionary<String, TCity>;
variable: string;
stSearch := '4';
vPDX := TDictionary<String, String>.Create; qry_TMP.DatabaseName := 'C:\S_DATABASE';
qry_TMP.SQL.Text := 'select * from SAMPLE_TABLE';
qry_TMP.Open;
while not qry_TMP.Eof do
begin
vPDX.AddOrSetValue(qry_TMP.FieldByName('LOCATION_ID').AsString, qry_TMP.FieldByName('Location').AsString);
qry_TMP.Next;
end;
if vPDX.TryGetValue(stSearch, variable) then
showmessage(variable);
This code Works but I need another field too (ZIP_CODE).
I tried it with Class but I got only the last item from the Table.
TSampleClass = class
ZIP_CODe: String;
Location: String;
end;
var
SampleClass, Value: TSampleClass;
vPDX := TDictionary<String, TSampleClass>.Create;
begin
stSearch := '4';
SampleClass := TSampleClass.Create;
vPDX := TDictionary<String, TSampleClass>.Create;
qry_TMP.DatabaseName := 'C:\S_DATABASE';
qry_TMP.SQL.Text := 'select * from SAMPLE_TABLE';
qry_TMP.Open;
while not qry_TMP.Eof do
begin
vPDX.AddOrSetValue(qry_TMP.FieldByName('LOCATION_ID').AsString, SampleClass);
SampleClass.ZIP_CODE := Qry_TMP.FieldByName('ZIP_CODE').AsString;
SampleClass.City := Qry_TMP.FieldByName('City').AsString;
qry_TMP.Next;
end;
if vPDX.TryGetValue(stSearch, SampleClass) then
showmessage(SampleClass.ZIP_CODE + SampleClass.City);
Anyone can help what will be the problem? I want Germany and the 5000 ZIP_CODE. Iwant to learn how to use TDictionary. Thanks for the helps!
You're calling tSampleClass.Create
only once.
Move it inside the WHILE
statement so that a new instance is created for each record.