Delphi's TDictionary Klasse ist in der unit System.Generics.Collections definiert, ist relativ komplex und beruht auf einer großen Teilen restlichen RTL. Die Korrektheit der Übersetzung von Code, in dem diese Klasse verwendet wird, wird in einem Unit-Test sicher gestellt, der von einem Embarcadero Beispiel abgeleitet ist.
Generics.Collections.TDictionary">TDictionary
Die Ausgabeoperationen sind für den Test durch boolsche Ausdrücke ersetzt worden, deren Werte überprüft werden.
Die Übersetzung mit Delphi2C# bedarf dann keiner weiteren manuellen Nachbearbeung mehr und ist im folgenden wiedergegeben. Anschließend folgen dann ein paar Erläuterungen.
using static docu_tdictionary.docu_tdictionaryInterface; using static docu_tdictionary.docu_tdictionaryImplementation; using System; using static System.SystemInterface; using System.Types; using static System.Types.TypesInterface; using System.SysUtils; using static System.SysUtils.SysUtilsInterface; using System.math; using static System.math.mathInterface; using System.Generics.Collections; using static System.Generics.Collections.CollectionsInterface;
namespace docu_tdictionary {
public class docu_tdictionaryInterface {
//http://docwiki.embarcadero.com/CodeExamples/Rio/en/Generics_Collections_TDictionary_(Delphi) public static bool TestDictionary() { bool result = false; result = TestDictionary1(); return result; }
} // class docu_tdictionaryInterface
public class docu_tdictionaryImplementation {
public class TCity : TObject { public string country = string.Empty; public double Latitude; public double Longitude;
public TCity() {} public override string ClassName() {return "TCity";} public override TMetaClass ClassType(){return class_id();} public override TMetaClass ClassParent(){return class_id ();} public override TObject Create(){return new TCity();} public static new TCity SCreate() {return new TCity();} }; public const double Epsilon = 0.0000001D;
public static bool TestDictionary1() { bool result = false; TDictionaryDictionary = null; TCity city = null; TCity Value = null; string Key = string.Empty; bool bTest = false; string S = string.Empty; result = true; /* Create the dictionary. */ Dictionary = new TDictionary (); city = new TCity(); /* Add some key-value pairs to the dictionary. */ city.country = "Romania"; city.Latitude = 47.16D; city.Longitude = 27.58D; Dictionary.Add("Iasi", city); city = new TCity(); city.country = "United Kingdom"; city.Latitude = 51.5D; city.Longitude = -0.17D; Dictionary.Add("London", city); city = new TCity(); 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. */ result = result && (Dictionary.Count == 3);
// Try looking up "Iasi". if(Dictionary.TryGetValue("Iasi", ref city) == true) { result = result && (city.country == "Romania"); } else result = false;
/* 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")) result = false;
/* Test how (United Kingdom, 51.5, -0.17) is a value in the dictionary but ContainsValue returns False if passed a different instance of TCity with the same data, as different instances have different references. */ if(Dictionary.ContainsKey("London")) { Dictionary.TryGetValue("London", ref city); if((city.country == "United Kingdom") && (CompareValue(city.Latitude, 51.5D, Epsilon) == EqualsValue) && (CompareValue(city.Longitude, -0.17D, Epsilon) == EqualsValue)) result = result && (city.country == "United Kingdom"); else result = false; city = new TCity(); city.country = "United Kingdom"; city.Latitude = 51.5D; city.Longitude = -0.17D; if(Dictionary.ContainsValue(city)) result = false; city = null; } else result = false;
/* Update the coordinates to the correct ones. */ city = new TCity(); city.country = "Argentina"; city.Latitude = -34.6D; city.Longitude = -58.45D; Dictionary.AddOrSetValue("Buenos Aires", city);
/* Generate the exception "Duplicates not allowed". */ try { bTest = false; Dictionary.Add("Buenos Aires", city); } catch(System.SysUtils.Exception) { bTest = true; } result = result && (bTest == true); bTest = false; /* Display all countries. */ foreach(TCity element_0 in Dictionary.Values) { Value = element_0; if(Value.country == "Argentina") bTest = true; } result = result && (bTest == true); bTest = false; /* Iterate through all keys in the dictionary and display their coordinates. */ foreach(string element_0 in Dictionary.Keys) { Key = element_0; S = FloatToStrF(Dictionary[Key].Longitude, TFloatFormat.ffFixed, 4, 2); if(S == "-58,45") bTest = true; } result = result && (bTest == true);
/* Clear all entries in the dictionary. */ Dictionary.Clear();
/* There should be no entries at this point. */ result = result && (Dictionary.Count == 0);
/* Free the memory allocated for the dictionary. */ Dictionary = null; city = null; return result; }
} // class docu_tdictionaryImplementation
} // namespace docu_tdictionary
english
Letzte Neuigkeiten |
28.10.24
Delphi2C# 2.5: in Groß- und Kleinschreibung mit Deklarationen übereinstimmende Symbolnamen [more...] |
29.01.24
Aurora2Cpp: Delphi 7 Konverter [more...] |
Diese Homepage ist aus einfachen Texten mit [Minimal Website ]generiert.
|
Minimal Website ist mit Hilfe des TextTransformers hergestellt.
|
Der TextTransformer ist gemacht mit dem Borland
CBuilder
|