Download presentation
Presentation is loading. Please wait.
Published byEvan Ramsey Modified over 9 years ago
1
8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming
2
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.
3
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.
4
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
5
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 element Jack.LastName = “Woods” Jack.FirstName = “James” Jack.Age = 50
6
8-6 UDT Example continued ‘Declare another Person variable Dim Joe as Person ‘Assign one Person variable to another one Joe = Jack ‘This will display “James Woods” because the ‘assignment is done element by element MsgBox(Joe.FirstName & “ “ & Joe.LastName)
7
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
8
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 MarriedCouple Dim Him As Person Dim Her As Person Dim Chidren() As Person End Structure
9
8-9 Step-by-step 8-1: Working with UDTs Demo
10
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
11
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
12
8-12 Step-by-step 8-2: Vintage DVDs Payroll Demo
13
8-13 Step-by-step 8-3: The Employee Information Form Demo
14
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)
15
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
16
8-16 Example of a Class
17
8-17 Example of an Instance, an Object
18
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
19
8-19 Inheritance
20
8-20 Polymorphism
21
8-21 Namespaces Hierarchical naming scheme.Net Framework Class Library contains many classes – System – System.IO – System.Windows – System.Windows.Forms
22
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
23
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
24
8-24 ArrayList Properties
25
8-25 ArrayList Methods
26
8-26 Step-by-Step 8-4: Declaring and using Objects Demo
27
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
28
8-28 A Class Example: A Two-Dimensional Vector Begin Class Vector2D Private mX as Double Private 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
29
8-29 Two Constructors Public Sub New() mX = 0.0 mY = 0.0 End Sub Public Sub New( x As Double, y As Double) mX = x mY = y End Sub
30
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
31
8-31 Step-by-step 8-5: Creating a Class and Objects Demo
32
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.