TStringList

Top  Previous  Next

Unit Tests > TStringList

A frequently used Delphi class is TStringList. The translation of the defining code in System.Classes needs little manual post-processing. However there are some streaming operations namely in the base class TPersitent, which aren't implemented. But the example from

 

http://www.delphibasics.co.uk/RTL.asp?Name=tstringlist

 

 

compiles and works without manual post-processing. (Again, the original code has been slightly modified for the testing purpose.)

 

 

using System.Classes;

using static System.Classes.ClassesInterface;

using static dbsc_tstringlist.dbsc_tstringlistInterface;

using static dbsc_tstringlist.dbsc_tstringlistImplementation;

using System;

using static System.SystemInterface;

 

 

namespace dbsc_tstringlist

{

 

public class dbsc_tstringlistInterface

{

 

//http://www.delphibasics.co.uk/RTL.asp?Name=TStringList

public static bool TStringListTest()

{

  bool result = false;

  result = true;

  result = result && TStringListTest1();

  result = result && TStringListTest2();

  result = result && TStringListTest3();

  return result;

}

 

} // class dbsc_tstringlistInterface

 

 

 

public class dbsc_tstringlistImplementation

{

 

public static bool TStringListTest1()

{

  bool result = false;

  TStringList animals = null;            // Define our string list variable

  int i = 0;

  result = true;

  // Define a string list object, and point our variable at it

  animals = new TStringList();

 

  // Now add some names to our list

  animals.Add("Cat");

  animals.Add("Mouse");

  animals.Add("Giraffe");

 

  // Now display these animals

  // for i := 0 to animals.Count-1 do

  //  ShowMessage(animals[i]);  // animals[i] equates to animals.Strings[i]

  result = result && (animals[0] == "Cat");

  result = result && (animals[1] == "Mouse");

  result = result && (animals[2] == "Giraffe");

 

  // Free up the list object

  animals = null;

  return result;

}

 

public static bool TStringListTest2()

{

  bool result = false;

  TStringList Names = null;            // Define our string list variable

  string ageStr = string.Empty;

  int i = 0;

  result = true;

  // Define a string list object, and point our variable at it

  Names = new TStringList();

 

  // Now add some names to our list

  Names.CommaText = "Neil=45, Brian=63, Jim=22";

 

  // And now find Brian's age

  ageStr = Names.ReadPropertyValues("Brian");

 

  // Display this value

  // ShowMessage('Brians age = '+ageStr);

  result = result && (ageStr == "63");

 

  // Now display all name and age pair values

  for(i = 0; i <= Names.Count - 1; i++)

  {

     //ShowMessage(names.Names[i]+' is '+names.ValueFromIndex[i]);

    if(i == 0)

      result = result && (new PChar(Names.ReadPropertyNames(i)).ToString() == "Neil") && (new PChar(Names.ReadPropertyValueFromIndex(i)).ToString() == "45");

    if(i == 1)

      result = result && (new PChar(Names.ReadPropertyNames(i)).ToString() == "Brian") && (new PChar(Names.ReadPropertyValueFromIndex(i)).ToString() == "63");

    if(i == 2)

      result = result && (new PChar(Names.ReadPropertyNames(i)).ToString() == "Jim") && (new PChar(Names.ReadPropertyValueFromIndex(i)).ToString() == "22");

  }

 

  // Free up the list object

  Names = null;

  return result;

}

 

public static bool TStringListTest3()

{

  bool result = false;

  TStringList cars = null;            // Define our string list variable

  int i = 0;

  result = true;

  // Define a string list object, and point our variable at it

  cars = new TStringList();

 

  // Now add some cars to our list - using the DelimitedText property

  // with overriden control variables

  cars.Delimiter = ' ';        // Each list item will be blank separated

  cars.QuoteChar = '|';        // And each item will be quoted with |'s

  cars.DelimitedText = "|Honda Jazz| |Ford Mondeo| |Jaguar \"E-type\"|";

 

  // Now display these cars

//  for i := 0 to cars.Count-1 do

//    ShowMessage(cars[i]);       // cars[i] equates to cars.Strings[i]

  result = result && (cars[0] == "Honda Jazz");

  result = result && (cars[1] == "Ford Mondeo");

  result = result && (cars[2] == "Jaguar \"E-type\"");

 

  // Free up the list object

  cars = null;

  return result;

}

 

 

} // class dbsc_tstringlistImplementation

 

}  // namespace dbsc_tstringlist

 

 

 

 

 

 

 

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content