RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TObject::ClassName Method

Returns a string indicating the type of the object instance (as opposed to the type of the variable passed as an argument).

Pascal
class function ClassName: string;
C++
string ClassName();

Use System::TObject::ClassName to obtain the class name from an object instance or class reference. This is useful for differentiating object instances that are assigned to a variable that has the type of an ancestor class.

Note: In C++ code, call System::TObject::ClassName as a method to obtain an object's class name. Use the global static function to obtain the class name from a metaclass object.
 

C++ Examples: 

 

/*
This example shows how to obtain the ancestry of a component
using the ClassType and ClassParent properties. It uses a
button and a list box on a form. When the user clicks the
button, the name of the button’s class and the names of its
parent classes are added to the list box.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TClass ClassRef;
  ListBox1->Clear();
  ClassRef = Sender->ClassType();
  while (ClassRef != NULL)
  {
    ListBox1->Items->Add(ClassRef->ClassName());
    ClassRef = ClassRef->ClassParent();
  };
}
/*
The list box contains the following strings after the user clicks the button:
    TButton
    TButtonControl
    TWinControl or TWinControl
    TControl
    TComponent
    TPersistent
    TObject 
*/
/*
To run this example, add the example code to a new project.
The example code dynamically creates a TCoolbar and three
TCoolBand objects populated with a windowed control in each
TCoolBand.
*/
void AddBands(TCoolBar *CoolBar, TList *Objects)
{
  TControl *pCurrent;
  for (int i = 0; i < Objects->Count; i++)
  {
    pCurrent = reinterpret_cast<TControl *>(Objects->Items[i]);
    // only add windowed controls to the coolbar
    if (dynamic_cast<TWinControl *>(pCurrent) != NULL)
    {
      // Placing the control onto the CoolBar
      // causes the TCoolBar object to create a TCoolBand 
      // and place the control within the band.
      pCurrent->Parent = CoolBar;   // This statement increments CoolBar->Bands->Count.
      // Get the reference of the TCoolBand just created and assign text.
      String S = pCurrent->ClassName();
      CoolBar->Bands->Items[CoolBar->Bands->Count - 1]->Text = S;
    }
  }
}

#include <memory>       //for STL auto_ptr class

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  TCoolBar *CoolBar = new TCoolBar(this); // The Ownder (Form1) will clean this up.
  CoolBar->Parent = this;
  CoolBar->Align = alTop;
  std::auto_ptr<TList> List(new TList);
  try
  {
    TControl *Control;
    Control = new TButton(CoolBar); // The Ownder (CoolBar) will clean this up.
    List->Add(Control);
    Control = new TCheckBox(CoolBar); // The Ownder (CoolBar) will clean this up.
    List->Add(Control);
    Control = new TEdit(CoolBar); // The Ownder (CoolBar) will clean this up.
    List->Add(Control);
    AddBands(CoolBar, List.get());
  }
  catch (...)
  {
    ShowMessage("Some objects could not be added to Coolband");
  }
}

 

Delphi Examples: 

{
This example shows how to obtain the ancestry of a component
using the ClassType and ClassParent properties. It uses a
button and a list box on a form. When the user clicks the
button, the name of the button’s class and the names of its
parent classes are added to the list box.
} 
procedure TForm1.Button1Click(Sender: TObject);
var
  ClassRef: TClass;
begin
  ListBox1.Clear;
  ClassRef := Sender.ClassType;
  while ClassRef <> nil do
  begin
    ListBox1.Items.Add(ClassRef.ClassName);
    ClassRef := ClassRef.ClassParent;
  end;
end;
{
The list box contains the following strings after the user clicks the button:
    TButton
    TButtonControl
    TWinControl or TWinControl
    TControl
    TComponent
    TPersistent
    TObject 
}

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!