Become Joomla Developer

Objects Oriented Programming Fundamental (OOP)

Creating the Employee Class

In this activity, you will become familiar with the following:

  • Creating a VB class definition file using VS
  • Creating and using an instance of the class from VB client code

Note: If you have not already done so, download the starter files from the Source Code area of the Apress

Defining the Employee Class

To create the Employee class, follow these steps:

  1. Start VS. Select File > Open > Project
  2. Navigate to the Activity6_1Starter folder, click the Act6_1.sln file, and click Open. When the project opens, it will contain a login form. You will use this form later to test the Employee class you create.
  3. Select Project > Add Class. In the Add New Item dialog box, rename the class file to Employee.vb, and then click Open. VS adds the Employee.vb file to the project and adds the following class definition code to the file:
    Public Class Employee
    End Class
    
  4. Enter the following code to add the private instance variables to the class definition file:
    Private _empID As Integer
    Private _loginName As String
    Private _password As String
    Private _securityLevel As Integer
    
  5. Add the following public properties to access the private instance variables defined in step 4:
    Public ReadOnly Property EmployeeID() As Integer
        Get
            Return _empID
        End Get
    End Property
    
    Public Property LoginName() As String
        Get
            Return _loginName
        End Get
        Set(ByVal Value As String)
            _loginName = Value
        End Set
    End Property
    Public Property Password() As String
        Get
            Return _password
        End Get
        Set(ByVal Value As String)
            _password = Value
        End Set
    End Property
    Public ReadOnly Property SecurityLevel() As Integer
    Get
    Return _securityLevel
    End Get
    End Property
    
  6. Add the following Login method to the class definition:
    Public Sub Login(ByVal loginName As String, ByVal password As String)
        LoginName = loginName
        Password = password
        'Data normally retrieved from database.
        'Hardcoded for demo only
        If loginName = "Raja" And password = "pheak" Then
            _empID = 1
            _securityLevel = 2
        ElseIf loginName = "Son" And password = "Khunpheakdey" Then
            _empID = 2
            _securityLevel = 4
        Else
            Throw New Exception("Login incorrect.")
        End If
    End Sub
    
  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.

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. Declare and instantiate a variable of type Employee called oEmployee:
    Dim oEmployee As Employee = New Employee()
  3. 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. Show a message box stating the user’s security level retrieved by reading the SecurityLevel property of the oEmployee object:
    MessageBox.Show ("You have security clearence level " & oEmployee.SecurityLevel.ToString)
  5. Select File > Save All
  6. Select Build > Build Solution. Make sure there are no build errors in the Error List window. If there are, fix them, and then rebuild.
  7. Select Debug > Start to run the project. Test the login form by entering a login name of Raja and a password of pheak. 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.
  8. After testing the login procedure, close the form, which, in turn, will stop the debugger.

Suggestion | Contact US | About US | Term of Use | Privacy Policy | Advertiseing