Home

Introduction to Databases:
A Database 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 a one or more computers, 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.

 

Practical Learning: Introducing Databases

  1. Start Microsoft Visual C++
  2. Create a Windows Forms Application named YugoNationalBank1

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 primitive types of values. Here is an example:

#pragma once

#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:
	ListBox ^ lbxNumbers;
	void FormLoader(Object ^ sender, EventArgs ^ e);
	void InitializeComponents();
	
public:
	CExercise();
};

CExercise::CExercise()
{
	InitializeComponents();
}

void CExercise::InitializeComponents()
{
	lbxNumbers = gcnew ListBox();
	lbxNumbers->Location = Point(10, 10);
	Controls->Add(lbxNumbers);

	Text = L"Numbers";
	StartPosition = FormStartPosition::CenterScreen;
	Load += gcnew EventHandler(this, &CExercise::FormLoader);
}

void CExercise::FormLoader(Object ^ sender, EventArgs ^ e)
{       
	double Numbers[] = { 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:

void CExercise::FormLoader(Object ^ sender, EventArgs ^ e)
{       
	double Numbers[] = { 12.44, 525.38, 6.28, 2448.32, 632.04 };

    for (int i = 0; i < 5; i++)
        lbxNumbers->Items->Add(Numbers[i]);
}

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. Here is an example:

Person.h
#pragma once
using namespace System;

public ref class CPerson
{
public:
    int PersonID;
    String ^ FirstName;
    String ^ LastName;
    String ^ Gender;

public:
    CPerson(void);
    CPerson(int ID, String ^ fn, String ^ ln, String ^ gdr);
};
Person.cpp
#include "StdAfx.h"
#include "Person.h"

CPerson::CPerson(void)
{
}

CPerson::CPerson(int ID, String ^ fn, String ^ ln, String ^ gdr)
{
    PersonID = ID;
    FirstName = fn;
    LastName = ln;
    Gender = gdr;
}

After creating the class, you can then use it as the type of the array. Here is an example:

#pragma once

#include "Person.h"

namespace Arrays
{
	. . . No Change

#pragma region Windows Form Designer generated code
	. . . No Change
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
	{
	    array<CPerson ^> ^ People = gcnew array<CPerson ^>(8);

	    People[0] = gcnew CPerson(72947, L"Paulette",
		                      L"Cranston", L"Female");
            People[1] = gcnew CPerson(70854, L"Harry", 
		                      L"Kumar", L"Male");
	    People[2] = gcnew CPerson(27947, L"Jules", 
	                          L"Davidson", L"Male");
	    People[3] = gcnew CPerson(62835, L"Leslie", 
		                      L"Harrington", L"Unknown");
	    People[4] = gcnew CPerson(92958, L"Ernest",
		                      L"Colson", L"Male");
	    People[5] = gcnew CPerson(91749, L"Patricia",
		                      L"Katts", L"Female");
	    People[6] = gcnew CPerson(29749, L"Patrice", 
		                      L"Abanda", L"Unknown");
	    People[7] = gcnew CPerson(24739, L"Frank",
		                      L"Thomasson", L"Male");

            for(int i = 0; i < 8; i++)
	    {
		ListViewItem ^ lviPerson = gcnew ListViewItem(People[i]->PersonID->ToString());
		lviPerson->SubItems->Add(People[i]->FirstName);
                lviPerson->SubItems->Add(People[i]->LastName);
		lviPerson->SubItems->Add(People[i]->Gender);
		lvwPeople->Items->Add(lviPerson);
	    }
	}
    };
}

Array

The Array Class

To assist you with creating or managing arrays, the .NET Framework provides the Array class. 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, which include its properties and methods. 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. Here is an example:

System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
{
    array<CPerson ^> ^ People = gcnew array<CPerson ^>(8);

    People[0] = gcnew CPerson(72947, L"Paulette",
	                      L"Cranston", L"Female");
    People[1] = gcnew CPerson(70854, L"Harry", 
	                      L"Kumar", L"Male");
    People[2] = gcnew CPerson(27947, L"Jules", 
                              L"Davidson", L"Male");
    People[3] = gcnew CPerson(62835, L"Leslie", 
	                      L"Harrington", L"Unknown");
    People[4] = gcnew CPerson(92958, L"Ernest",
	                      L"Colson", L"Male");
    People[5] = gcnew CPerson(91749, L"Patricia",
	                      L"Katts", L"Female");
    People[6] = gcnew CPerson(29749, L"Patrice", 
	                      L"Abanda", L"Unknown");
    People[7] = gcnew CPerson(24739, L"Frank",
	                      L"Thomasson", L"Male");

    for(int i = 0; i < People->Length; i++)
    {
	ListViewItem ^ lviPerson = gcnew ListViewItem(People[i]->PersonID->ToString());
	lviPerson->SubItems->Add(People[i]->FirstName);
        lviPerson->SubItems->Add(People[i]->LastName);
	lviPerson->SubItems->Add(People[i]->Gender);
	lvwPeople->Items->Add(lviPerson);
    }
}

When you declare an array variable, you must specify the number of elements that the array will contain. Here is an example:

System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
{
    array<CPerson ^> ^ People = gcnew array<CPerson ^>(4);
}

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. To illustrate this, consider the following program:

System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
{
    array<CPerson ^> ^ People = gcnew array<CPerson ^>(4);

    People[0] = gcnew CPerson(72947, L"Paulette",
	                      L"Cranston", L"Female");
    People[1] = gcnew CPerson(70854, L"Harry", 
	                      L"Kumar", L"Male");
    People[2] = gcnew CPerson(27947, L"Jules", 
                              L"Davidson", L"Male");
    People[3] = gcnew CPerson(62835, L"Leslie", 
	                      L"Harrington", L"Unknown");
    People[4] = gcnew CPerson(92958, L"Ernest",
	                      L"Colson", L"Male");
    People[5] = gcnew CPerson(91749, L"Patricia",
	                      L"Katts", L"Female");
    People[6] = gcnew CPerson(29749, L"Patrice", 
	                      L"Abanda", L"Unknown");
    People[7] = gcnew CPerson(24739, L"Frank",
	                      L"Thomasson", L"Male");

    for(int i = 0; i < People->Length; i++)
    {
	ListViewItem ^ lviPerson = gcnew ListViewItem(People[i]->PersonID->ToString());
	lviPerson->SubItems->Add(People[i]->FirstName);
        lviPerson->SubItems->Add(People[i]->LastName);
	lviPerson->SubItems->Add(People[i]->Gender);
	lvwPeople->Items->Add(lviPerson);
    }
}

Notice that the variable is declared to hold only 4 elements but the user tries to access a 5th one. This would produce an IndexOutOfRangeExcception exception:

Index out of Range

One of the most valuable features of the Array class is that it allows an array to be "resizable". 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() method. Its syntax is:

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

As you can see, this is a generic method 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 method:

System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
{
    array<CPerson ^> ^ People = gcnew array<CPerson ^>(4);

    People[0] = gcnew CPerson(72947, L"Paulette",
	                      L"Cranston", L"Female");
    People[1] = gcnew CPerson(70854, L"Harry", 
	                      L"Kumar", L"Male");
    People[2] = gcnew CPerson(27947, L"Jules", 
	                      L"Davidson", L"Male");
    People[3] = gcnew CPerson(62835, L"Leslie", 
	                      L"Harrington", L"Unknown");

    Array::Resize(People, 8);

    People[4] = gcnew CPerson(92958, L"Ernest",
	                      L"Colson", L"Male");
    People[5] = gcnew CPerson(91749, L"Patricia",
	                      L"Katts", L"Female");
    People[6] = gcnew CPerson(29749, L"Patrice", 
	                      L"Abanda", L"Unknown");
    People[7] = gcnew CPerson(24739, L"Frank",
	                      L"Thomasson", L"Male");

    for(int i = 0; i < People->Length; i++)
    {
	ListViewItem ^ lviPerson = gcnew ListViewItem(People[i]->PersonID->ToString());
	lviPerson->SubItems->Add(People[i]->FirstName);
        lviPerson->SubItems->Add(People[i]->LastName);
	lviPerson->SubItems->Add(People[i]->Gender);
	lvwPeople->Items->Add(lviPerson);
    }
}

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.

An Array as a Field

As done previously, you can create an array in a method. A disadvantage of this approach is that the array can be accessed from only the method (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:

#pragma once

#include <windows.h>
#include "Person.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:
	array<CPerson ^> ^ People;

	void FormLoader(Object ^ sender, EventArgs ^ e);
	void InitializeComponents();
	
public:
	CExercise();
};

CExercise::CExercise()
{
	InitializeComponents();
}

void CExercise::InitializeComponents()
{
	Text = L"People";
	StartPosition = FormStartPosition::CenterScreen;
	Load += gcnew EventHandler(this, &CExercise::FormLoader);
}

void CExercise::FormLoader(Object ^ sender, EventArgs ^ e)
{       
	
}

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

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 in another method or another event of the form. Here is an example:

#pragma once

#include <windows.h>
#include "Person.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:
	array<CPerson ^> ^ People;
	ListView ^ lvwPeople;
	ColumnHeader ^ colIndex;
	ColumnHeader ^ colFirstName;
	ColumnHeader ^ colLastName;
	ColumnHeader ^ colGender;

	void FormLoader(Object ^ sender, EventArgs ^ e);
	void InitializeComponents();
	
public:
	CExercise();
};

CExercise::CExercise()
{
	InitializeComponents();
}

void CExercise::InitializeComponents()
{
	Text = L"People";
	lvwPeople = gcnew ListView;
	lvwPeople->Location = Point(12, 12);
	lvwPeople->Width = 290;
	lvwPeople->Height = 135;
	lvwPeople->View = View::Details;
	lvwPeople->FullRowSelect = true;
	lvwPeople->GridLines = true;

	colIndex = gcnew ColumnHeader();
	colIndex->Text = L"Pers ID";
	colIndex->Width = 50;
	lvwPeople->Columns->Add(colIndex);

	colFirstName = gcnew ColumnHeader;
	colFirstName->Text = L"First Name";
	colFirstName->Width = 80;
	lvwPeople->Columns->Add(colFirstName);

	colLastName  = gcnew ColumnHeader;
	colLastName->Text = L"Last Name";
	colLastName->Width = 80;
	lvwPeople->Columns->Add(colLastName);

	colGender    = gcnew ColumnHeader;
	colGender->Text = L"Gender";
	colGender->TextAlign = HorizontalAlignment::Center;
	colGender->Width = 70;
	lvwPeople->Columns->Add(colGender);

	Controls->Add(lvwPeople);
	StartPosition = FormStartPosition::CenterScreen;
	Width = 320;
	Height = 185;
	Load += gcnew EventHandler(this, &CExercise::FormLoader);
}

void CExercise::FormLoader(Object ^ sender, EventArgs ^ e)
{       
	People = gcnew array<CPerson ^>(8);

	People[0] = gcnew CPerson;
	People[0]->PersonID = 72947;        People[0]->Gender = L"Female";
	People[0]->FirstName = L"Paulette"; People[0]->LastName = L"Cranston";
            
    	People[1] = gcnew CPerson;
    	People[1]->PersonID = 70854;      People[1]->Gender = L"Male";
	People[1]->FirstName = L"Harry";  People[1]->LastName = L"Kumar";
            
    	People[2] = gcnew CPerson;
    	People[2]->PersonID = 27947;	  People[2]->Gender = L"Male";
	People[2]->FirstName = L"Jules";  People[2]->LastName = L"Davidson";
            
    	People[3] = gcnew CPerson;
    	People[3]->PersonID = 62835;	  People[3]->Gender = L"Unknown";
	People[3]->FirstName = L"Leslie"; People[3]->LastName = L"Harrington";
            
    	People[4] = gcnew CPerson;
    	People[4]->PersonID = 92958;	  People[4]->Gender = L"Male";
	People[4]->FirstName = L"Ernest"; People[4]->LastName = L"Colson";
            
    	People[5] = gcnew CPerson;
    	People[5]->PersonID = 91749; 	    People[5]->Gender = L"Female";
	People[5]->FirstName = L"Patricia"; People[5]->LastName = L"Katts";
            
    	People[6] = gcnew CPerson;
    	People[6]->PersonID = 29749;  	    People[6]->Gender = L"Unknown";
	People[6]->FirstName = L"Patrice";  People[6]->LastName = L"Abanda";
            
    	People[7] = gcnew CPerson;
    	People[7]->PersonID = 24739; 	    People[7]->Gender = L"Male";
	People[7]->FirstName = L"Frank";    People[7]->LastName = L"Thomasson";

	for each( CPerson ^ pers in People)
	{
		ListViewItem ^ lviPerson = gcnew ListViewItem(pers->PersonID->ToString());

		lviPerson->SubItems->Add(pers->FirstName);
		lviPerson->SubItems->Add(pers->LastName);
		String ^ gdr = (String ^)pers->Gender;
		lviPerson->SubItems->Add(gdr);

		lvwPeople->Items->Add(lviPerson);
	}
}

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

Arrays and Methods

An array can be passed as argument to a method. Here is an example:

void ArrayInitializer(array<CPerson ^> ^ People)
{
}

In the method, you can use the array as you see fit. For example you can assign values to the elements of the array. When calling a method that is passed an array, you can pass the argument by reference so it would come back with new values. Here are examples:

#pragma once

#include <windows.h>
#include "Person.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:
	ListView ^ lvwPeople;
	ColumnHeader ^ colIndex;
	ColumnHeader ^ colFirstName;
	ColumnHeader ^ colLastName;
	ColumnHeader ^ colGender;

	void CExercise::ArrayInitializer(array<CPerson ^> ^ People);
	void CExercise::ShowPeople(array<CPerson ^> ^ People);

	void FormLoader(Object ^ sender, EventArgs ^ e);
	void InitializeComponents();
	
public:
	CExercise();
};

CExercise::CExercise()
{
	InitializeComponents();
}

void CExercise::InitializeComponents()
{
	Text = L"People";
	lvwPeople = gcnew ListView;
	lvwPeople->Location = Point(12, 12);
	lvwPeople->Width = 290;
	lvwPeople->Height = 135;
	lvwPeople->View = View::Details;
	lvwPeople->FullRowSelect = true;
	lvwPeople->GridLines = true;

	colIndex = gcnew ColumnHeader();
	colIndex->Text = L"Pers ID";
	colIndex->Width = 50;
	lvwPeople->Columns->Add(colIndex);

	colFirstName = gcnew ColumnHeader;
	colFirstName->Text = L"First Name";
	colFirstName->Width = 80;
	lvwPeople->Columns->Add(colFirstName);

	colLastName  = gcnew ColumnHeader;
	colLastName->Text = L"Last Name";
	colLastName->Width = 80;
	lvwPeople->Columns->Add(colLastName);

	colGender    = gcnew ColumnHeader;
	colGender->Text = L"Gender";
	colGender->TextAlign = HorizontalAlignment::Center;
	colGender->Width = 70;
	lvwPeople->Columns->Add(colGender);

	Controls->Add(lvwPeople);
	StartPosition = FormStartPosition::CenterScreen;
	Width = 320;
	Height = 185;
	Load += gcnew EventHandler(this, &CExercise::FormLoader);
}

void CExercise::ArrayInitializer(array<CPerson ^> ^ People)
{
	People[0] = gcnew CPerson;
	People[0]->PersonID = 72947; 	    People[0]->Gender = L"Female";
	People[0]->FirstName = L"Paulette"; People[0]->LastName = L"Cranston";
            
    	People[1] = gcnew CPerson;
    	People[1]->PersonID = 70854;	    People[1]->Gender = L"Male";
	People[1]->FirstName = L"Harry";    People[1]->LastName = L"Kumar";
            
    	People[2] = gcnew CPerson;
    	People[2]->PersonID = 27947;  	    People[2]->Gender = L"Male";
	People[2]->FirstName = L"Jules";    People[2]->LastName = L"Davidson";
            
    	People[3] = gcnew CPerson;
    	People[3]->PersonID = 62835;	    People[3]->Gender = L"Unknown";
	People[3]->FirstName = L"Leslie";   People[3]->LastName = L"Harrington";
            
    	People[4] = gcnew CPerson;
    	People[4]->PersonID = 92958;	    People[4]->Gender = L"Male";
	People[4]->FirstName = L"Ernest";   People[4]->LastName = L"Colson";
            
    	People[5] = gcnew CPerson;
    	People[5]->PersonID = 91749;	    People[5]->Gender = L"Female";
	People[5]->FirstName = L"Patricia"; People[5]->LastName = L"Katts";
            
    	People[6] = gcnew CPerson;
    	People[6]->PersonID = 29749;	    People[6]->Gender = L"Unknown";
	People[6]->FirstName = L"Patrice";  People[6]->LastName = L"Abanda";
            
    	People[7] = gcnew CPerson;
    	People[7]->PersonID = 24739; 	    People[7]->Gender = L"Male";
	People[7]->FirstName = L"Frank";    People[7]->LastName = L"Thomasson";
}

void CExercise::ShowPeople(array<CPerson ^> ^ People)
{
	for each( CPerson ^ pers in People)
	{
		ListViewItem ^ lviPerson = gcnew ListViewItem(pers->PersonID->ToString());

		lviPerson->SubItems->Add(pers->FirstName);
		lviPerson->SubItems->Add(pers->LastName);
		String ^ gdr = (String ^)pers->Gender;
		lviPerson->SubItems->Add(gdr);

		lvwPeople->Items->Add(lviPerson);
	}
}

void CExercise::FormLoader(Object ^ sender, EventArgs ^ e)
{       
	array<CPerson ^> ^ People = gcnew array<CPerson ^>(8);

	ArrayInitializer(People);
	ShowPeople(People);
}

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

A method can be created to return an array. When creating the method, on the left side of the name of the method, type the name of the type of value the method would return, including the square brackets. In the method, create and initialize an array. Before exiting the method, you must return the array. Here is an example:

#pragma once

#include <windows.h>
#include "Person.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:
	ListView ^ lvwPeople;
	ColumnHeader ^ colIndex;
	ColumnHeader ^ colFirstName;
	ColumnHeader ^ colLastName;
	ColumnHeader ^ colGender;

	array<CPerson ^> ^ CExercise::ArrayInitializer();
	void CExercise::ShowPeople(array<CPerson ^> ^ People);

	void FormLoader(Object ^ sender, EventArgs ^ e);
	void InitializeComponents();
	
public:
	CExercise();
};

CExercise::CExercise()
{
	InitializeComponents();
}

void CExercise::InitializeComponents()
{
	Text = L"People";
	lvwPeople = gcnew ListView;
	lvwPeople->Location = Point(12, 12);
	lvwPeople->Width = 290;
	lvwPeople->Height = 135;
	lvwPeople->View = View::Details;
	lvwPeople->FullRowSelect = true;
	lvwPeople->GridLines = true;

	colIndex = gcnew ColumnHeader();
	colIndex->Text = L"Pers ID";
	colIndex->Width = 50;
	lvwPeople->Columns->Add(colIndex);

	colFirstName = gcnew ColumnHeader;
	colFirstName->Text = L"First Name";
	colFirstName->Width = 80;
	lvwPeople->Columns->Add(colFirstName);

	colLastName  = gcnew ColumnHeader;
	colLastName->Text = L"Last Name";
	colLastName->Width = 80;
	lvwPeople->Columns->Add(colLastName);

	colGender    = gcnew ColumnHeader;
	colGender->Text = L"Gender";
	colGender->TextAlign = HorizontalAlignment::Center;
	colGender->Width = 70;
	lvwPeople->Columns->Add(colGender);

	Controls->Add(lvwPeople);
	StartPosition = FormStartPosition::CenterScreen;
	Width = 320;
	Height = 185;
	Load += gcnew EventHandler(this, &CExercise::FormLoader);
}

array<CPerson ^> ^ CExercise::ArrayInitializer()
{
    array<CPerson ^> ^ People = gcnew array<CPerson ^>(8);

    People[0] = gcnew CPerson;
    People[0]->PersonID = 72947;	People[0]->Gender = L"Female";
    People[0]->FirstName = L"Paulette";	People[0]->LastName = L"Cranston";
            
    People[1] = gcnew CPerson;
    People[1]->PersonID = 70854;	People[1]->Gender = L"Male";
    People[1]->FirstName = L"Harry";	People[1]->LastName = L"Kumar";
            
    People[2] = gcnew CPerson;
    People[2]->PersonID = 27947;	People[2]->Gender = L"Male";
    People[2]->FirstName = L"Jules";	People[2]->LastName = L"Davidson";
            
    People[3] = gcnew CPerson;
    People[3]->PersonID = 62835;	People[3]->Gender = L"Unknown";
    People[3]->FirstName = L"Leslie";	People[3]->LastName = L"Harrington";
            
    People[4] = gcnew CPerson;
    People[4]->PersonID = 92958;	People[4]->Gender = L"Male";
    People[4]->FirstName = L"Ernest";	People[4]->LastName = L"Colson";
            
    People[5] = gcnew CPerson;
    People[5]->PersonID = 91749;	People[5]->Gender = L"Female";
    People[5]->FirstName = L"Patricia"; People[5]->LastName = L"Katts";
            
    People[6] = gcnew CPerson;
    People[6]->PersonID = 29749; 	People[6]->Gender = L"Unknown";
    People[6]->FirstName = L"Patrice";  People[6]->LastName = L"Abanda";
            
    People[7] = gcnew CPerson;
    People[7]->PersonID = 24739;	People[7]->Gender = L"Male";
    People[7]->FirstName = L"Frank";    People[7]->LastName = L"Thomasson";

    return People;
}

void CExercise::ShowPeople(array<CPerson ^> ^ People)
{
	for each( CPerson ^ pers in People)
	{
		ListViewItem ^ lviPerson = gcnew ListViewItem(pers->PersonID->ToString());

		lviPerson->SubItems->Add(pers->FirstName);
		lviPerson->SubItems->Add(pers->LastName);
		String ^ gdr = (String ^)pers->Gender;
		lviPerson->SubItems->Add(gdr);

		lvwPeople->Items->Add(lviPerson);
	}
}

void CExercise::FormLoader(Object ^ sender, EventArgs ^ e)
{       
	array<CPerson ^> ^ People = ArrayInitializer();
	ShowPeople(People);
}

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

 

 

Practical Learning: Creating an Array

  1. To create a new class, on the main menu, click Project -> Add Class...
  2. In the Add Class dialog box, click C++ Class and click Add
  3. Set the name to CEmployee and click Finish
  4. Complete the file as follows:
     
    #pragma once
    using namespace System;
    
    public ref class CEmployee
    {
    public:
        CEmployee(void);
    
        int EmployeeNumber;
        String ^ FirstName;
        String ^ LastName;
        String ^ Title;
        bool CanCreateNewAccount;
        double HourlySalary;
    
        property String ^ FullName
        {
    	String ^ get()
    	{
    		return LastName + L", " + FirstName;
    	}
        }
    };
  5. To create a form that will be used to displays the employees information, on the main menu, click Project -> Add New Item...
  6. In the Templates list, click Windows Form
  7. Set the name to Employees
  8. Click Add
  9. Design the form as follows:
     
    Employees
     
    Control Text Name Other Properties
    ListView List View   lvwEmployees FullRowSelect: True
    GridLines: True
    View: Details
    Columns
    (Name) Text TextAlign Width
    colIndex #   20
    colEmployeeNumber Empl #   50
    colFirstName First Name   65
    colLastName Last Name   65
    colFullName Full Name   95
    colTitle Title   145
    colHourlySalary Salary Right 50
    Button Button Close btnClose  
  10. To create a new class, on the main menu, click Project -> Add Class...
  11. In the Add Class dialog box, click C++ Class
  12. Click Add
  13. Set the name to CCustomer
  14. Click Finish
  15. Complete the file as follows:
     
    #pragma once
    using namespace System;
    
    public ref class CCustomer
    {
    public:
        CCustomer(void);
    
        String ^ CreatedBy;
        DateTime DateCreated;
        String ^ CustomerName;
        String ^ AccountNumber;
        String ^ AccountType;
        String ^ AccountStatus;
    };
  16. To create a form that will show the customers records, on the main menu, click Project -> Add New Item...
  17. In the Templates list, click Windows Form
  18. Set the name to Customers and press Enter
  19. Design the form as follows:
     
    Customers
     
    Control Text Name Other Properties
    ListView List View   lvwCustomers FullRowSelect: True
    GridLines: True
    View: Details
    Columns
    (Name) Text TextAlign Width
    colIndex #   20
    colCreatedBy Created By   100
    colDateCreated Date Created Center 75
    colCustomerName Customer Name   120
    colAccountNumber Account # Center 80
    colAccountType Account Type   80
    coloAccountStatus Status   50
    Button Button Close btnClose  
  20. Double-click the Close button and implement its event as follows:
     
    System::Void btnClose_Click(System::Object^  sender, System::EventArgs^  e)
    {
        Close();
    }
  21. To create a new class, on the main menu, click Project -> Add Class...
  22. Click C++ Class and click Add
  23. Set the name to CAccountTransaction and press Enter
  24. Complete the file as follows:
     
    #pragma once
    using namespace System;
    
    ref class CAccountTransaction
    {
    public:
    	CAccountTransaction(void);
    
    	DateTime TransactionDate;
    	int ProcessedBy;
    	String ^ProcessedFor;
    	String ^ TransactionType;
    	double DepositAmount;
    	double WithdrawalAmount;
    	double ChargeAmount;
    };
  25. To create a new form that will show the transactions done on the customers accounts, on the main menu, click Project -> Add New Item...
  26. Select Windows Form
  27. Set the name to CustomersTransactions and press Enter
  28. Design the form as follows:
     
    Customers Transactions
     
    Control Text Name Other Properties
    ListView List View   lvwTransactions FullRowSelect: True
    GridLines: True
    View: Details
    Columns
    (Name) Text TextAlign Width
    colIndex #   25
    colTransactionDate Trans Date Center 80
    colProcessedBy Processed By Center 80
    colProcessedFor Processed For Center 90
    colTransactionType Trans Type   90
    colDepositAmount Deposit Right  
    colWithdrawalAmount Withdrawal Right 65
    colChargeAmount Charge Right 50
    Button Button Close btnClose  
  29. Double-click the Close button and implement its event as follows:
     
    System::Void btnClose_Click(System::Object^  sender, System::EventArgs^  e)
    {
        Close();
    }
  30. To create a new class, on the main menu, click Project -> Add Class...
  31. In the Templates list, click C++ Class
  32. Click Add
  33. Set the name to CAccountsRecords
  34. Click Finish
  35. Complete the file as follows:
     
    #pragma once
    #include "Employee.h"
    #include "Customer.h"
    #include "AccountTransaction.h"
    
    ref class CAccountsRecords
    {
    public:
        CAccountsRecords(void);
    
        static array<CEmployee ^> ^ GetEmployees()
        {
    	array<CEmployee ^> ^ StaffMembers = gcnew array<CEmployee ^>(7);
        
    	StaffMembers[0] = gcnew CEmployee;
    	StaffMembers[0]->EmployeeNumber = 74228;
    	StaffMembers[0]->FirstName = L"Chrissie";
    	StaffMembers[0]->LastName = L"Nomads";
    	StaffMembers[0]->Title = L"General Manager";
    	StaffMembers[0]->CanCreateNewAccount = true;
    	StaffMembers[0]->HourlySalary = 40.25;
    
            StaffMembers[1] = gcnew CEmployee;
    	StaffMembers[1]->EmployeeNumber = 27905;
    	StaffMembers[1]->FirstName = L"Calvin";
    	StaffMembers[1]->LastName = L"Braxton";
    	StaffMembers[1]->Title = L"Public Relations Manager";
    	StaffMembers[1]->CanCreateNewAccount = false;
    	StaffMembers[1]->HourlySalary = 25.95;
         
            StaffMembers[2] = gcnew CEmployee;
    	StaffMembers[2]->EmployeeNumber = 94805;
    	StaffMembers[2]->FirstName = L"Emilie";
    	StaffMembers[2]->LastName = L"Pastore";
    	StaffMembers[2]->Title = L"Branch Manager";
    	StaffMembers[2]->CanCreateNewAccount = true;
    	StaffMembers[2]->HourlySalary = 32.55;
    
            StaffMembers[3] = gcnew CEmployee;
    	StaffMembers[3]->EmployeeNumber = 39850;
    	StaffMembers[3]->FirstName = L"Walter";
    	StaffMembers[3]->LastName = L"Lemme";
    	StaffMembers[3]->Title = L"Accounts Manager";
    	StaffMembers[3]->CanCreateNewAccount = true;
    	StaffMembers[3]->HourlySalary = 28.35;
         
            StaffMembers[4] = gcnew CEmployee;
    	StaffMembers[4]->EmployeeNumber = 70594;
    	StaffMembers[4]->FirstName = L"Cecile";
    	StaffMembers[4]->LastName = L"Richards";
    	StaffMembers[4]->Title = L"Accounts Representative";
    	StaffMembers[4]->CanCreateNewAccount = false;
    	StaffMembers[4]->HourlySalary =   18.15;
          
            StaffMembers[5] = gcnew CEmployee;
    	StaffMembers[5]->EmployeeNumber = 85285;
    	StaffMembers[5]->FirstName = L"Joan";
    	StaffMembers[5]->LastName = L"Verrion";
    	StaffMembers[5]->Title = L"Accounts Representative";
    	StaffMembers[5]->CanCreateNewAccount = false;
    	StaffMembers[5]->HourlySalary =  14.85;
          
    	StaffMembers[6] = gcnew CEmployee;
    	StaffMembers[6]->EmployeeNumber = 94852;
    	StaffMembers[6]->FirstName = L"Donald";
    	StaffMembers[6]->LastName = L"Waters";
    	StaffMembers[6]->Title = L"Accounts Representative";
    	StaffMembers[6]->CanCreateNewAccount = false;
    	StaffMembers[6]->HourlySalary =   16.45;
    
    	return StaffMembers;
        }
    
        static array<CCustomer ^> ^ GetCustomers()
        {
    	array<CCustomer ^> ^ Clients = gcnew array<CCustomer ^>(5);
             
    	Clients[0] = gcnew CCustomer;
    	Clients[0]->CreatedBy = L"Lemme, Walter";
    	Clients[0]->DateCreated = DateTime(2007, 1, 16);
    	Clients[0]->CustomerName ="Louis George Berholdt";
    	Clients[0]->AccountNumber = L"95-722947-93";
    	Clients[0]->AccountType = L"Checking";
    	Clients[0]->AccountStatus = L"Active";
                    
    	Clients[1] = gcnew CCustomer;
    	Clients[1]->CreatedBy = L"Pastore, Emilie";
    	Clients[1]->DateCreated = DateTime(2007, 1, 18);
    	Clients[1]->CustomerName ="William Foster";
    	Clients[1]->AccountNumber = L"62-384638-48";
    	Clients[1]->AccountType = L"Checking";
    	Clients[1]->AccountStatus = L"Active";
                    
    	Clients[2] = gcnew CCustomer;
    	Clients[2]->CreatedBy = L"Lemme, Walter";
    	Clients[2]->DateCreated = DateTime(2007, 1, 18);
    	Clients[2]->CustomerName ="Catherine Hoods";
    	Clients[2]->AccountNumber = L"92-318284-75";
    	Clients[2]->AccountType = L"Checking";
    	Clients[2]->AccountStatus = L"Active";
                    
    	Clients[3] = gcnew CCustomer;
    	Clients[3]->CreatedBy = L"Lemme, Walter";
    	Clients[3]->DateCreated = DateTime(2007, 3, 26);
    	Clients[3]->CustomerName ="Harriett Cranston";
    	Clients[3]->AccountNumber = L"17-490040-83";
    	Clients[3]->AccountType = L"Saving";
    	Clients[3]->AccountStatus = L"Active";
                    
    	Clients[4] = gcnew CCustomer;
    	Clients[4]->CreatedBy = L"Nomads, Chrissie";
    	Clients[4]->DateCreated = DateTime(2007, 4, 04);
    	Clients[4]->CustomerName ="Janice Bonnie Weiss";
    	Clients[4]->AccountNumber = L"58-405048-15";
    	Clients[4]->AccountType = L"Checking";
    	Clients[4]->AccountStatus = L"Active";
    
    	return Clients;
        }
    
        static array<CAccountTransaction ^> ^ GetTransactions()
        {
    	array<CAccountTransaction ^> ^ Transactions =
    		gcnew array<CAccountTransaction ^>(28);
        
    	Transactions[0] = gcnew CAccountTransaction;
    	Transactions[0]->TransactionDate = DateTime(2007, 1,16);
    	Transactions[0]->ProcessedBy = 39850;
    	Transactions[0]->ProcessedFor = L"95-722947-93";
    	Transactions[0]->TransactionType = L"Deposit";
    	Transactions[0]->DepositAmount = 325.50;
    	
    	Transactions[1] = gcnew CAccountTransaction;
    	Transactions[1]->TransactionDate = DateTime(2007, 1,18);
    	Transactions[1]->ProcessedBy = 94805;
    	Transactions[1]->ProcessedFor = L"62-384638-48";
    	Transactions[1]->TransactionType = L"Deposit";
    	Transactions[1]->DepositAmount = 550.00;
    
    	Transactions[2] = gcnew CAccountTransaction;
    	Transactions[2]->TransactionDate = DateTime(2007,1,18);
    	Transactions[2]->ProcessedBy = 39850;
    	Transactions[2]->ProcessedFor = L"92-318284-75";
    	Transactions[2]->TransactionType = L"Deposit";
    	Transactions[2]->DepositAmount = 975.35;
    	
    	Transactions[3] = gcnew CAccountTransaction;
    	Transactions[3]->TransactionDate = DateTime(2007,1,22);
    	Transactions[3]->ProcessedBy = 94852;
    	Transactions[3]->ProcessedFor = L"95-722947-93";
    	Transactions[3]->TransactionType = L"Withdrawal";
    	Transactions[3]->WithdrawalAmount = 100.00;
    	
    	Transactions[4] = gcnew CAccountTransaction;
    	Transactions[4]->TransactionDate = DateTime(2007,1,22);
    	Transactions[4]->ProcessedBy = 70594;
    	Transactions[4]->ProcessedFor = L"62-384638-48";
    	Transactions[4]->TransactionType = L"Withdrawal";
    	Transactions[4]->WithdrawalAmount = 122.48;
    
    	Transactions[5] = gcnew CAccountTransaction;
    	Transactions[5]->TransactionDate = DateTime(2007,1,22);
    	Transactions[5]->ProcessedBy = 94852;
    	Transactions[5]->ProcessedFor = L"92-318284-75";
    	Transactions[5]->TransactionType = L"Withdrawal";
    	Transactions[5]->WithdrawalAmount = 214.86;
    
    	Transactions[6] = gcnew CAccountTransaction;
    	Transactions[6]->TransactionDate = DateTime(2007,1,22);
    	Transactions[6]->ProcessedBy = 85285;
    	Transactions[6]->ProcessedFor = L"62-384638-48";
    	Transactions[6]->TransactionType = L"Withdrawal";
    	Transactions[6]->WithdrawalAmount = 140.00;
    
    	Transactions[7] = gcnew CAccountTransaction;
    	Transactions[7]->TransactionDate = DateTime(2007,1,24);
    	Transactions[7]->ProcessedBy = 85285;
    	Transactions[7]->ProcessedFor = L"95-722947-93";
    	Transactions[7]->TransactionType = L"MoneyOrder";
    	Transactions[7]->WithdrawalAmount = 116.24;
    
    	Transactions[8] = gcnew CAccountTransaction;
    	Transactions[8]->TransactionDate = DateTime(2007,1,24);
    	Transactions[8]->ProcessedFor = L"95-722947-93";
    	Transactions[8]->TransactionType = L"TransactionFee";
    	Transactions[8]->ChargeAmount = 1.45;
    
    	Transactions[9] = gcnew CAccountTransaction;
    	Transactions[9]->TransactionDate = DateTime(2007,1,30);
    	Transactions[9]->ProcessedBy = 70594;
    	Transactions[9]->ProcessedFor = L"62-384638-48";
    	Transactions[9]->TransactionType = L"Withdrawal";
    	Transactions[9]->WithdrawalAmount = 40.00;
    
    	Transactions[10] = gcnew CAccountTransaction;
    	Transactions[10]->TransactionDate = DateTime(2007,1,30);
    	Transactions[10]->ProcessedFor = L"95-722947-93";
    	Transactions[10]->TransactionType = L"Monthly Charge";
    	Transactions[10]->ChargeAmount = 6.00;
    
    	Transactions[11] = gcnew CAccountTransaction;
    	Transactions[11]->TransactionDate = DateTime(2007,1,30);
    	Transactions[11]->ProcessedFor = L"92-318284-75";
    	Transactions[11]->TransactionType = L"Monthly Charge";
    	Transactions[11]->ChargeAmount = 6.00;
    
    	Transactions[12] = gcnew CAccountTransaction;
    	Transactions[12]->TransactionDate = DateTime(2007,1,30);
    	Transactions[12]->ProcessedFor = L"62-384638-48";
    	Transactions[12]->TransactionType = L"Monthly Charge";
    	Transactions[12]->ChargeAmount = 6.00;
    
    	Transactions[13] = gcnew CAccountTransaction;
    	Transactions[13]->TransactionDate = DateTime(2007,2,06);
    	Transactions[13]->ProcessedBy = 85285;
    	Transactions[13]->ProcessedFor = L"92-318284-75";
    	Transactions[13]->TransactionType = L"Withdrawal";
    	Transactions[13]->WithdrawalAmount = 42.35;
    
    	Transactions[14] = gcnew CAccountTransaction;
    	Transactions[14]->TransactionDate = DateTime(2007,2,06);
    	Transactions[14]->ProcessedBy = 70594;
    	Transactions[14]->ProcessedFor = L"95-722947-93";
    	Transactions[14]->TransactionType = L"Withdrawal";
    	Transactions[14]->WithdrawalAmount = 115.00;
    	
    	Transactions[15] = gcnew CAccountTransaction;
    	Transactions[15]->TransactionDate = DateTime(2007,2,06);
    	Transactions[15]->ProcessedBy = 94852;
    	Transactions[15]->ProcessedFor = L"62-384638-48";
    	Transactions[15]->TransactionType = L"Withdrawal";
    	Transactions[15]->WithdrawalAmount = 64.14;
    
    	Transactions[16] = gcnew CAccountTransaction;
    	Transactions[16]->TransactionDate = DateTime(2007,2,28);
    	Transactions[16]->ProcessedFor = L"95-722947-93";
    	Transactions[16]->TransactionType = L"Monthly Charge";
    	Transactions[16]->ChargeAmount = 6.00;
    
    	Transactions[17] = gcnew CAccountTransaction;
    	Transactions[17]->TransactionDate = DateTime(2007,2,28);
    	Transactions[17]->ProcessedFor = L"95-722947-93";
    	Transactions[17]->TransactionType = L"Overdraft";
    	Transactions[17]->WithdrawalAmount = 35.00;
    
    	Transactions[18] = gcnew CAccountTransaction;
    	Transactions[18]->TransactionDate = DateTime(2007,2,28);
    	Transactions[18]->ProcessedFor = L"62-384638-48";
    	Transactions[18]->TransactionType = L"Monthly Charge";
    	Transactions[18]->ChargeAmount = 6.00;
    
    	Transactions[19] = gcnew CAccountTransaction;
    	Transactions[19]->TransactionDate = DateTime(2007,2,28);
    	Transactions[19]->ProcessedFor = L"92-318284-75";
    	Transactions[19]->TransactionType = L"Monthly Charge";
    	Transactions[19]->ChargeAmount = 6.00;
    
    	Transactions[20] = gcnew CAccountTransaction;
    	Transactions[20]->TransactionDate = DateTime(2007,3,15);
    	Transactions[20]->ProcessedBy = 70594;
    	Transactions[20]->ProcessedFor = L"95-722947-93";
    	Transactions[20]->TransactionType = L"Withdrawal";
    	Transactions[20]->WithdrawalAmount = 200.00;
    
    	Transactions[21] = gcnew CAccountTransaction;
    	Transactions[21]->TransactionDate = DateTime(2007,3,26);
    	Transactions[21]->ProcessedBy = 39850;
    	Transactions[21]->ProcessedFor = L"17-490040-83";
    	Transactions[21]->TransactionType = L"Deposit";
    	Transactions[21]->DepositAmount = 1000.00;
    	
    	Transactions[22] = gcnew CAccountTransaction;
    	Transactions[22]->TransactionDate = DateTime(2007,4,4);
    	Transactions[22]->ProcessedBy = 74228;
    	Transactions[22]->ProcessedFor = L"58-405048-15";
    	Transactions[22]->TransactionType = L"Deposit";
    	Transactions[22]->DepositAmount = 126.85;
    
    	Transactions[23] = gcnew CAccountTransaction;
    	Transactions[23]->TransactionDate = DateTime(2007,4,6);
    	Transactions[23]->ProcessedBy = 85285;
    	Transactions[23]->ProcessedFor = L"92-318284-75";
    	Transactions[23]->TransactionType = L"Deposit";
    	Transactions[23]->DepositAmount = 250.00;
    
    	Transactions[24] = gcnew CAccountTransaction;
    	Transactions[24]->TransactionDate = DateTime(2007,4,6);
    	Transactions[24]->ProcessedBy = 70594;
    	Transactions[24]->ProcessedFor = L"62-384638-48";
    	Transactions[24]->TransactionType = L"Deposit";
    	Transactions[24]->DepositAmount = 400.00;
    
    	Transactions[25] = gcnew CAccountTransaction;
    	Transactions[24]->TransactionDate = DateTime(2007,4,12);
    	Transactions[24]->ProcessedBy = 94852;
    	Transactions[24]->ProcessedFor = L"95-722947-93";
    	Transactions[24]->TransactionType = L"Deposit";
    	Transactions[24]->DepositAmount = 100.00;
    
    	Transactions[25] = gcnew CAccountTransaction;
    	Transactions[25]->TransactionDate = DateTime(2007,4,30);
    	Transactions[25]->ProcessedFor = L"58-405048-15";
    	Transactions[25]->TransactionType = L"Monthly Charge";
    	Transactions[25]->ChargeAmount = 6.00;
    
    	Transactions[26] = gcnew CAccountTransaction;
    	Transactions[26]->TransactionDate = DateTime(2007,4,30);
    	Transactions[26]->ProcessedFor = L"62-384638-48";
    	Transactions[26]->TransactionType = L"Monthly Charge";;
    	Transactions[26]->ChargeAmount = 6.00;
    
    	Transactions[27] = gcnew CAccountTransaction;
    	Transactions[26]->TransactionDate = DateTime(2007,4,30);
    	Transactions[26]->ProcessedFor = L"92-318284-75";
    	Transactions[26]->TransactionType = L"Monthly Charge";
    	Transactions[26]->ChargeAmount = 6.00;
                    
    	Transactions[27] = gcnew CAccountTransaction;
    	Transactions[27]->TransactionDate = DateTime(2007,4,30);
    	Transactions[27]->ProcessedFor = L"17-490040-83";
    	Transactions[27]->TransactionType = L"Monthly Charge";
    	Transactions[27]->ChargeAmount = 6.00;
    
    	return Transactions;
        }
    };
  36. Access the first form and add a button to it
  37. Change the properties of the button as follows:
    (Name): btnEmployees
    Text:     Employees
  38. Double-click the Customers button
  39. In the top section of the file, include the Employee.h and the Employees.h header files
     
    #pragma once
    #include "Employee.h"
    #include "Employees.h"
    #include "AccountsRecords.h"
    
    namespace YugoNationalBank1 {
  40. Double-click the Employees button and implement its event as follows:
     
    System::Void btnEmployees_Click(System::Object^  sender, System::EventArgs^  e)
    {
        int i = 1;
        Employees ^ frmEmployees = gcnew Employees;
    	array<CEmployee ^> ^ StaffMembers = CAccountsRecords::GetEmployees();
    
        frmEmployees->lvwEmployees->Items->Clear();
    
        for each(CEmployee ^ empl in StaffMembers)
        {
            ListViewItem ^ lviEmployee = gcnew ListViewItem(i.ToString());
    
            lviEmployee->SubItems->Add(empl->EmployeeNumber.ToString());
            lviEmployee->SubItems->Add(empl->FirstName);
            lviEmployee->SubItems->Add(empl->LastName);
            lviEmployee->SubItems->Add(empl->FullName);
            lviEmployee->SubItems->Add(empl->Title);
            lviEmployee->SubItems->Add(empl->HourlySalary.ToString(L"F"));
    
            frmEmployees->lvwEmployees->Items->Add(lviEmployee);
            i++;
        }
    
        frmEmployees->ShowDialog();
    }
  41. Return to the Central form and a button to it
  42. Change the properties of the button as follows:
    (Name): btnCustomers
    Text:     Customers
  43. Double-click the Customers button
  44. In the top section of the file, include the Customer.h and the Customers.h header files
     
    #pragma once
    #include "Employee.h"
    #include "Employees.h"
    #include "AccountsRecords.h"
    #include "Customer.h"
    #include "Customers.h"
    #include "CustomersTransactions.h"
    
    namespace YugoNationalBank1 {
  45. Implement its event as follows:
     
    System::Void btnCustomers_Click(System::Object^  sender, System::EventArgs^  e)
    {
        int i = 1;
        Customers ^ frmCustomers = gcnew Customers;
        array<CCustomer ^> ^ Clients = CAccountsRecords::GetCustomers();
    
        frmCustomers->lvwCustomers->Items->Clear();
    
        for each (CCustomer ^ client in Clients)
        {
            ListViewItem ^ lviCustomer = gcnew ListViewItem(i.ToString());
    
            lviCustomer->SubItems->Add(client->CreatedBy);
            lviCustomer->SubItems->Add(client->DateCreated.ToShortDateString());
            lviCustomer->SubItems->Add(client->CustomerName);
            lviCustomer->SubItems->Add(client->AccountNumber);
            lviCustomer->SubItems->Add(client->AccountType);
            lviCustomer->SubItems->Add(client->AccountStatus);
    
            frmCustomers->lvwCustomers->Items->Add(lviCustomer);
            i++;
        }
    
        frmCustomers->ShowDialog();
    }
  46. Return to the Central form and a button to it
  47. Change the properties of the button as follows:
    (Name): btnTransactions
    Text:     Transactions
  48. Double-click the Transactions button and implement its event as follows:
     
    System::Void btnTransactions_Click(System::Object^  sender, 
    			System::EventArgs^  e)
    {
        int i = 1;
        CustomersTransactions ^ frmTransactions = gcnew CustomersTransactions();
        array<CAccountTransaction ^> ^ Transactions = 
    	CAccountsRecords::GetTransactions();
    
        frmTransactions->lvwTransactions->Items->Clear();
    
        for each(CAccountTransaction ^ ActTrans in Transactions)
        {
            ListViewItem ^ lviTransactions = gcnew ListViewItem(i.ToString());
    
            lviTransactions->SubItems->Add(
    		ActTrans->TransactionDate.ToString(L"dd-MMM-yyyy") );
    
            if(ActTrans->ProcessedBy == 0 )
                lviTransactions->SubItems->Add(L"");
            else
                lviTransactions->SubItems->Add(ActTrans->ProcessedBy.ToString());
    
            lviTransactions->SubItems->Add(ActTrans->ProcessedFor);
            lviTransactions->SubItems->Add(ActTrans->TransactionType->ToString());
    
            if(ActTrans->DepositAmount == 0)
                lviTransactions->SubItems->Add(L"");
            else
                lviTransactions->SubItems->Add(
    		ActTrans->DepositAmount.ToString(L"F"));
    
            if (ActTrans->WithdrawalAmount == 0)
                lviTransactions->SubItems->Add(L"");
            else
                lviTransactions->SubItems->Add(
    		ActTrans->WithdrawalAmount.ToString(L"F"));
    
            if (ActTrans->ChargeAmount == 0)
                lviTransactions->SubItems->Add(L"");
            else
                lviTransactions->SubItems->Add(
    		ActTrans->ChargeAmount.ToString(L"F"));
    
            frmTransactions->lvwTransactions->Items->Add(lviTransactions);
            i++;
        }
    
        frmTransactions->ShowDialog();
    }
  49. Execute the application to see the results
  50. Close the forms and return to your programming environment
 
 
   
 

Home Copyright © 2009-2016, FunctionX, Inc. Next