22 Object Oriented Programming Mark Dixon 1 Questions

22 – Object Oriented Programming Mark Dixon 1

Questions: Databases • How many primary keys? • How many foreign keys? Mark Dixon 3 2 2

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>" Mark Dixon 3

Questions: SQL in VB • Are these correct (assume variables and fields exist)? id = 4 sql = SELECT * FROM Customer sql = sql " WHERE [Cust. ID] = " + id + "; " cmd = New Old. Db. Command(sql, cn) Mark Dixon 4

Questions: Writing to Databases • What SQL command is used to add a new record to a database table. INSERT • What SQL command is used to remove a record from a database table. DELETE • Write an SQL command to put "Hello" into the Description field of all records in the Message table. UPDATE Message SET Description = ‘Hello’; Mark Dixon 5

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 Mark Dixon 6

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 Mark Dixon 7

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. • • Mark Dixon 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 8

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 Mark Dixon 9

Example: Animals Mark Dixon 10

Example: Student Mark Dixon 11

Public and Private • Control access to properties and methods Class a Public x As Single Private y As Single Public Sub Reset. Y() y = 0 End Sub End Class Dim b As New a this works (x is public) b. x = 5 b. Reset. Y() this works (Reset. Y is public) b. y = 10 this will fail (y is private) Mark Dixon 12

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 Mark Dixon 13

Example: Counter (html) <html> <head><title>Counter</title></head> <body> <form runat="server"> <input id="btn. Reset" type="submit" value="Reset" runat="server" /> <input id="btn. Up" type="submit" value="Up" runat="server" /> <input id="btn. Down" type="submit" value="Down" runat="server" /> <p id="par. Msg" runat="server"></p> </form> </body> </html> Mark Dixon 14

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 Counter. vb Public Class Counter Private m. Count As Long Public Function Get. Count() As L Get. Count = m. Count Sub btn. Reset_Click(s As Object, e As Event. Args) Handles btn. Reset. Server. Click End Function c. Reset() End Sub Public Sub Reset() m. Count = 0 Sub btn. Up_Click(s As Object, e As Event. Args) Handles btn. Up. Server. Click End Sub c. Up() End Sub Page_Load. Complete(s As Object, e As Event. Args) par. Msg. inner. Text = c. Get. Count() End Sub Class file must be in App_Code folder Mark Dixon Public Sub Up() m. Count = m. Count + 1 End Sub Public Sub Down() m. Count = m. Count - 1 End Sub End Class 15

. NET Folders • Right click project – App_Code – used for classes (put all classes here) – App_Data – used for databases Mark Dixon 16

Questions: OOP Public Class Counter Private m. Count As Long • How many – classes – properties – methods – functions – procedures Mark Dixon 1 Public Function Get. Count() As Long Get. Count = m. Count End Function 1 Public Sub Reset() m. Count = 0 End Sub 4 Public Sub Up() m. Count = m. Count + 1 End Sub 2 Public Sub Down() m. Count = m. Count - 1 End Sub End Class 3 Function Twice(x As Long) As Long Return x * 2 End Function 17

Class Diagrams • Used to describe structure of object classes: Class Name Module Code: string Title: string Get. Title(): string Set. Title(t: string) Count(): integer Mark Dixon Class Attributes/Properties Class Operations/Methods 18

Implementing Class Diagrams Module Code: String Title: String Get. Title(): string Set. Title(t: string) Count(): integer Class Module Public Code As String Public Title As String Public Function Get. Title() As String Public Sub Set. Title(t As String) Public Function Count() As Integer End Class Mark Dixon 19

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. Mark Dixon 20
- Slides: 20