Creating Instances of the Employee Class

Creating Instances of the Employee Class

So far we've built a class and examined it with X-ray glasses, but we haven't run it yet. Let's run our program now.

  1. In the Division.vb tab in your code editor, add the following code right after the End Namespace line that encapsulates the class. The next section of code, Sub Main and the showEmployee subroutine, will be encapsulated within a module, a code file that contains functions. This module will provide a harness in which to test our simple class within the same code window. We will create two instances of the Division class. Each instance will use a different overloaded constructor. Then we will set properties for each instance and pass the objects to the showEmployee subroutine to be displayed.

    Public Module modmain          '"Main" is application's entry point     Sub Main()         Dim newEmp1 As New CompanyDivision.Division()         Dim newEmp2 As New _             CompanyDivision.Division("Marea", "Castaneda")                  With newEmp1             .FirstName = "Garrett"             .LastName = "Connell"             .Division = "MIS"             .MonthsInDivision = 18         End With                  showEmployee(newEmp1)                  newEmp2.Division = "Administration"         newEmp2.MonthsInDivision = 7                  If TypeOf newEmp2 Is _             CompanyDivision.Division Then             If newEmp2.Serialize = True Then                 showEmployee(newEmp2)             End If         End If                      End Sub     'This private subroutine will be passed a Division     'class and print the contents. Note that the     'signature shows it will take a     'CompanyDivision.Division data type as a parameter.          Private Sub showEmployee(ByRef currentEmployee As _         CompanyDivision.Division)         Dim sDisplayString As String                  sDisplayString = "Employee Name: " & _             currentEmployee.FirstName & _             " " & currentEmployee.LastName & crlf         sDisplayString += "Employee Division: " &  _             currentEmployee.Division & crlf         sDisplayString += "Employee Months: " &  _             currentEmployee.MonthsInDivision & crlf         sDisplaystring += "Employee Number: " &  _             currentEmployee.EmployeeNumber & crlf                  MessageBox.Show(sDisplayString, "Employee Scanner", _             MessageBoxButtons.OK, MessageBoxIcon.Information)              End Sub      End Module

  2. We have to instruct the compiler to start the program by using our new Sub Main. Select View | Solution Explorer, right-click the Employee assembly, and then select Properties from the menu. The Assembly name and the Root namespace should say Employee.

  3. Select Sub Main in the Startup object drop-down list. Your selection in this list tells the compiler to use Sub Main when the program starts. Click OK to close the dialog box, and then select Debug | Start to run our program. You'll see output like that shown in Figure 3-13.

    Figure 3-13

    The first employee's information.

The two instances of the Division class that we created are assigned to reference variables newEmp1 and newEmp2. The first instance calls the empty constructor, but the second passes in parameters for the first and last name. Because we inherited from Employee, MyBase is called to initialize the base class Employee.

Public Module modmain          '"Main" is application's entry point     Sub Main()         Dim newEmp1 As New CompanyDivision.Division()         Dim newEmp2 As New _             CompanyDivision.Division("Marea", "Castaneda")       

Next we set the properties of our class. The first two name properties are called in the parent class because we don't have implementations of FirstName or LastName in the Division class. The next two properties are implemented in the child class. After these properties are set, the showEmployee procedure is called. This procedure prints out the contents of the object. You can see that by setting properties we have communicated with both the child and parent classes.

With newEmp1     .FirstName = "Garrett"     .LastName = "Connell"     .Division = "MIS"     .MonthsInDivision = 18 End With showEmployee(newEmp1)

We now set the two Division properties in newEmp2. Because we instantiated this object by passing in the first and last name to the constructor, we only need to set Division and MonthsInDivision.

newEmp2.Division = "Administration" newEmp2.MonthsInDivision = 7

You'll often need to determine exactly which type of object you are dealing with. You can use the built-in TypeOf operator to check whether the run-time type of a value is compatible with a given type. In other words, the first operand (newEmp2) must be a reference type, and the second operand (CompanyDivision.Division) must be a type name. The TypeOf operator returns True if a conversion from the run-time type of the operand to the type exists.

If newEmp2 is of data type CompanyDivision.Division, the Serialize method is called. Because all methods in Visual Basic .NET are virtual, the child implementation is called. However, it so happens that our child implementation then calls the parent implementation of Serialize via MyBase. If all of these checks return True, we print the contents of this object.

If TypeOf newEmp2 Is CompanyDivision.Division Then     If newEmp2.Serialize = True Then         showEmployee(newEmp2)     End If End If

Of course, everything is as it should be, so the contents of newEmp2 are displayed successfully, as you can see in Figure 3-14.

Figure 3-14

The second employee's information.

This example shows that even though we instantiated two separate objects from the Division class, each object is an individual with its own values for its private data members. Under the hood, Visual Basic .NET uses only a single instance of the methods and properties of our classes, but it maintains separate member data variables.

The subroutine showEmployee takes in an object of type CompanyDivision.Division as a parameter. You can see that the subroutine is strongly typed—it will accept a parameter only of type CompanyDivision.Division. We dimension a local string variable and concatenate the various values of the object passed in as a parameter and display them.

Private Sub showEmployee(ByRef currentEmployee As _     CompanyDivision.Division)     Dim sDisplayString As String     sDisplayString = "Employee Name: " & _         currentEmployee.FirstName & _         " " & currentEmployee.LastName & crlf     sDisplayString += "Employee Division: " &  _         currentEmployee.Division & crlf     sDisplayString += "Employee Months: " &  _         currentEmployee.MonthsInDivision & crlf     sDisplaystring += "Employee Number: " &  _         currentEmployee.EmployeeNumber & crlf              MessageBox.Show(sDisplayString, "Employee Scanner", _         MessageBoxButtons.OK, MessageBoxIcon.Information)              End Sub

Notice the crlf (carriage return/line feed) characters at the end of each line. These characters are not provided by the .NET Framework but by the Microsoft.VisualBasic assembly. Whenever you create a new Visual Basic .NET program that uses features of the language, import the Microsoft.VisualBasic namespace.



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net