Home

Introduction to Arrays and Lists

 

A List as an Array of Values

 

Introduction

A database is a list of values. The values can be organized to make it easy to retrieve and optionally manipulate them. A computer database is a list of values that are stored in the computer, usually as one or more files. The values can then be accessed when needed. Probably the most fundamental type of list of values can be created, and managed, as an array.

Creating an Array

Before creating an array, you must decide what type of values each element of the array would be. A simple array can be made of values of primitive types. 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;

public ref class CExercise : public Form
{
private:
     Button ^ btnValues;

public:
    CExercise()
    {
	InitializeComponent();
    }

private:
void InitializeComponent()
{
    btnValues = gcnew Button;
    btnValues->Text = "Values";
    btnValues->Location = Point(10, 10);
    btnValues->Click += gcnew EventHandler(this, &CExercise::btnValuesClick);

    Controls->Add(btnValues);
    Text = L"Exercise";
    StartPosition = FormStartPosition::CenterScreen;
}

void btnValuesClick(Object ^ sender, EventArgs ^ e)
{
    array<double> ^ numbers = 
    	gcnew array<double> { 12.44, 525.38, 6.28, 2448.32, 632.04 };
}
};

int APIENTRY WinMain(HINSTANCE hInstance,
		     HINSTANCE hPrevInstance,
		     LPSTR lpCmdLine,
		     int nCmdShow)
{
    Application::Run(gcnew CExercise);

    return 0;
}

Once the array has been created, you can access each one of its elements using the [] operator. 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;

public ref class CExercise : public Form
{
private:
    Button ^ btnValues;
    DataGridView ^ dgvValues;

public:
    CExercise()
    {
	InitializeComponent();
    }

private:
    void InitializeComponent()
    {
	btnValues = gcnew Button;
	btnValues->Text = "Values";
        btnValues->Location = Point(10, 10);
	btnValues->Click += gcnew EventHandler(this, &CExercise::btnValuesClick);

        dgvValues = gcnew DataGridView();
        dgvValues->Location = Point(12, 40);

        Controls->Add(btnValues);
        Controls->Add(dgvValues);
        Text = L"Exercise";
        StartPosition = FormStartPosition::CenterScreen;
    }

    void btnValuesClick(Object ^ sender, EventArgs ^ e)
    {
	array<double> ^ numbers = 
		gcnew array<double> { 12.44, 525.38, 6.28, 2448.32, 632.04 };

        dgvValues->Columns->Add("Values", "Values");

        for(int i = 0; i < 5; i++)
        {
            dgvValues->Rows->Add();
            dgvValues->Rows[i]->Cells[0]->Value = numbers[i].ToString();
        }
    }
};

int APIENTRY WinMain(HINSTANCE hInstance,
		     HINSTANCE hPrevInstance,
		     LPSTR lpCmdLine,
		     int nCmdShow)
{
    Application::Run(gcnew CExercise);

    return 0;
}

Array

An array can also be made of elements that are each a composite type. That is, each element can be of a class type. Of course, you must have a class first. You can use one of the many built-in classes of the .NET Framework or you can create your own class. After creating the class, you can then use it as the type of 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;
    }

    void btnLoadClick(Object ^ sender, EventArgs ^ e)
    {
        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)
        };

        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;
}

Students Records

The Array Class

To assist you with creating and managing arrays, the .NET Framework provides the Array class:

public ref class Array abstract : ICloneable, 
    				  IList, 
    				  ICollection, 
    				  IEnumerable, 
    				  IStructuralComparable, 
    				  IStructuralEquatable

Every array you create is derived from this class. As a result, all arrays of your program share many characteristics and they get their foundation from the Array class. Once you declare an array variable, it automatically has access to the members of the Array class. For example, instead of counting the number of elements in an array, you can access the Length property of the array variable.

In traditional languages, when you declare an array variable, you must specify the number of elements that the array will contain. You must do this especially if you declare the variable without initializing the array.

After declaring the variable, before using the array, you must initialize it. Otherwise you would receive an error. When initializing the array, you can only initialize it with the number of elements you had specified.

One of the most valuable features of the Array class is that it allows an array to be resized. That is, if you find out that an array has a size smaller than the number of elements you want to add to it, you can increase its capacity. To support this, the Array class is equipped with the static Resize() member function. Its syntax is:

public:
    generic<typename T>
    static void Resize(array<T>^% array, int newSize);

As you can see, this is a generic member function that takes two arguments. The first argument is the name of the array variable that you want to resize. It must be passed by reference. The second argument is the new size you want the array to have. Here is an example of calling this member function:

#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 ^>(4);
        
	people[0] = gcnew CStudent(72947, "Cranston", "Paulette", Genders::Female);
        people[1] = gcnew CStudent(70854, "Harry", "Kumar", Genders::Male);
        people[2] = gcnew CStudent(27947, "Jules", "Davidson", Genders::Male);
        people[3] = gcnew CStudent(62835, "Leslie", "Harrington",
        			   Genders::Unspecified);

        Array::Resize<CStudent ^>(people, 10);

        people[4] = gcnew CStudent(92958, "Ernest", "Colson", Genders::Male);
        people[5] = gcnew CStudent(91749, "Patricia", "Katts", Genders::Female);
        people[6] = gcnew CStudent(29749, "Patrice", "Abanda", Genders::Unspecified);
        people[7] = gcnew CStudent(24739, "Frank", "Thomasson", Genders::Male);

        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;
}

An Array as a Field

As done previously, you can create an array in a member function. A disadvantage of this approach is that the array can be accessed from only the member function (or event) in which it is created. As an alternative, you can declare an array as a member of a class. Here is an example:

public ref class CExercise : public Form
{
    array<CStudent ^> ^ people;
}

The advantage of this approach is that you can access the array from any member of the same class or even from another file of the same program. After declaring the variable, you can initialize it. You can do this in a constructor or in an event that would be fired before any other event that would use the array. This type of initialization is usually done in a Load event of a form. After initializing the array, you can then access it in another member function or another event of the form. 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;

    array<CStudent ^> ^ people;

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)
    {
        people = gcnew array<CStudent ^>(4);
        
	people[0] = gcnew CStudent(72947, "Cranston", "Paulette", Genders::Female);
        people[1] = gcnew CStudent(70854, "Harry", "Kumar", Genders::Male);
        people[2] = gcnew CStudent(27947, "Jules", "Davidson", Genders::Male);
        people[3] = gcnew CStudent(62835, "Leslie", "Harrington",
        			   Genders::Unspecified);

        Array::Resize<CStudent ^>(people, 10);

        people[4] = gcnew CStudent(92958, "Ernest", "Colson", Genders::Male);
        people[5] = gcnew CStudent(91749, "Patricia", "Katts", Genders::Female);
        people[6] = gcnew CStudent(29749, "Patrice", "Abanda", Genders::Unspecified);
        people[7] = gcnew CStudent(24739, "Frank", "Thomasson", Genders::Male);

        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;
}
 
 
 

Arrays and Member Functions

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;
}

Operations on an Array

 

Introduction

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:

  • Adding elements to the array
  • Re-arranging the list or the order of elements in the array
  • Finding out whether the array contains a certain element
  • If the array contains a certain element, what index that element has

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.

Adding an Element to an Array

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.

Getting an Element From an Array

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.

Checking the Existence of an Element

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.

Finding an Element

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.

 
 
   
 

Home Copyright © 2010-2016, FunctionX