Home  |  Delphi .net Home  |  System.Object  |  ToString Method
ToString  
Method  
Converts the Object contents into a string representation
Object Class
System NameSpace
CF1.  Function ToString ( ) : String;
CF : Methods with this mark are Compact Framework Compatible
Description
Converts the current salient Object contents into a formatted string.
 
This is a very important method - allowing all classes to show themselves as it were.
 
For the base Object type, there are no real contents, so the class name itself is used as the string. For your classes, you should define a meaningful ToString implementation. For example, the Exception class displays the trace of exceptions it contains.
Microsoft MSDN Links
System
System.Object
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

var
  MyObj : System.Object;
  MyStr : String;

begin
  // Instantiate the object
  MyObj := System.Object.Create;

  Console.WriteLine('MyObj.ToString = ' + MyObj.ToString);

  // Define the string contents (strings aer automatically created)
  MyStr := 'Hello World';

  Console.WriteLine('MyStr.ToString = ' + MyStr.ToString);

  Console.ReadLine;
end.
Show full unit code
  MyObj.ToString = System.Object
  MyStr.ToString = Hello World
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author