/*_____________________________________________________________ C S 1 3 1 W O R K I N G W I TH S T R U C T S Ron Kessler ______________________________________________________________ */ /*This project introduces you structures which used to be called "User-defined types" which is a much better description. Before the advent of relational databases, these data structures were very handy and made it easy to handle related pieces of data. This demo shows how to create a data structure based upon an employee. Instead of creating variables for first, last, id, etc., I will create a new data type and give it a name. I can then use it like any other data type in.net. */ //***********START OF MY CODE**************** //---create my new data structure value struct Payroll //value structs are created on the stack which speeds up cleanup! { int id; //---members of the structure String^ firstName; String^ lastName; double hourlyRate; String^ division; }; //---create variable of Payroll type Payroll myEmployee; private: System::Void btnCalc_Click(System::Object^ sender, System::EventArgs^ e) { //---validate all textboxes for nulls & calculate if valid if(ValidateForm()) { //---input the data myEmployee.id = int::Parse(txtID->Text->Trim()); myEmployee.firstName= txtFirst->Text->Trim(); myEmployee.lastName= txtLast->Text->Trim(); myEmployee.division = txtDivision->Text->Trim(); myEmployee.hourlyRate = double::Parse( txtPayGrade->Text->Trim()); //---process it int hours =int::Parse( txtHours->Text->Trim()); double grossPay = ComputePay(myEmployee.hourlyRate, hours); //---display result lblGrossPay->Text = grossPay.ToString("n2"); } } //************HELPER METHODS************** private:double ComputePay(double payRate, int hours) { return payRate * hours; } private: bool ValidateForm() { for each(Control^ myControl in this->Controls) if(myControl->GetType() ==TextBox::typeid) if(myControl->Text->Trim() == "") { MessageBox::Show("Please enter all data.", "Payroll Dept.", MessageBoxButtons::OK, MessageBoxIcon::Error); return false; } return true; }