An array can be passed as argument to a member function. Here is an example: void ShowStudents(array<CStudent ^> ^ People)
{
}
In the member function, you can use the array as you see fit. For example you can assign values to the elements of the array. When calling a member function that is passed an array, you can pass the argument by reference so it would come back with new values. Here are examples: #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; enum class Genders { Male, Female, Unspecified }; public ref class CStudent { public: int StudentID; String ^ FirstName; String ^ LastName; Genders Gender; CStudent(int ID, String ^ fName, String ^lName, Genders gdr) { StudentID = ID; FirstName = fName; LastName = lName; Gender = gdr; } }; public ref class CExercise : public Form { private: Button ^ btnLoad; DataGridView ^ dgvValues; public: CExercise() { InitializeComponent(); } private: void InitializeComponent() { btnLoad = gcnew Button; btnLoad->Text = "Load"; btnLoad->Location = Point(12, 12); btnLoad->Click += gcnew EventHandler(this, &CExercise::btnLoadClick); dgvValues = gcnew DataGridView; dgvValues->Location = Point(12, 44); dgvValues->Size = System::Drawing::Size(270, 200); dgvValues->Anchor = AnchorStyles::Left | AnchorStyles::Top | AnchorStyles::Right | AnchorStyles::Bottom; Text = "Students Records"; Controls->Add(btnLoad); Controls->Add(dgvValues); Size = System::Drawing::Size(320, 280); StartPosition = FormStartPosition::CenterScreen; } void InitializeStudents(array<CStudent ^> ^ %People) { People = gcnew array<CStudent ^> { gcnew CStudent(72947, "Cranston", "Paulette", Genders::Female), gcnew CStudent(70854, "Harry", "Kumar", Genders::Male), gcnew CStudent(27947, "Jules", "Davidson", Genders::Male), gcnew CStudent(62835, "Leslie", "Harrington", Genders::Unspecified), gcnew CStudent(92958, "Ernest", "Colson", Genders::Male), gcnew CStudent(91749, "Patricia", "Katts", Genders::Female), gcnew CStudent(29749, "Patrice", "Abanda", Genders::Unspecified), gcnew CStudent(24739, "Frank", "Thomasson", Genders::Male) }; } void ShowStudents(array<CStudent ^> ^ People) { int i = 0; for each(CStudent ^ std in People) { dgvValues->Rows->Add(); dgvValues->Rows[i]->Cells[0]->Value = std->StudentID.ToString(); dgvValues->Rows[i]->Cells[1]->Value = std->FirstName; dgvValues->Rows[i]->Cells[2]->Value = std->LastName; dgvValues->Rows[i]->Cells[3]->Value = std->Gender.ToString(); i++; } } void btnLoadClick(Object ^ sender, EventArgs ^ e) { array<CStudent ^> ^ Students = gcnew array<CStudent ^>(8); InitializeStudents(Students); dgvValues->Columns->Add("StudendNumber", "Student ID"); dgvValues->Columns->Add("FirstName", "First Name"); dgvValues->Columns->Add("Last Name", "Last Name"); dgvValues->Columns->Add("Gender", "Gender"); ShowStudents(Students); } }; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Application::Run(gcnew CExercise); return 0; } A member function can be created to return an array. When creating the member function, on the left side of the name of the member function, type the name of the type of value the member function would return, including the square brackets. In the member function, create and initialize an array. Before exiting the member function, you must return the array. 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; enum class Genders { Male, Female, Unspecified }; public ref class CStudent { public: int StudentID; String ^ FirstName; String ^ LastName; Genders Gender; CStudent(int ID, String ^ fName, String ^lName, Genders gdr) { StudentID = ID; FirstName = fName; LastName = lName; Gender = gdr; } }; public ref class CExercise : public Form { private: Button ^ btnLoad; DataGridView ^ dgvValues; public: CExercise() { InitializeComponent(); } private: void InitializeComponent() { btnLoad = gcnew Button; btnLoad->Text = "Load"; btnLoad->Location = Point(12, 12); btnLoad->Click += gcnew EventHandler(this, &CExercise::btnLoadClick); dgvValues = gcnew DataGridView; dgvValues->Location = Point(12, 44); dgvValues->Size = System::Drawing::Size(270, 200); dgvValues->Anchor = AnchorStyles::Left | AnchorStyles::Top | AnchorStyles::Right | AnchorStyles::Bottom; Text = "Students Records"; Controls->Add(btnLoad); Controls->Add(dgvValues); Size = System::Drawing::Size(320, 280); StartPosition = FormStartPosition::CenterScreen; } array<CStudent ^> ^ InitializeStudents() { array<CStudent ^> ^ People = gcnew array<CStudent ^> { gcnew CStudent(72947, "Cranston", "Paulette", Genders::Female), gcnew CStudent(70854, "Harry", "Kumar", Genders::Male), gcnew CStudent(27947, "Jules", "Davidson", Genders::Male), gcnew CStudent(62835, "Leslie", "Harrington", Genders::Unspecified), gcnew CStudent(92958, "Ernest", "Colson", Genders::Male), gcnew CStudent(91749, "Patricia", "Katts", Genders::Female), gcnew CStudent(29749, "Patrice", "Abanda", Genders::Unspecified), gcnew CStudent(24739, "Frank", "Thomasson", Genders::Male) }; return People; } void ShowStudents(array<CStudent ^> ^ People) { int i = 0; for each(CStudent ^ std in People) { dgvValues->Rows->Add(); dgvValues->Rows[i]->Cells[0]->Value = std->StudentID.ToString(); dgvValues->Rows[i]->Cells[1]->Value = std->FirstName; dgvValues->Rows[i]->Cells[2]->Value = std->LastName; dgvValues->Rows[i]->Cells[3]->Value = std->Gender.ToString(); i++; } } void btnLoadClick(Object ^ sender, EventArgs ^ e) { array<CStudent ^> ^ Students = InitializeStudents(); dgvValues->Columns->Add("StudendNumber", "Student ID"); dgvValues->Columns->Add("FirstName", "First Name"); dgvValues->Columns->Add("Last Name", "Last Name"); dgvValues->Columns->Add("Gender", "Gender"); ShowStudents(Students); } }; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Application::Run(gcnew CExercise); return 0; }
Because an array is primarily a series of objects or values, there are various pieces of information you would get interested to get from it. Typical operations include:
Although you can write your own routines to perform these operations, the Array class provides most of the member functions you would need to get the necessary information.
We have seen that, using the [] operator, you can add a new element to an array. Still, to support this operation, the Array class is equipped with the SetValue() member function that is overloaded with various versions. Here is an example that adds an element to the third position of the array: #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;
enum class Genders { Male, Female, Unspecified };
public ref class CStudent
{
public:
int StudentID;
String ^ FirstName;
String ^ LastName;
Genders Gender;
CStudent(int ID, String ^ fName, String ^lName, Genders gdr)
{
StudentID = ID;
FirstName = fName;
LastName = lName;
Gender = gdr;
}
};
public ref class CExercise : public Form
{
private:
Button ^ btnLoad;
DataGridView ^ dgvValues;
public:
CExercise()
{
InitializeComponent();
}
private:
void InitializeComponent()
{
btnLoad = gcnew Button;
btnLoad->Text = "Load";
btnLoad->Location = Point(12, 12);
btnLoad->Click += gcnew EventHandler(this, &CExercise::btnLoadClick);
dgvValues = gcnew DataGridView;
dgvValues->Location = Point(12, 44);
dgvValues->Size = System::Drawing::Size(270, 200);
dgvValues->Anchor = AnchorStyles::Left | AnchorStyles::Top |
AnchorStyles::Right | AnchorStyles::Bottom;
Text = "Students Records";
Controls->Add(btnLoad);
Controls->Add(dgvValues);
Size = System::Drawing::Size(320, 280);
StartPosition = FormStartPosition::CenterScreen;
}
void btnLoadClick(Object ^ sender, EventArgs ^ e)
{
array<CStudent ^> ^ people = gcnew array<CStudent ^>(8);
people->SetValue(gcnew CStudent(72947, "Cranston", "Paulette",
Genders::Female), 0);
people->SetValue(gcnew CStudent(70854, "Harry", "Kumar", Genders::Male), 1);
people->SetValue(gcnew CStudent(27947, "Jules", "Davidson",
Genders::Male), 2);
people->SetValue(gcnew CStudent(62835, "Leslie", "Harrington",
Genders::Unspecified), 3);
people->SetValue(gcnew CStudent(92958, "Ernest", "Colson", Genders::Male), 4);
people->SetValue(gcnew CStudent(91749, "Patricia", "Katts",
Genders::Female), 5);
people->SetValue(gcnew CStudent(29749, "Patrice", "Abanda",
Genders::Unspecified), 6);
people->SetValue(gcnew CStudent(24739, "Frank", "Thomasson",
Genders::Male), 7);
dgvValues->Columns->Add("StudendNumber", "Std ID");
dgvValues->Columns->Add("FirstName", "First Name");
dgvValues->Columns->Add("Last Name", "Last Name");
dgvValues->Columns->Add("Gender", "Gender");
for (int i = 0; i < 8; i++)
{
dgvValues->Rows->Add();
dgvValues->Rows[i]->Cells[0]->Value = people[i]->StudentID.ToString();
dgvValues->Rows[i]->Cells[1]->Value = people[i]->FirstName;
dgvValues->Rows[i]->Cells[2]->Value = people[i]->LastName;
dgvValues->Rows[i]->Cells[3]->Value = people[i]->Gender.ToString();
}
}
};
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
Application::Run(gcnew CExercise);
return 0;
}
When the Array::SetValue() member function is called, it replaces the element at the indicated position.
The reverse of adding an item to an array is to retrieve one. You can do this with the [] operator. To support this operation, the Array class is equipped with the GetValue() member function that comes in various versions. When calling this member function, make sure you provide a valid index, if you don't, you would get an IndexOutOfRangeException exception.
Once some elements have been added to an array, you can try to locate one. One of the most fundamental means of looking for an item consists of finding out whether a certain element exists in the array. To support this operation, the Array class is equipped with the Exists() member function whose syntax is: public: generic<typename T> static bool Exists(array<T>^ array, Predicate<T>^ match); This is a generic member function that takes two arguments. The first is the array on which you will look for the item. The second argument is a delegate that specifies the criterion to apply.
One of the most valuable operations you can perform on an array consists of looking for a particular element. To assist you with this, the Array class is equipped with the Find() member function. Its syntax is: public: generic<typename T> static T Find(array<T>^ array, Predicate<T>^ match); This generic member function takes two arguments. The first argument is the name of the array that holds the elements. The second argument is a delegate that has the criterion to be used to find the element. |
|
|||||||||||||||
|