Talk:Generics Collections TDictionary (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

{This code is better than old

The code is not correct. For Which ever one TCity to TDictionary ducklings, which we are storing a memory address, because TCicy is a data type for referenciay not by value. So every time we change the value of TCity what about writing as a dememoria address, all items of TDictionary have the same memory address and therefore the same value.

To Solve this City should do: = TCity.Create; After each Add. But this could leave free TCity release when the TCity and TDictionary.

I think it's better to make it the type TCity Record. Since it's n value data type.

}

uses

 SysUtils, Generics.Collections;
 type
 TCity = Record
   Country : String;
   Latitude : Double;
   Longitude : Double;
 end;

var

 Dictionary: TDictionary<String, TCity>;
 City, Value: TCity;
 Key: String;
 temp: String;
 pair: TEnumerator<TPair<String, TCity>>;

begin

 { Create the dictionary. }
 Dictionary := TDictionary<String, TCity>.Create;
 //City := TCity.Create;
 { Add some key-value pairs to the dictionary. }
 City.Country := 'Romania';
 City.Latitude := 47.16;
 City.Longitude := 27.58;
 Dictionary.Add('Iasi', City);
 City.Country := 'United Kingdom';
 City.Latitude := 51.5;
 City.Longitude := -0.17;
 Dictionary.Add('London', City);
 City.Country := 'Argentina';
 { Notice the wrong coordinates }
 City.Latitude := 0;
 City.Longitude := 0;
 Dictionary.Add('Buenos Aires', City);
 for Value in Dictionary.Values do
   Writeln(Value.Country);
 Readln;


 { Display the current number of key-value entries. }
 writeln('Number of pairs in the dictionary: ' +
 IntToStr(Dictionary.Count));
 // Try looking up "Iasi".
 if (Dictionary.TryGetValue('Iasi', City) = True) then
 begin
   writeln(
   'Iasi is located in ' + City.Country +
   ' with latitude = ' + FloatToStrF(City.Latitude, ffFixed, 4, 2) +
   ' and longitude = ' + FloatToStrF(City.Longitude, ffFixed, 4, 2)
   );
 end
 else
   writeln('Could not find Iasi in the dictionary');
 { Remove the "Iasi" key from dictionary. }
 Dictionary.Remove('Iasi');
 { Make sure the dictionary's capacity is set to the number of entries. }
 Dictionary.TrimExcess;
 { Test if "Iasi" is a key in the dictionary. }
 if Dictionary.ContainsKey('Iasi') then
   writeln('The key "Iasi" is in the dictionary.')
 else
   writeln('The key "Iasi" is not in the dictionary.');
 { Test if (Argentina, 0, 0) is a value in the dictionary. }
 City.Country := 'United Kingdom';
 City.Latitude := 51.5;
 City.Longitude := -0.17;
 if Dictionary.ContainsValue(City) then
   writeln('The value (United Kingdom, 51.5, -0.17) is in the dictionary.')
 else
   writeln('The value (United Kingdom, 51.5, -0.17) is not in the dictionary.');
 { Update the coordinates to the correct ones. }
 City.Country := 'Argentina';
 City.Latitude := -34.6;
 City.Longitude := -58.45;
 Dictionary.AddOrSetValue('Buenos Aires', City);
 { Generate the exception "Duplicates not allowed". }
 try
   Dictionary.Add('Buenos Aires', City);
 except
   on Exception do
     { ShowMessage('Could not add entry. Duplicates are not allowed.'); }
 end;
 { Display all countries. }
 temp := 'All countries:' + #13;
 for Value in Dictionary.Values do
   temp := temp + #13 + Value.Country;
 writeln(temp);
 { Iterate through all keys in the dictionary and display their coordinates. }
 temp := 'All cities and their coordinates:' + #13;
 for Key in Dictionary.Keys do
   temp := temp + #13 + Key + ': ' + #13 +
   FloatToStrF(Dictionary.Items[Key].Latitude, ffFixed, 4, 2) + ', ' +
   FloatToStrF(Dictionary.Items[Key].Longitude, ffFixed, 4, 2) + #13;
 writeln(temp);
 { Clear all entries in the dictionary. }
 Dictionary.Clear;
 { There should be no entries at this point. }
 writeln('Number of key-value pairs in the dictionary: ' + IntToStr(Dictionary.Count));
 { Free the memory allocated for the dictionary. }
 Dictionary.Free;
 readln;
 try
   { TODO -oUser -cConsole Main : Insert code here }
 except
   on E: Exception do
     Writeln(E.ClassName, ': ', E.Message);
 end;

end.


  • The current code example has still one flaw: it simply catches all exception classes when wrapping method calls. That's bad style. It should only catch the very specific exceptions one can expect when trying to fetch items which are not in the list or when adding duplicate key items etc. But I guess part of it stems from the fact that the overview page of TDictionary in the table only talks about exceptions itsself without naming the specific exception classes.--Markus.humm (talk) 03:14, 9 June 2014 (PDT)