mark dixon 1 22 – object oriented programming. mark dixon 2 questions: databases how many primary...

20
Mark Dixon 1 22 – Object Oriented Programming

Upload: osborne-ward

Post on 21-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 1

22 – Object Oriented Programming

Page 2: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 2

Questions: Databases• How many primary keys?• How many foreign keys?

32

PlantPlantID EnglishName ScientificName Price Toxic FileName

1 Foxglove Digitalis purpurea 2.5 TRUE Foxglove.jpg2 Daisy Bellis perennis 0.45 FALSE Daisy.jpg3 Hemlock Conium maculatum 8.79 TRUE Hemlock.jpg4 Marsh Mallow Althaea officinalis 3.25 FALSE MarshMallow.jpg5 Lords-and-Ladies Arum maculatum 2.25 TRUE Lords.jpg6 Wild Carrot Daucus carota 1.25 FALSE WildCarrot.jpg7 Bluebell Hyacinthoides non-scripta 1.8 FALSE Bluebell.jpg8 Common Poppy Papaver rhoeas 1.28 FALSE Poppy.jpg

OrderOrderID CustID PlantID Quantity Date Current

1 1 7 10 14-Mar-06 TRUE2 2 5 2 14-Mar-06 TRUE3 1 3 1 14-Mar-06 FALSE5 2 4 4 14-Mar-06 FALSE

46 1 2 9 09-Jun-06 FALSE

CustomerCustID Surname Forenames email Password

1 Dixon Mark [email protected] a2 Jones Sally [email protected] sally

Page 3: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 3

Questions: HTML in VB• Are these correct (assume variables and

fields exist)?

s = s + <td> + r("Model")

s = s r("Length")

h = "<div>" + h + "</div>"

Page 4: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 4

Questions: SQL in VB• Are these correct (assume variables and

fields exist)?

id = 4

sql = SELECT * FROM Customer

sql = sql " WHERE [CustID] = " + id + ";"

cmd = New OldDbCommand(sql, cn)

Page 5: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 5

Questions: Writing to Databases• What SQL command is used to add a new

record to a database table.

• What SQL command is used to remove a record from a database table.

• Write an SQL command to put "Hello" into the Description field of all records in the Message table.

INSERT

DELETE

UPDATE Message SET Description = ‘Hello’;

Page 6: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 6

Session Aims & Objectives• Aims

– To highlight that the object oriented techniques covered earlier can be used in ASP

• Objectives,by end of this week’s sessions, you should be able to:

– create a class definition in server-side code– create an instance of a class– create a class definition from a class diagram

Page 7: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 7

Object-Oriented Paradigm• A program is made up of a number of objects that

communicate with each other by passing messages

• Each object contains– attributes/properties that represent its state, and– operations/methods that represent its behaviour

• Objects often mirror the real world– Customers– Students– Patients

Page 8: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 8

Classes and Instances• Object Classes

– general descriptions of types of objects,e.g. student, product, customer, lecturer, and room.

• Object Instances– specific items of a given class, e.g.

• each of you could be an instance of the student class• Room 214 could be an instance of the room class• I could be an instance of the lecturer class• Bolt could be an instance of the part class

Page 9: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 9

Object Concepts - Implementation

• Properties – implemented as– data structures (variables and arrays)

• Methods – implemented as either– a procedure (to perform some processing), or– a function (to return a value).

• Object oriented paradigm builds on (rather than replaces) the structured paradigm

Page 10: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 10

Example: Animals

Page 11: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 11

Example: Student

Page 12: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 12

Public and Private• Control access to properties and methods

Class a Public x As Single Private y As Single

Public Sub ResetY() y = 0 End SubEnd Class

Dim b As New a b.x = 5 b.ResetY() b.y = 10

this works (x is public) this works (ResetY is public) this will fail (y is private)

Page 13: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 13

Benefits of OOP in code• Procedures and Functions are part of object

– encapsulation

• Related Data and Operations together

• Private keyword – restrict access to data

• Clearer code

• Less prone to error

Page 14: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 14

Example: Counter (html)<html> <head><title>Counter</title></head> <body> <form runat="server"> <input id="btnReset" type="submit" value="Reset" runat="server" /> <input id="btnUp" type="submit" value="Up" runat="server" /> <input id="btnDown" type="submit" value="Down" runat="server" /> <p id="parMsg" runat="server"></p> </form> </body></html>

Page 15: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 15

Example: Counter (code)Dim c As Counter

Sub Page_Load() If Session("c") Is Nothing Then Session("c") = New Counter End If c = Session("c") End Sub

Sub btnReset_Click(s As Object, e As EventArgs) Handles btnReset.ServerClick c.Reset() End Sub

Sub btnUp_Click(s As Object, e As EventArgs) Handles btnUp.ServerClick c.Up() End Sub

Sub Page_LoadComplete(s As Object, e As EventArgs) parMsg.innerText = c.GetCount() End Sub

Public Class Counter Private mCount As Long

Public Function GetCount() As Long GetCount = mCount End Function

Public Sub Reset() mCount = 0 End Sub

Public Sub Up() mCount = mCount + 1 End Sub

Public Sub Down() mCount = mCount - 1 End SubEnd Class

Counter.vb

Class file must be in App_Code folder

Page 16: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 16

.NET Folders• Right click project

– App_Code – used for classes (put all classes here)– App_Data – used for databases

Page 17: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 17

Questions: OOP• How many

– classes

– properties

– methods

– functions

– procedures

Public Class Counter Private mCount As Long

Public Function GetCount() As Long GetCount = mCount End Function

Public Sub Reset() mCount = 0 End Sub

Public Sub Up() mCount = mCount + 1 End Sub

Public Sub Down() mCount = mCount - 1 End SubEnd Class

Function Twice(x As Long) As Long Return x * 2End Function

1

1

4

2

3

Page 18: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 18

Class Diagrams• Used to describe structure of object classes:

Module

Code: stringTitle: string

GetTitle(): stringSetTitle(t: string)Count(): integer

Class Attributes/Properties

Class Operations/Methods

Class Name

Page 19: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 19

Class Module Public Code As String Public Title As String

Public Function GetTitle() As String Public Sub SetTitle(t As String) Public Function Count() As IntegerEnd Class

Implementing Class Diagrams

Module

Code: StringTitle: String

GetTitle(): stringSetTitle(t: string)Count(): integer

Page 20: Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 20

Tutorial Exercise: Counter• Task 1: Get the Counter example from the lecture

working.• Task 2: Modify your code – so that the value

cannot go below 0 or above 10. hint: you can't actually stop it going outside the range, but you can detect if it does and then change it.