A list is meant to hold one or more items created and added to it. To support this operation, the IList class is equipped with a member function named Add that the collection classes must implement. The syntax of the Add() member function is: int Add(Object^ value); The Add() member function takes one argument that is the item to be added. You should not be concerned whether the list is empty or full. The compiler takes care of everything behind the scenes. Here is an example of calling the Add() member function of List<> collection: #include <windows.h>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Collections::Generic;
public ref class CCourse
{
public:
String ^ CourseCode;
String ^ CourseName;
int Credits;
CCourse();
CCourse(String ^ code, String ^ name, int semesterHours);
};
CCourse::CCourse()
{
CourseCode = "COLL 101";
CourseName = "Unknown";
Credits = 0;
}
CCourse::CCourse(String ^ code, String ^ name, int semesterHours)
{
CourseCode = code;
CourseName = name;
Credits = semesterHours;
}
public ref class CExercise : public Form
{
private:
Button ^ btnAdd;
List<CCourse ^> ^ CourseCatalog;
public:
CExercise()
{
InitializeComponent();
}
private:
void InitializeComponent()
{
btnAdd = gcnew Button;
btnAdd->Text = "Add";
btnAdd->Location = Point(20, 20);
btnAdd->Click += gcnew EventHandler(this, &CExercise::btnAddClick);
Text = "Exercise";
Controls->Add(btnAdd);
Load += gcnew EventHandler(this, &CExercise::FormLoaded);
Size = System::Drawing::Size(500, 300);
StartPosition = FormStartPosition::CenterScreen;
}
void FormLoaded(Object ^ sender, EventArgs ^ e)
{
CourseCatalog = gcnew List<CCourse ^>;
}
void btnAddClick(Object ^ sender, EventArgs ^ e)
{
CCourse ^ one = gcnew CCourse("CMSC 101", "Introductory Computer Science", 3);
CourseCatalog->Add(one);
}
};
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
Application::Run(gcnew CExercise);
return 0;
}
Instead of adding just one item in the collection, you can first create a list and add it at once. To support this operation, most collection classes are equipped with a member function named AddRange. The syntax of the AddRange() member function of the List<> class is: public: void AddRange(IEnumerable<T>^ collection); This member function takes an ICollection object as argument. This means that you can pass a variable whose type implements the ICollection interface, such as an array. Here is an example: void btnAddClick(Object ^ sender, EventArgs ^ e) { array<CCourse ^> ^ courses = gcnew array<CCourse ^>(4); courses[0] = gcnew CCourse("BMGT 304", "Managing E-Commerce in Organizations", 3); courses[1] = gcnew CCourse("ECON 201", "Principles of Macroeconomics", 3); courses[2] = gcnew CCourse("BMGT 324", "Introduction to Entrepreneurship: Starting a Small Business", 1); courses[3] = gcnew CCourse("CMST 306", "Introduction to Visual Basic Programming", 3); CourseCatalog->AddRange(courses); }
When the Add() member function is called, it adds a new item to the end of the collection. Sometimes, you want to add the new item inside the list at a position of your choice. To help you do this, the List<> provides the Insert() member function. Its syntax is: public: virtual void Insert(int index, T item) sealed; The Insert() member function is used to insert one item inside the list. If you have a collection to insert, you can call the InsertRange() member function whose syntax is: public: void InsertRange(int index, IEnumerable<T>^ collection);
The size of a list is the number of items it contains. To provide this information, the IList interface provides a property named Count that the implementers inherit. The Count property is of type int. This property is only used to provide information. You cannot change the number of items in the list by assigning a number to this property. The number of items automatically increases when a new item has been added and it decreases when an item is deleted.
The IList class is equipped with an indexed property that makes it possible to access any member of its variable using the square brackets [] applied to the variable. Here is an example: #include <windows.h> #using <System.dll> #using <System.Drawing.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Drawing; using namespace System::Windows::Forms; using namespace System::Collections::Generic; public ref class CCourse { public: String ^ CourseCode; String ^ CourseName; int Credits; CCourse(); CCourse(String ^ code, String ^ name, int semesterHours); }; CCourse::CCourse() { CourseCode = "COLL 101"; CourseName = "Unknown"; Credits = 0; } CCourse::CCourse(String ^ code, String ^ name, int semesterHours) { CourseCode = code; CourseName = name; Credits = semesterHours; } public ref class CExercise : public Form { private: Button ^ btnAdd; DataGridView ^ dgvCourses; List<CCourse ^> ^ CourseCatalog; public: CExercise() { InitializeComponent(); } private: void InitializeComponent() { btnAdd = gcnew Button; btnAdd->Text = "Add"; btnAdd->Location = Point(10, 10); btnAdd->Click += gcnew EventHandler(this, &CExercise::btnAddClick); dgvCourses = gcnew DataGridView; dgvCourses->Location = Point(12, 44); dgvCourses->Size = System::Drawing::Size(480, 200); Text = "Exercise"; Controls->Add(btnAdd); Controls->Add(dgvCourses); StartPosition = FormStartPosition::CenterScreen; Load += gcnew EventHandler(this, &CExercise::FormLoaded); Size = System::Drawing::Size(520, 300); dgvCourses->Anchor = AnchorStyles::Left | AnchorStyles::Top | AnchorStyles::Right | AnchorStyles::Bottom; } void FormLoaded(Object ^ sender, EventArgs ^ e) { dgvCourses->Columns->Add("CourseCode", "Course Code"); dgvCourses->Columns[0]->Width = 95; dgvCourses->Columns->Add("CourseName", "Course Name"); dgvCourses->Columns[1]->Width = 280; dgvCourses->Columns->Add("Credits", "Credits"); dgvCourses->Columns[2]->Width = 50; CourseCatalog = gcnew List<CCourse ^>; } void ShowCourses() { for (int i = 0; i < CourseCatalog->Count; i++) { dgvCourses->Rows->Add(); dgvCourses->Rows[i]->Cells[0]->Value = CourseCatalog[i]->CourseCode; dgvCourses->Rows[i]->Cells[1]->Value = CourseCatalog[i]->CourseName; dgvCourses->Rows[i]->Cells[2]->Value = CourseCatalog[i]->Credits.ToString(); } } void btnAddClick(Object ^ sender, EventArgs ^ e) { CCourse ^ one = gcnew CCourse("CMSC 101", "Introductory Computer Science", 3); CourseCatalog->Add(one); array<CCourse ^> ^ courses = gcnew array<CCourse ^>(4); courses[0] = gcnew CCourse("BMGT 304", "Managing E-Commerce in Organizations", 3); courses[1] = gcnew CCourse("ECON 201", "Principles of Macroeconomics", 3); courses[2] = gcnew CCourse("BMGT 324", "Introduction to Entrepreneurship: Starting a Small Business", 1); courses[3] = gcnew CCourse("CMST 306", "Introduction to Visual Basic Programming", 3); CourseCatalog->AddRange(courses); ShowCourses(); } }; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Application::Run(gcnew CExercise); return 0; }
The IList class is equipped with a member function named GetEnumerator. This makes it possible to use for each to iterate through the collection. The GetEnumerator() returns an object of type IEnumerable. Here is an example: void ShowCourses() { int i = 0; dgvCourses->Rows->Clear(); for each(CCourse ^ one in CourseCatalog) { dgvCourses->Rows->Add(); dgvCourses->Rows[i]->Cells[0]->Value = one->CourseCode; dgvCourses->Rows[i]->Cells[1]->Value = one->CourseName; dgvCourses->Rows[i]->Cells[2]->Value = one->Credits.ToString(); i++; } } As an alternative, the List<> class provides a member function named ForEach. Its syntax is: public: void for each(Action<T>^ action); This member function needs a predicate that specifies what action to perform on each member of the collection.
After a collection has been created, you may want to know whether it contains a certain element. To assist you with this, the IList interface provides a member function named Contains that its implements inherit. The syntax of the Contains() member function: public: virtual bool Contains(T item) sealed; This member function takes one argument as the item to look for. If the item is found in the collection, the member function returns true. If there is no such an item in the list, the member function returns false. To use this member function, in your application, you should override the Equals() member function in your class. There are many ways you can implement it. At a minimum, you can create a conditional statement that checks if a simple condition is met. If so, the member function can return true. Otherwise, it would return false. Once you have done this, you can then call the Contains() member function on your List<> variable. Here is an example: #include <windows.h> #using <System.dll> #using <System.Drawing.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Drawing; using namespace System::Windows::Forms; using namespace System::Collections::Generic; public ref class CCourse { public: String ^ CourseCode; String ^ CourseName; int Credits; CCourse(); CCourse(String ^ code); CCourse(String ^ code, String ^ name, int semesterHours); virtual bool Equals(Object ^obj) override; }; CCourse::CCourse() { CourseCode = "COLL 101"; CourseName = "Unknown"; Credits = 0; } CCourse::CCourse(String ^ code) { CourseCode = code; CourseName = "Unknown"; Credits = 0; } CCourse::CCourse(String ^ code, String ^ name, int semesterHours) { CourseCode = code; CourseName = name; Credits = semesterHours; } bool CCourse::Equals(Object ^ obj) { CCourse ^ current = reinterpret_cast<CCourse ^>(obj); if(current->CourseCode == CourseCode) return true; else return false; } public ref class CExercise : public Form { private: Button ^ btnCheck; DataGridView ^ dgvCourses; List<CCourse ^> ^ CourseCatalog; public: CExercise() { InitializeComponent(); } private: void InitializeComponent() { btnCheck = gcnew Button; btnCheck->Text = "Check"; btnCheck->Location = Point(10, 10); btnCheck->Click += gcnew EventHandler(this, &CExercise::btnCheckClick); dgvCourses = gcnew DataGridView; dgvCourses->Location = Point(12, 44); dgvCourses->Size = System::Drawing::Size(480, 200); Text = "Exercise"; Controls->Add(btnCheck); Controls->Add(dgvCourses); StartPosition = FormStartPosition::CenterScreen; Load += gcnew EventHandler(this, &CExercise::FormLoaded); Size = System::Drawing::Size(520, 300); dgvCourses->Anchor = AnchorStyles::Left | AnchorStyles::Top | AnchorStyles::Right | AnchorStyles::Bottom; } void FormLoaded(Object ^ sender, EventArgs ^ e) { dgvCourses->Columns->Add("CourseCode", "Course Code"); dgvCourses->Columns[0]->Width = 95; dgvCourses->Columns->Add("CourseName", "Course Name"); dgvCourses->Columns[1]->Width = 280; dgvCourses->Columns->Add("Credits", "Credits"); dgvCourses->Columns[2]->Width = 50; CourseCatalog = gcnew List<CCourse ^>; CCourse ^ one = gcnew CCourse("CMSC 101", "Introductory Computer Science", 3); CourseCatalog->Add(one); array<CCourse ^> ^ courses = gcnew array<CCourse ^>(4); courses[0] = gcnew CCourse("BMGT 304", "Managing E-Commerce in Organizations", 3); courses[1] = gcnew CCourse("ECON 201", "Principles of Macroeconomics", 3); courses[2] = gcnew CCourse("BMGT 324", "Introduction to Entrepreneurship: Starting a Small Business", 1); courses[3] = gcnew CCourse("CMST 306", "Introduction to Visual Basic Programming", 3); CourseCatalog->AddRange(courses); ShowCourses(); } static void Display(CCourse ^ sample) { MessageBox::Show(String::Format("Course Code:\t{0}\n" + "Course Name:\t{1}\nCredits:\t\t{2}", sample->CourseCode, sample->CourseName, sample->Credits), "Course Catalog", MessageBoxButtons::OK, MessageBoxIcon::Information); } void ShowCourses() { int i = 0; dgvCourses->Rows->Clear(); for each(CCourse ^ one in CourseCatalog) { dgvCourses->Rows->Add(); dgvCourses->Rows[i]->Cells[0]->Value = one->CourseCode; dgvCourses->Rows[i]->Cells[1]->Value = one->CourseName; dgvCourses->Rows[i]->Cells[2]->Value = one->Credits.ToString(); i++; } } void btnCheckClick(Object ^ sender, EventArgs ^ e) { CCourse ^ sample = gcnew CCourse("BMGT 324"); if(CourseCatalog->Contains(sample)) MessageBox::Show("The list contains this course already", "Course Catalog", MessageBoxButtons::OK, MessageBoxIcon::Information); else MessageBox::Show("That course was not yet added to the catalog", "Course Catalog", MessageBoxButtons::OK, MessageBoxIcon::Information); } }; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Application::Run(gcnew CExercise); return 0; }
void btnCheckClick(Object ^ sender, EventArgs ^ e) { CCourse ^ sample = gcnew CCourse("BMGT 320"); if(CourseCatalog->Contains(sample)) MessageBox::Show("The list contains this course already", "Course Catalog", MessageBoxButtons::OK, MessageBoxIcon::Information); else MessageBox::Show("That course was not yet added to the catalog", "Course Catalog", MessageBoxButtons::OK, MessageBoxIcon::Information); }
Alternatively, you can implement an Equals() member function that checks as many conditions as you want. As an alternative to the Contains() member function, the List<> class is equipped with a member function named Exists. Its syntax is: public: bool Exists(Predicate<T>^ match); As mentioned for arrays, this member function needs a predicate that would indicate how to check that an item exists in the collection.
Deleting an item consists of removing from a list. To support this operation, the IList interface provides the Remove() member function that its implementers must define. Its syntax is: public: virtual bool Remove(T item) sealed; This member function takes one argument that represents the item to be deleted. Of course, the item must exists in the list. To use this member function, your class should override the Equals() member function. You can then pass an object to this member function. Here is an example: void btnDeleteClick(Object ^ sender, EventArgs ^ e)
{
CCourse ^ one = gcnew CCourse("ECON 201");
CourseCatalog->Remove(one);
ShowCourses();
}
Instead of passing the exact object you want to delete, you can specify the index of the item. To make this possible, the IList interface provides a member function named RemoveAt. Its syntax: public: virtual void RemoveAt(int index) sealed; This time, the member function takes a constant integer. If there is an item at that index, the compiler deletes it. Here is an example: void btnDeleteClick(Object ^ sender, EventArgs ^ e) { CourseCatalog->RemoveAt(2); ShowCourses(); } If you pass an invalid index, the compiler throws an ArgumentOutOfRangeException exception.
The Remove() and the RemoveAt() member functions are used to delete one item. To allow you to remove everything from a list, the IList interface provides the Clear() member function. Its syntax is: public: virtual void Clear() sealed; When called, this member function empties the collection and sets its count of items to 0. |
|
|||||||||||||||||||||
|