TP Chapter 1 Exercise 1: Defining The Employee Class
TP Chapter 1 Exercise 1: Defining The Employee Class
class Employee
{
}
4. Enter the following code between the opening and closing brackets to add the private
instance variables to the class body in the definition file:
5. Next, add the following public properties to access the private instance variables defined in
step 4:
7. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
Exercise 2: Testing the Employee Class
To test the Employee class, follow these steps:
1. Open frmLogin in the code editor and locate the btnLogin click event code.
■Tip Double-clicking the Login button in the form designer will also bring up the event code
in the code editor.
2. In the body of the btnLogin click event, declare and instantiate a variable of type Employee
called oEmployee:
Employee oEmployee = new Employee();
3. Next, call the Login method of the oEmployee object, passing in the values of the login
name and the password from the text boxes on the form:
oEmployee.Login(txtName.Text,txtPassword.Text);
4. After calling the Login method, show a message box stating the user’s security level,
which is retrieved by reading the SecurityLevel property of the oEmployee object:
MessageBox.Show("Your security level is " + oEmployee.SecurityLevel);
5. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
6. Select Debug ➤ Start to run the project. Test the login form by entering a login name of
Smith and a password of is. You should get a message indicating a security level of 2. Try
entering your name and a password of pass. You should get a message indicating the login
failed.
After testing the login procedure, close the form; this will stop the debugger.
Exercise 3: Overloading Constructor
7. Add the following code to the overloaded constructor, which simulates extracting the
employee data from a database and assigns the data to the instance properties of the class:
8. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
Exercise 4: Testing the Employee Class Constructors
To test the Employee class constructors, follow these steps:
1. Open the EmployeeInfoForm in the form editor and double click the New Employee
button to bring up the click event code in the code editor.
2. In the Click Event method body, declare and instantiate a variable of type Employee called
oEmployee:
Employee
oEmployee = new Employee();
3. Next, update the EmployeeID text box with the employee ID, disable the EmployeeID text
box, and clear the remaining textboxes:
4. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
5. Open the EmployeeInfoForm in the form editor and double click the Existing Employee
button to bring up the click event code in the code editor.
6. In the Click Event method body, declare and instantiate a variable of type Employee called
oEmployee. Retrieve the employee ID from the txtEmpID text box and pass it as an argument
in the constructor. The int.Parse method converts the text to an integer data type:
Employee oEmployee = new Employee(int.Parse(txtEmpID.Text));
7. Next, disable the Employee ID textbox and fill in the remaining text boxes with the values
of the Employee object’s properties:
txtEmpID.Enabled = false;
txtLoginName.Text = oEmployee.LoginName;
txtPassword.Text = oEmployee.PassWord;
txtSSN.Text = oEmployee.SSN.ToString();
txtDepartment.Text = oEmployee.Department;
8. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
9. Select Debug ➤ Start to run the project and test the code.
10. When the EmployeeInfo form is displayed, click the New Employee button. You should
see that a new employee ID has been generated in the Employee ID textbox.
11. Click the Reset button to clear and enable the Employee ID text box.
12. Enter a value of 1 for the employee ID and click the Get Existing Employee button. The
information for the employee is displayed on the form.
13. After testing the constructors, close the form, which will stop the debugger.
Exercise 5: Overloading a Class Method
To overload a class method, follow these steps:
1. Open the Employee.cs code in the code editor.
2. Add the following Update method to the Employee class. This method simulates the
updating of the employee security information to a database:
3. Add a second Update method to simulate the updating of the employee human resources
data to a database:
4. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
Exercise 6: Testing the Overloaded Update Method
To test the overloaded Update method, follow these steps:
1. Open the EmployeeInfo Form in the Form editor and double click the Update SI button.
You are presented with the click event code in the Code Editor window.
2. In the Click Event method, declare and instantiate a variable of type Employee called
oEmployee. Retrieve the employee ID from the txtEmpID text box and pass it as an argument
in the constructor:
Employee oEmployee = new Employee(int.Parse(txtEmpID.Text));
3. Next, call the Update method, passing the values of the login name and password from the
text boxes. Show the method return message to the user in a message box:
MessageBox.Show(oEmployee.Update(txtLoginName.Text, txtPassword.Text));
4. Update the login name and password text boxes with the property values of the Employee
object:
txtLoginName.Text = oEmployee.LoginName;
txtPassword.Text = oEmployee.PassWord;
5. Repeat this process to add similar code to the Update HR button Click Event method to
simulate updating the human resources information. Add the following code to the Click
Event method: