Creating a Dynamic Array of Objects

All (or almost all) VCL objects, when dynamically created, are declared using the new operator. Sometimes you will need to have a group of objects declared at once; this is similar to creating a list of objects. And this highlights the process of creating an array of objects. Since you cannot declare a regular object without using a pointer to its class, you should find another solution.

The first alternative would be to create each object variable using its own instance. Here is an example:

TStringList *Spring1 = new TStringList;
TStringList *Spring2 = new TStringList;

Spring1->Add("Paul");
Spring1->Add("Alain");
Spring1->Add("Justin");
Spring1->Add("Frank");

Spring2->Add("Patrice");
Spring2->Add("Antoinette");
Spring2->Add("Albertine");

If you are planning to use a great number of objects of the same type, the alternative is to use an array of objects. Once again, use the new operator. This time, you should not directly assign the declared variable to the object's constructor. You must assign each object individually using its position in the array. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TStringList *Spring[2];
    Spring[0] = new TStringList;
    Spring[1] = new TStringList;

    Spring[0]->Add("Paul");
    Spring[0]->Add("Alain");
    Spring[0]->Add("Justin");
    Spring[0]->Add("Frank");

    Spring[1]->Add("Patrice");
    Spring[1]->Add("Antoinette");
    Spring[1]->Add("Albertine");

    ListBox1->Items->AddStrings(Spring[0]);
    ListBox2->Items->AddStrings(Spring[1]);
}
//---------------------------------------------------------------------------

If the array is even larger, you can use a for loop to assign the items of the array to the constructor. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    const int HowMany = 4;
    TStringList *Spring[HowMany];

    for(int i = 0; i < HowMany; i++)
        Spring[i] = new TStringList;

    Spring[0]->Add("Joseph");
    Spring[0]->Add("Michael");
    Spring[0]->Add("Caroline");
    Spring[0]->Add("Helene");
    Spring[0]->Add("Adam");

    Spring[1]->Add("Mauricette");
    Spring[1]->Add("Mark");
    Spring[1]->Add("Gertrude");

    Spring[2]->Add("Lydia");
    Spring[2]->Add("Annette");
    Spring[2]->Add("Pauline");
    Spring[2]->Add("Esther");

    Spring[3]->Add("Said");
    Spring[3]->Add("Amidou");
}
//---------------------------------------------------------------------------

The following example requires only a form. Here is the header file of the form:

//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
    void __fastcall FormCreate(TObject *Sender);
private:	// User declarations
    TPanel* Pan;
    TRadioButton *Buttons[4];
public:		// User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Here is the source file of the form:

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    Pan = new TPanel(Form1);
    Pan->Parent = Form1;
    Pan->Left = 16;
    Pan->Top  = 20;
    Pan->Width = 200;
    Pan->Height = 110;

    for(int i = 0; i < 4; i++)
        Buttons[i] = new TRadioButton(Pan);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    for(int i = 0; i < 4; i++)
        Buttons[i]->Parent = Pan;

    Buttons[0]->Caption = "Borland Customs Applications";
    Buttons[0]->Left = 16;
    Buttons[0]->Top  = 16;
    Buttons[0]->Width = 160;
    Buttons[1]->Caption = "The Visual Component Library";
    Buttons[1]->Left = 16;
    Buttons[1]->Top  = 38;
    Buttons[1]->Width = 160;
    Buttons[2]->Caption = "Borland C++ Builder";
    Buttons[2]->Left = 16;
    Buttons[2]->Top  = 60;
    Buttons[2]->Width = 160;
    Buttons[3]->Caption = "Develop - Test - Run!!!";
    Buttons[3]->Left = 16;
    Buttons[3]->Top  = 84;
    Buttons[3]->Width = 160;
}
//---------------------------------------------------------------------------
 

Copyright © 2002-2016, FunctionX