texttransformer.jpg

Delphi's class TDictionary is defined in the unit System.Generics.Collections. It is relatively complex and it uses much parts of the RTL. The correctness of the translation of code in which this class is used is guaranteed by a unit test which is derived from an Embarcadero example.

Generics.Collections.TDictionary">Generics.Collections.TDictionary

As for all test cases, the output operations have been replaced by boolean expressions which are checked at the execution of the test.
The translation with Delphi2C# doesn't require any further manual post-processing and is shown below.


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;
  TDictionary Dictionary = 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

   deutsch Deutsch

 

 
Latest News
10/28/24
Delphi2C# 2.5: Symbol names matching declaration case [more...]

01/29/24
Aurora2Cpp: Delphi 7 translator [more...]



"So, all in all we are very pleased with Delphi2C#."


Henk van der Meer
Innomeer 02/02/2023

 
This website is generated from plain text with [Minimal Website ]

Minimal Website
Minimal Website is made with TextTransformer

TextTransformer
TextTransformer is made with Borland CBuilder

  borland