/* _____________________________________ E M P L O Y E E C L A S S Ron Kessler Created 10/18/2014 _____________________________________ This class contains the properties and methods necessary to add/delete/display employees on the main form Updated 10/20/2014 */ #pragma once #include "General.h"; using namespace System; using namespace System::Windows::Forms; // so we can use messagebox using namespace System::Collections::Generic; //add this for clarification namespace WorkingWithLists { public ref class Employee sealed //sealed means this class cannot be inherited and saves a bit of processing so use it! { public: Employee(void) { } #pragma region Class Fields/Variables public: List^ employeeNames; //define list here and instantiate it later private: String^ _Name2Add; private: String^ _Name2Delete; #pragma endregion #pragma region Properties public: property String^ NewName { String^ get() { return _Name2Add; } void set(String^ value) { _Name2Add = value; } } public: property String^ DeleteName { String^ get() { return _Name2Delete; } void set(String^ value) { _Name2Delete = value; } } #pragma endregion #pragma region Load Initial Names public: bool LoadNames() { //see page 226 in Murach 2008 C++ to understand how lists autosize as you insert items //the list doubles it size as soon as the original size is exceeded. try { employeeNames = gcnew List(); employeeNames->Add("Dennis Miller"); employeeNames->Add("Jason Anderson"); employeeNames->Add("Nyguen Tran"); employeeNames->Add("Lee Wang"); employeeNames->Add("Andres Hernandez"); employeeNames->Add("Karen Lee"); employeeNames->Add("Dorothy the Dinosaur"); employeeNames->Add("Wags the Dog"); employeeNames->Add("Barney"); employeeNames->Add("Greg Wiggles"); employeeNames->Sort(); //sort them return true; } catch (Exception^ ex) { return false; } } #pragma endregion #pragma region Delete Person public: bool DeletePerson() { /*--- whichItem will get the index # just like in a Listbox control. RemoveAt is used to remove an item at a specified index #. */ try { int whichItem = employeeNames->BinarySearch(_Name2Delete); if (whichItem != -1) //we found it { employeeNames->RemoveAt(whichItem); return true; } else { General::ShowMSG("Person not found.", General::deleteMsgTitle); return false; } } catch (Exception^ ex) { General::ShowMSG("I could not delete person.", General::deleteMsgTitle); return false; } } #pragma endregion #pragma region Add New Employee public: bool AddNewPerson() { try { if(_Name2Add->Length > 0) employeeNames->Add(_Name2Add); return true; } catch (Exception^ ex) { General::ShowMSG("I could not add this person.", General::addMsgTitle); return false; } } #pragma endregion }; }