Delphi Basics Home  |  Delphi Basics .NET Home  |  System.Object  |  GetType Method
GetType  
Method  
Get the class type of the current object
Object Class
System NameSpace
CF1.  Function GetType : Type;
CF : Methods with this mark are Compact Framework Compatible
Description
The GetType method returns the class type of the current object instance.
Notes
If a variable is assigned an object reference that is inherited from the variable type, then GetType shows the assigned class, not the original declaration class.
Microsoft MSDN Links
System
System.Object
System.Object.GetType
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

type
  Level1 = Class(System.Object);
  Level2 = Class(Level1);

var
  obj0 : System.Object;
  obj1 : Level1;
  obj2 : Level2;
  obj3 : System.Object;

begin
  // Create some objects and show their types
  obj0 := System.Object.Create;
  obj1 := Level1.Create;
  obj2 := Level2.Create;
  obj3 := obj2;

  Console.WriteLine('obj0 type = ' + obj0.GetType.ToString);
  Console.WriteLine('obj1 type = ' + obj1.GetType.ToString);
  Console.WriteLine('obj2 type = ' + obj2.GetType.ToString);
  Console.WriteLine('obj3 type = ' + obj3.GetType.ToString);

  Console.ReadLine;
end.
   obj0 type = System.Object
   obj1 type = Project1.Level1
   obj2 type = Project1.Level2
   obj3 type = Project1.Level2
 
© CodeGear 2006 - 2007. All rights reserved.