The TList class is equipped with an indexed property named Items: __property void * Items = {read=Get,write=Put}; In reality, the items of a TList collection are stored as an array. To let you access each item using its array-position, the TList class overloads the [] operator: void * __fastcall operator [](int Index); lection and it can be represented as of Items[]. The TList::Items property takes an index as an integer and returns the object that is stored at that index. Remember that the TList class doesn't define or control the type of object it holds and it can hold almost anything. Therefore, when accessing a member of the list, you must cast it to the type of your object. You do this using reinterpret_cast. The basic formula to follow is: Variable = reinterpret_cast<Class *>(TList Variable...); You can also use the Items member variable in a for loop to scan the list.
To get to the very first item in the list, you have two options. You can call the Items variable with an index of 0 as Items[0]. Alternatively, you can call the TList::First() method whose syntax is: void * __fastcall First(void); Here is an example of calling this method: //---------------------------------------------------------------------------
void __fastcall TfrmExercise::btnAddClick(TObject *Sender)
{
TCountry * Pays = new TCountry;
Pays->CountryName = L"Ecuador";
Pays->Area = 283561;
Pays->Population = 14790608;
Pays->Capital = L"Quito";
Pays->InternetCode = L"ec";
Countries->Add(Pays);
Pays = new TCountry;
Pays->CountryName = L"Algeria";
Pays->Area = 2381741;
Pays->Population = 34586184;
Pays->Capital = L"Algiers";
Pays->InternetCode = L"dz";
Countries->Add(Pays);
Pays = new TCountry;
Pays->CountryName = L"Portugal";
Pays->Area = 92090;
Pays->Population = 10735765;
Pays->Capital = L"Lisbon";
Pays->InternetCode = L"pt";
Countries->Add(Pays);
}
//---------------------------------------------------------------------------
void __fastcall TfrmExercise::btnFirstClick(TObject *Sender)
{
TCountry * FirstCountry = reinterpret_cast<TCountry *>(Countries->First());
ShowMessage(L"The first country in the list is " +
FirstCountry->CountryName);
}
//---------------------------------------------------------------------------
After clicking the Add followed by the First buttons, this would produce:
To access the last item of a TList collection, you can either call Items[TList::Count-1] or use the TList::Last() method. Its syntax is: void * __fastcall Last(void); Here is an example of calling this method: //---------------------------------------------------------------------------
void __fastcall TfrmExercise::btnLastClick(TObject *Sender)
{
TCountry * FirstCountry = reinterpret_cast<TCountry *>(Countries->Last());
ShowMessage(L"The first country in the list is " +
FirstCountry->CountryName);
}
//---------------------------------------------------------------------------
If there is an item in the list and you want to get its index, you can call the TList::IndexOf() method. Its syntax is: int __fastcall IndexOf(void * Item); To use the IndexOf() method, you must provide the item that you want to locate. If the item exists, this function returns the index of the item.
Some of the common operations performed on items of a list consist of adding, inserting, deleting, or moving items to or from a list. All these operations are already provided by the TList class. The most you have to do is to adapt the class to your goal. In the previous section of this lesson, we learned how to statically create a list. That is, we learned how you, the programmer, can create a list for the user. If you want the user to have more control on the list, you should provide the ability to add new items to a list or insert items.
Besides the TList::Add() method used to add an item to a list, the TList class also allows you to insert an item inside the list. This operation is handled by the Insert() method. Its syntax is: void __fastcall Insert(int Index, void *Item); The first argument, Index, specifies the index that the new item will occupy after being added. Because the list of items is zero-based (since it is a C++ array), to add an item to the first position, specify Index as 0. In the same way, to add an item to the 3rd position, specify Index as 2. The Item argument is the item that you want to insert. There are two main ways you would use the Insert() method. If you know the exact position where you want to insert an object, then supply the known index. Here is an example: //---------------------------------------------------------------------------
void __fastcall TfrmExercise::btnInsertClick(TObject *Sender)
{
TCountry * Pais = new TCountry;
Pais->CountryName = L"Mexico";
Pais->Area = 1964375;
Pais->Population = 112468855;
Pais->Capital = L"Mexico City";
Pais->InternetCode = L"mx";
Countries->Insert(0, Pais);
}
//---------------------------------------------------------------------------
By contrast, you can use Insert() to insert a new item before or after one of your choice. To do this, you must first retrieve the index of the item that will succeed the one you want to add. This index would be used as a the Index argument.
If you find out that your list includes an item you don't need, you can remove such an item from your list. The deletion of an item from a TList list can be handled by the Delete() method. Its syntax is: void __fastcall Delete(int Index); To process your request, this function needs the index of the item you want to delete. Here is an example: //---------------------------------------------------------------------------
void __fastcall TfrmExercise::btnDeleteClick(TObject *Sender)
{
Countries->Delete(2);
}
//---------------------------------------------------------------------------
You can also get the index using the TList::Items[] member variable.
Besides the Delete() method, the TList class provides a method used to delete a record if you know the value of the record instead of its position. The method is TList::Remove() and its syntax is: int __fastcall Remove(void *Item); Instead of the index, the Remove method needs the item itself. If you supply an item that exists in the list, Remove() would delete it. If the operation is successful, Remove() returns the index the item had. There are two significant differences between the Delete() and the Remove() methods:
Clearing a list consists of deleting all of its records in one step. This operation can be taken care of by the TList::Clear() method. Its syntax is: void __fastcall Clear(); As you can see, there is no particular information this function needs. If the list is empty, the method would not do anything. If the list contains records, then all of them would be deleted. The only thing you might do is to warn the user especially if the records cannot be recovered. |
|
|||||||||||||||||||||
|