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");
}
//---------------------------------------------------------------------------
|
|