Autor Beitrag
NOS1971
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 193

Windows 8.1 PRO 64 Bit
Delphi XE7 Professional
BeitragVerfasst: Mi 05.02.14 17:21 
Hallo zusammen,

wie greife ich denn auf alle Items eines TObjectDictionary zu wie in einer for schleife und mit einem index ... oder geht da soetwas nicht ?


Grüßle,
Andreas
jfheins
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 918
Erhaltene Danke: 158

Win 10
VS 2013, VS2015
BeitragVerfasst: Mi 05.02.14 17:47 
da es von TEnumerable abgleitet ist, müsste es ja mit der for in Schleife gehen.

Index fällt flach, sowas hat ein Dictionary ja nicht. Aber wenn du alle Schlüssel kennst, kannst du auch diese hernehmen.
bummi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1248
Erhaltene Danke: 187

XP - Server 2008R2
D2 - Delphi XE
BeitragVerfasst: Mi 05.02.14 17:52 
Du iteriserts über Keys oder Values.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
uses Generics.Collections;
type
TMyClass=Class
  ID:Integer;
  Value:String;
  //.....
  Constructor Create(aID:Integer;aValue:String);
End;

 TMyDictionary = TObjectDictionary<Integer, TMyClass>;


constructor TMyClass.Create(aID:Integer; aValue: String);
begin
 inherited Create;
 ID := aID;
 Value := aValue;
end;


procedure TForm3.Button1Click(Sender: TObject);
var
 i:Integer;
 obj:TMyClass;
 md:TMyDictionary;
 key:Integer;
 lob:TMyClass;
begin
  ReportMemoryLeaksOnShutDown := true;
  md:=TMyDictionary.Create([doOwnsValues]);

  for I := 0 to 10 do
      begin
        obj := TMyClass.Create(i , IntToStr(i));
        md.Add(i,obj);
      end;
  for key in md.Keys do
    Listbox1.Items.Add(md.Items[key].Value);

  Listbox1.Items.Add('__');


  for lob in md.Values do
    Listbox1.Items.Add(lob.Value);
  md.Free;
end;


Wenn Du es sortiert benötigst erzeugst Du eine sortierte Liste mit den Keys und greifst über md.Items[List[i]] auf die Items zu.

_________________
Das Problem liegt üblicherweise zwischen den Ohren H₂♂
DRY DRY KISS
NOS1971 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 193

Windows 8.1 PRO 64 Bit
Delphi XE7 Professional
BeitragVerfasst: Mi 05.02.14 18:45 
Das Problem liegt dann daran das mein TObjectDictionary schon so aussieht

TMyDictionary = TObjectDictionary<String, TMyClass>;

Kann man das nachträglich nicht umwandeln sodass ein zugriff auf alle items möglich ist ...

eventuell gibt es ja eine möglichkeit auf alle items zuzugreifen ... egal in welcher reihenfolge aber auf alle halt ...
bummi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1248
Erhaltene Danke: 187

XP - Server 2008R2
D2 - Delphi XE
BeitragVerfasst: Mi 05.02.14 19:52 
Der einzige Unterschied scheint mir zu sein dass Du String statt Integer verwendet hast,
wenn Du
ausblenden Delphi-Quelltext
1:
key:String;					

definierst kannst Du genauso zugreifen
ausblenden Delphi-Quelltext
1:
2:
3:
for key in md.Keys do
   //irgendwas mit 
    md.Items[key].Value

_________________
Das Problem liegt üblicherweise zwischen den Ohren H₂♂
DRY DRY KISS

Für diesen Beitrag haben gedankt: NOS1971
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19273
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 05.02.14 20:23 
Und zusätzlich gibt es noch ToArray bzw. Keys.ToArray, damit bekommst du ein Array zurück. Wenn du auf dem arbeitest, kannst du auch das Dictionary in der Schleife modifizieren. (Denn wenn du direkt über das Dictionary iterierst, kannst du dort keine Elemente hinzufügen oder löschen.)

Für diesen Beitrag haben gedankt: NOS1971