8-1 chapter 8 using user-defined data types and object oriented programming

32
8-1 Chapter 8 Using User- Defined Data Types and Object Oriented Programming

Upload: evan-ramsey

Post on 29-Dec-2015

231 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-1

Chapter 8

Using User-Defined Data Types and Object Oriented Programming

Page 2: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-2

Learning Objectives

Understand User-defined Data Types (UDTs) and their Relationship to Object Oriented Programming (OOP).

Create and use your own UDTs.

Understand and discuss the basic concepts of Object Oriented Programming.

Page 3: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-3

Learning Objectives (continued)

Describe the basics of the .Net framework and namespaces.

Work with ArrayLists and HashTables

Create and use your own object classes.

Page 4: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-4

User Defined Data Types

User defined data types (UDT’s) allows you to group together values of different types into a single variable

Ex: Imagine a Person data type that would contain a String to hold the first name, another string to hold the last name and an integer to hold the age

Page 5: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-5

UDT Example

Structure Person Dim LastName As String Dim FirstName As String Dim Age As Integer End Structure

‘Declare a variable of type Person

Dim Jack As Person‘Assign values to each elementJack.LastName = “Woods”Jack.FirstName = “James”Jack.Age = 50

Page 6: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-6

UDT Example continued

‘Declare another Person variableDim Joe as Person

‘Assign one Person variable to another oneJoe = Jack

‘This will display “James Woods” because the ‘assignment is done element by elementMsgBox(Joe.FirstName & “ “ & Joe.LastName)

Page 7: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-7

The With Statement

Use With … End With syntax to avoid repeating the variable every time you access a element of it

With Joe

MsgBox(“FirstName: “ & .FirstName)

MsgBox(“LastName: “ & .LastName)

MsgBox(“Age: “ & .Age)

End with

Page 8: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-8

More details about UDT’s

UDT’s can contain procedures – Sub’s and/or Function’s

You can create arrays of UDT’s– Dim Crowd() As Person ‘ an array of persons

Individual elements of UDT’s can be UDT’s

Structure MarriedCoupleDim Him As PersonDim Her As PersonDim Chidren() As Person

End Structure

Page 9: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-9

Step-by-step 8-1: Working with UDTs

Demo

Page 10: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-10

Sorting an Array of User Defined Types

The algorithm is the same as for simple types Needed

– Comparison: will depend on the UDT’s– Swap: same as for simple types

Page 11: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-11

Sorting Employees()

Sub procedure to sort the Employees array

Sub SortEmp() Dim Counter, NextToLast As Integer Dim NotSwitched As Boolean, Temp As EmpRecord NotSwitched = False

Do Until NotSwitched NotSwitched = True

For Counter = 0 To EmpCntr - 1

If Employees(Counter). LName > Employees(intCounter + 1).LName Then Temp = Employees(intCounter) Employees(Counter) = Employees(Counter + 1) Employees(Counter + 1) = Temp NotSwitched = False End If

Next Loop

End Sub

Page 12: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-12

Step-by-step 8-2: Vintage DVDs Payroll

Demo

Page 13: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-13

Step-by-step 8-3: The Employee Information Form

Demo

Page 14: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-14

Object Oriented Programming

Object: a self-contained module that can combine data and program code and that cooperates in the program by passing strictly defined messages to one another.

Objects have– Properties (characteristics, information, nouns)– Methods (capabilities, actions, verbs)– Events (notification messages)

Page 15: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-15

The Class: Blueprint for objects

To create object in code, you first need a Class module

The class is the cookie cutter, the object is the cookie

An object is an instance of a Class, i.e. created according to the blueprint provided by the Class

Page 16: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-16

Example of a Class

Page 17: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-17

Example of an Instance, an Object

Page 18: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-18

Pillars of Object Oriented Programming

Encapsulation– If you know a class or object provides a certain functionality,

you don’t need to know how it does it– An object is a black box

Inheritance– Allows to specialize a class, (adding more properties or

methods) without having to modify base class or rewrite code that is the same

Polymorphism– Ability to interact with different classes in similar way. Ask a

dog to walk, ask a cow to walk. Different classes, same message

Page 19: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-19

Inheritance

Page 20: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-20

Polymorphism

Page 21: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-21

Namespaces

Hierarchical naming scheme .Net Framework Class Library contains many

classes– System– System.IO– System.Windows– System.Windows.Forms

Page 22: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-22

More on namespaces

To avoid having to write the full name of a class, (including namespace) you can “import” a namespace by using the Imports keyword

– Imports System.Windows.Forms– This must be put at the top of a module outside all class

code

You create you own namespace when you create a project, it is the “root namespace” for your code

Page 23: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-23

Collection types of Classes

The Collection namespace contains many classes with specific uses

ArrayList– allows you to create objects that act like smart arrays– Easy to resize

HashTable– Similar to ArrayList, but you give each element that you

store a unique name.– Easy to search

Page 24: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-24

ArrayList Properties

Page 25: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-25

ArrayList Methods

Page 26: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-26

Step-by-Step 8-4: Declaring and using Objects

Demo

Page 27: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-27

Creating a Class

1. Add a Class module to the project.2. Declare local variables to be used in the module.3. Initialize the Class properties of the object by

creating a constructor.4. Write the statements necessary to enable the class

to have values assigned to its properties or to assign its properties to variables outside of the object.

5. Write the methods that will carry out processing within the class.

6. Save the class module

Page 28: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-28

A Class Example:A Two-Dimensional Vector

Begin Class Vector2DPrivate mX as DoublePrivate mY as Double

Public Property X() As Double Get

Return mX End Get Set(ByVal Value As Double)

mX = Value End Set End Property

::

End Class

Page 29: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-29

Two Constructors

Public Sub New()mX = 0.0mY = 0.0

End Sub

Public Sub New( x As Double, y As Double)mX = xmY = y

End Sub

Page 30: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-30

A Method

Public Function Add( v As Vector2D ) As Vector2D

Dim vTemp as New Vector2D(0,0)

vTemp.X = mX + v.X

vTemp.Y = mY + v.Y

Return vTemp

End Function

Page 31: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-31

Step-by-step 8-5: Creating a Class and Objects

Demo

Page 32: 8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming

8-32

Copyright 2004 John Wiley & Sons, Inc.

All rights reserved. Reproduction or translation of this work beyond that permitted in section 117 of the 1976 United States Copyright Act without express permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages caused by the use of these programs or from the use of the information herein