Using Delphi Xe's tdictionary

Source: Internet
Author: User

Originally used TList, the definition of a Record is stored in the TList, in order to be able to manage some objects, such as a class of n multiple instances, can be indexed, find, release, etc.

Today just saw the original has a call Tdictionary object, use it very convenient.  Pretty much like the Dictionary table we defined in db, Key, Value. And that regardless of key, Value is quite developed, allowing a variety of definitions of the class.

OK, the following official demo is very easy to understand, the methods are:

Type tcity=Class Country:String; Latitude:Double; Longitude:Double;End;Const EPSILON=0.0000001;var Dictionary: tdictionary<String, tcity>; City, Value: tcity; Key:String;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:= Tcity.Create; City.Country:=' Kingdom '; City.Latitude:=51.5; City.Longitude:=-0.17; Dictionary.Add(' London ', City); City:= Tcity.Create; City.Country:=' Argentina ';{Notice the wrong coordinates} City.Latitude:=0; City.Longitude:=0; Dictionary.Add(' Buenos Aires ', City);{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)ThenBeginWriteln(' Iasi is located in '+ City.Country+' With latitude = '+Floattostrf(City.Latitude, fffixed,4,2)+' and longitude = '+Floattostrf(City.Longitude, fffixed,4,2));EndElseWriteln(' Could not find Iasi in the dictionary ');{Remove the "Iasi" Key from Dictionary.} Dictionary.Remove(' Iasi ');{Make sure the dictionary ' s capacity are set to the number of entries.} Dictionary.TrimExcess;{Test If "Iasi" is a key in the dictionary.}If Dictionary.ContainsKey(' Iasi ')ThenWriteln(' The key ' Iasi ' is in the dictionary. ')ElseWriteln(' The key ' Iasi ' is not in the dictionary. ');{Test How (Kingdom, 51.5, -0.17) are a value in the dictionary but Containsvalue returns False if passed a differen T instance of tcity with the same data, as different instances has different references. }If Dictionary.ContainsKey(' London ')ThenBegin Dictionary.TryGetValue(' London ', City);If(City.Country=' Kingdom ')and(CompareValue(City.Latitude,51.5, EPSILON)= Equalsvalue)and(CompareValue(City.Longitude,-0.17, EPSILON)= Equalsvalue)ThenWriteln(' The value (kingdom, 51.5, -0.17) is in the dictionary. ')ElseWriteln(' Error:the value (kingdom, 51.5, -0.17) is not in the dictionary. '); City:= Tcity.Create; City.Country:=' Kingdom '; City.Latitude:=51.5; City.Longitude:=-0.17;If Dictionary.Containsvalue(City)ThenWriteln(' Error:a new instance of tcity with values (for Kingdom, 51.5, -0.17) matches an existing instance in the dictionary. ')ElseWriteln(' A new instance of tcity with values (for Kingdom, 51.5, -0.17) does not match any existing instance in the dictionary. ‘); City.Free;EndElseWriteln(' error:the key ' London ' is not in the dictionary. ');{Update the coordinates to the correct ones.} City:= Tcity.Create; 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);ExceptOn ExceptionDoWriteln(' Could not add entry. Duplicates is not allowed. ');End;{Display all countries.}Writeln(' All countries: ');For ValueIn Dictionary.ValuesDoWriteln(Value.Country);{Iterate through all keys in the dictionary and display their coordinates.}Writeln(' All cities and their coordinates: ');For KeyIn Dictionary.KeysDoBeginWriteln(Key+‘: ‘+Floattostrf(Dictionary.Items[Key].Latitude, fffixed,4,2)+‘, ‘+Floattostrf(Dictionary.Items[Key].Longitude, fffixed,4,2));End;{Clear all entries in the dictionary.} Dictionary. Clear;  {there should is no entries at this point.} writeln ( ' number of key-value pairs in the dictionary after Cleaning: ' + inttostr (Dictionary.count)  ;  {Free the Memory allocated for the dictionary. } dictionary. Free. Freereadln; End              

Using the tdictionary of Delphi Xe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.