Designing, Creating, and Using a Class - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

Designing, Creating, and Using a Class

Description:

Designing, Creating, and Using a Class – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 50
Provided by: steve1742
Category:

less

Transcript and Presenter's Notes

Title: Designing, Creating, and Using a Class


1
Chapter 9
  • Designing, Creating, and Using a Class

2
Objectives
  • Understand when to create a class when designing
    a solution
  • Design a class using the UML
  • Create a new class in Visual Basic .NET
  • Create a constructor for a class
  • Create a property for a class
  • Code a Get property procedure

3
Objectives
  • Code a Set property procedure
  • Create a method for a class
  • Create a class by using inheritance
  • Draw a UML object diagram for an object
  • Use a class in a Visual Basic .NET program

4
Program Development
5
Program Development
6
Program Development
7
Program Development
8
Starting Visual Basic .NET and Starting a Class
Library Project
  • Insert the Data Disk in drive A
  • Start Visual Basic .NET. When the Start Page
    appears, click the New Project button on the
    Start Page
  • When the New Project dialog box appears, click
    the Class Library icon in the Templates area.
    Double-click the text, ClassLibrary1, in the Name
    box. Type Compensation Review in the Name box
  • Click the Browse button. If necessary, click 3½
    Floppy (A) in the Look in box. Click Chapter9
    and then click the Open button
  • Click the OK button and Maximize the Visual Basic
    .NET window. If necessary, close the Output
    window

9
Declaring Private Variables in a Class
  • Right-click the Class1.vb class in the Solution
    Explorer window. Click the Rename command on the
    shortcut menu, type Employee.vb as the new module
    name, and then press the ENTER key
  • If necessary, right-click the Employee.vb class
    in the Solution Explorer window and then click
    the View Code command on the shortcut menu
  • Enter Option Strict On as the first line of code
    in the code window. Replace the class name,
    Class1, by typing the class name, Employee, on
    line 2
  • Type lines 3 through 10 as shown on the following
    slide

10
Declaring Private Variables in a Class
11
Creating a Constructor for a Class
  • Enter lines 12 through 20, as shown below

12
Adding a Property to a Class
13
Coding a Get Property Procedure
14
Adding a Property to a Class and Coding a Get
Property Procedure
  • Enter line 23, followed by line 25, as shown
    below

15
Coding a Set Property Procedure
16
Coding a Set Property Procedure
  • Change the variable name, Value, to intEmployeeID
    on line 27
  • Enter line 28 as shown below

17
Coding the Remaining Properties
  • Unpin the Toolbox window from the main work area
    in order to display more code in the main work
    area. Enter lines 32 through 93 as shown on the
    following slide

18
(No Transcript)
19
Coding a Method for a Class
  • Enter lines 95 through 100 as shown below

20
Adding a Class to a Class Library
  • Right-click the Compensation Review project in
    the Solution Explorer window. When the shortcut
    menu appears, point to the Add command
  • Click the Add Class command on the Add menu.
    When the Add New Item Compensation Review
    dialog box appears, select the Class1.vb text in
    the Name box and type HourlyEmployee.vb as the
    new name
  • Click the Open button

21
Adding a Class to a Class Library
22
Inheriting from a Base Class
23
Inheriting from a Base Class
  • Enter Option Strict On as the first line of code
    in the code window. Click line 3 and then type
    the line of code shown below

24
Creating a Private Variable for a Property in a
Subclass
  • Enter line 5 as shown below

25
Creating a Constructor and a Property in a
Subclass
  • Type lines 7 through 10, 12, and 14-17, as shown
    below

26
Creating an Additional Subclass
  • Right-click the Compensation Review project in
    the Solution Explorer window. When the shortcut
    menu appears, click the Add command
  • Click the Add Class command on the Add menu.
    When the Add New Item Compensation Review
    dialog box appears, select the Class1.vb text in
    the Name box and type SalariedEmployee.vb as the
    new name
  • Click the Open button. When the
    SalariedEmployee.vb code window appears, enter
    the code on the following slide

27
Creating an Additional Subclass
28
Building, Saving, and Documenting the Class
Library
  • Right-click the Compensation Review project in
    the Solution Explorer window. When the shortcut
    menu appears, click the Build command
  • Click the Employee.vb tab and then use the Print
    command on the File menu to print a record of the
    code for the Employee class
  • Click the HourlyEmployee.vb tab and then use the
    Print command on the File menu to print a record
    of the code for the HourlyEmployee class
  • Click the SalariedEmployee.vb tab and then use
    the Print command on the File menu to print a
    record of the code for the SalariedEmployee class
  • Click the Close Solution command on the File menu
    to close the Compensation Review solution

29
Using a Class in Code
30
Starting a New Project and Creating the User
Interface
  • If necessary, click the Show Start Page command
    on the Help menu. When the Start Page appears,
    click the New Project button on the Start Page
  • When the New Project dialog box appears, if
    necessary, click Create directory for Solution.
    Click the Windows Application icon in the
    Templates area. Double-click the text,
    WindowsApplication1, in the Name box. Type
    EmployeeClassTest in the Name box
  • Click the Browse button. If necessary, click 3½
    Floppy (A) in the Look in box. Click Chapter9
    and then click the Open button

31
Starting a New Project and Creating the User
Interface
  • Click the OK button. If necessary, close the
    Output window
  • With Form1 selected, set the properties of the
    Form1 form as specified on the following slide

32
Starting a New Project and Creating the User
Interface
33
Starting a New Project and Creating the User
Interface
  • Add and then set the property values of the
    controls as listed in Table 9-9 on pages VB 9.54
    through VB 9.57. Add the three RadioButton
    controls to the first GroupBox control. Add the
    TextBox control and the two NumericUpDown
    controls to the second GroupBox control, as shown
    on the following slide

34
Starting a New Project and Creating the User
Interface
35
Adding a Reference to a Class
  • Double-click the Employee Object radio button on
    the Form1 form. When Visual Basic .NET creates
    the radEmployeeObject_CheckedChanged event
    procedure, right-click the References folder in
    the Solution Explorer window
  • Click the Add References command on the
    References shortcut menu
  • Click the Browse button
  • Use the Look in box to navigate to the
    A\Chapter9\Compensation Review\Compensation
    Review\bin folder. If necessary, click the
    Compensation Review.dll component to select it
  • Click the Open button

36
Adding a Reference to a Class
  • Click the OK button. If necessary, expand the
    References folder to view the references in the
    EmployeeClassTest project

37
Importing a Namespace
38
Importing a Namespace
  • Enter line 1 of the code as shown below. Type
    the keyword, Imports, followed by a space
  • Double-click Compensation_Review

39
Creating Objects from Classes
  • Type the first four words of line 196 as shown
    below, followed by a space. If necessary, scroll
    the Intellisense list to the Employee keyword
  • Double-click Employee. Enter lines 197 and 198
    as shown below

40
Getting Property Values of an Object in Code
  • Enter lines 201 through 207 as shown on the
    following slide
  • Click the Form1.vbDesign tab in the main work
    area. Double-click the HourlyEmployee Object
    radio button on the Form1 form. When Visual
    Basic .NET creates the radHourlyEmployeeObject_Che
    ckedChanged event procedure, enter lines 211-217
    as shown on the following slide
  • Click the Form1.vbDesign tab in the main work
    area. Double-click the SalariedEmployee Object
    radio button on the Form1 form. When Visual
    Basic .NET creates the radSalariedEmployeeObject_C
    heckedChanged event procedure, enter lines
    221-227 as shown on the following slide

41
Getting Property Values from an Object in Code
42
Setting Property Values of an Object in Code
  • Click the Form1.vbDesign tab in the main work
    area. Double-click the Set Properties button on
    the Form1 form. When Visual Basic .NET creates
    the btnSetProperties_Click event procedure, enter
    lines 231 through 240 as shown on the following
    slide

43
Setting Property Values of an Object in Code
44
Saving and Testing the Project
  • Click the Save All button on the Standard toolbar
  • Click the Start button on the Visual Basic .NET
    Standard toolbar. When the EmployeeClassTest
    application starts, minimize the Visual Basic
    .NET window. Type Sandy Grant in the Name text
    box in the EmployeeClassTest application window
    and then click the Set Properties button
  • Click the HourlyEmployee Object radio button.
    Type Randolph Jefferson in the Name text box.
    Type 10.25 as the Hourly rate. Click the Set
    Properties button
  • Click the Employee Object radio button
  • Type Emerson Grant in the Name box and click the
    Set Properties button

45
Saving and Testing the Project
  • Click the SalariedEmployee Object radio button.
    Type Lisa Land as the Name and type 75,500 as the
    Annual Salary. Click the Set Properties button
  • Continue changing the properties of the three
    objects and making certain that the proper values
    appear as each radio button is selected, until
    you are satisfied that the application works as
    designed and you understand how the objects and
    properties relate to what is displayed on the
    screen
  • Click the Close button on the EmployeeClassTest
    application window title bar. If necessary,
    click the Visual Basic .NET taskbar button to
    display the Visual Basic .NET window. If
    necessary, close the Output window

46
Documenting the Application and Quitting Visual
Basic .NET
  • Click the Form1.vb Design tab, and then use the
    PRINT SCREEN key to print a record of the user
    interface design of the Form1 form
  • Click the Form1.vb tab. Click File on the menu
    bar and then click Page Setup
  • When the Page Setup dialog box appears, if
    necessary, click Line numbers and then click the
    OK button
  • Use the Print command on the File menu to print a
    record of the code for the EmployeeClassTest
    application
  • Click the Visual Basic .NET Close button

47
Summary
  • Understand when to create a class when designing
    a solution
  • Design a class using the UML
  • Create a new class in Visual Basic .NET
  • Create a constructor for a class
  • Create a property for a class
  • Code a Get property procedure

48
Summary
  • Code a Set property procedure
  • Create a method for a class
  • Create a class by using inheritance
  • Draw a UML object diagram for an object
  • Use a class in a Visual Basic .NET program

49
Chapter 9 Complete
Write a Comment
User Comments (0)
About PowerShow.com