mark dixon page 1 21 – object oriented programming in asp
Post on 21-Dec-2015
230 views
TRANSCRIPT
Mark Dixon Page 1
21 – Object Oriented Programming in ASP
Mark Dixon Page 2
Questions: HTML in VB• Are these correct (assume variables and
fields exist)?
s = s + <td> + rs.Fields("Model").value
s = s rs.Fields("Length").value
h = "<div>" + h + "</div>"
Mark Dixon Page 3
Questions: SQL in VB• Are these correct (assume variables and
fields exist)?
id = 4
sql = SELECT * FROM Customer
sql = sql " WHERE [CustID] = " + id + ";"
rs.Open(sql, cs)
Mark Dixon Page 4
Questions: Writing to Databases• Write a line of VB code to add a new record
to a recordset called rs.
• Write a line of VB code to remove the current record from a recordset called rs.
• Write a line of VB code to put "Hello" into a field called Message in the current record
rs.AddNew()
rs.Delete()
rs.Fields("Message").Value = "Hello"
Mark Dixon Page 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 Page 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 Page 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.
• 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
Mark Dixon Page 8
Object Concepts - Implementation
• Properties – implemented as– data structures (variables, arrays, and types).
• 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 Page 9
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
Mark Dixon Page 10
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 Page 11
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
Mark Dixon Page 12
Public and Private• Control access to properties and methods
Class a Public x As Single Private y As SingleEnd Class
Dim b As New a b.x = 5 b.y = 10
this works (x is public) this will fail (y is private)
Mark Dixon Page 13
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>
Mark Dixon Page 14
Example: Counter (code)<script language="VB" runat="server" src="Counter.vb"></script><script language="VB" runat="server">Dim c As Object
Sub Page_Load() If Session("c") Is Nothing Then Session("c") = New Counter Else c = Session("c") If Request.Form("btnReset") > "" Then c.Reset() ElseIf Request.Form("btnUp") > "" Then c.Up() ElseIf Request.Form("btnDown") > "" Then c.Down() End If parMsg.innerText = c.GetCount() End IfEnd Sub</script>
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
Mark Dixon Page 15
Questions: OOP• How many
– classes
– properties
– methods
– functions
– procedures
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
1
1
4
1
3
Mark Dixon Page 16
Object Associations• In practice projects will be made of
– many object classes– that interact with each other (are associated)
• There are several types of association• One of the most often used is the ‘part of’
association,– where one object class forms part of another
object class
• A common example of this occurs where it is necessary to store multiple instances of a class
Mark Dixon Page 17
Example: Bar (Analysis)
• Scenario 1: small project, limited automation
–Nouns: drinks, order, cost
–Verbs: describe, calculate cost
The students' Union bar needs a computer system for recording the purchase of drinks. Typically, a student will stagger to the bar and describe their order, consisting of one or (usually) more drinks.The bar staff will then prepare the drinks and calculate the cost of the order.
Mark Dixon Page 18
Example: Bar (Design v1)• Object Classes, properties, and methods
– Class diagram:
Order
mDrinks(): Drink
Add(string, long)Remove(long)Display (): StringCost(): double
Drink
mType: StringmQty: Long
Mark Dixon Page 19
Example: Bar (Drink module)• Drink (class module):
Class Drink Public mType As String Public mQty As LongEnd Class
Drink
mType: StringmQty: Long
Mark Dixon Page 20
Example: Bar (Order module)
Class Order Private mDrinks(9) As Drink
Public Sub Add(tmpType As String, tmpQty As Long) Dim d As Long ' Find free slot. For d = 0 To 9 If mDrinks(d) Is Nothing Then Exit For End If Next
' Create object and store data. mDrinks(d) = New Drink mDrinks(d).mType = tmpType mDrinks(d).mQty = tmpQty End SubEnd Class
Order
mDrinks(): Drink
Add(string, long)Remove(long)Display (): StringCost(): double
Mark Dixon Page 21
Example: Bar (Order module)• method (procedure) to display order's drinks
in a list boxPublic Function Display() As StringDim d As LongDim tmpStr As String tmpStr = "" For d = 0 To 9 If Not (mDrinks(d) Is Nothing) Then tmpStr = tmpStr & mDrinks(d).mQty & " " tmpStr = tmpStr & mDrinks(d).mType End If Next Display = tmpStrEnd Function
Order
mDrinks(): Drink
Add(string, long)Remove(long)Display (): StringCost(): double
Mark Dixon Page 22
Example: Bar (Order module)• method (procedure) to remove drink from
orderPublic Sub Remove(d As Long) mDrinks(d) = NothingEnd Sub
Order
mDrinks(): Drink
Add(string, long)Remove(long)Display (ListBox)Cost(): double
Mark Dixon Page 23
Tutorial Exercise: Bar• Task 1: Get the Bar example from the lecture working.• Task 2: Modify your code – add code to calculate the cost
of the order. This object method is not in the lecture notes – you need to create it (not necessarily on your own – discuss it with others, feel free to ask me for help).
• Task 3: What happens if the user tries to add more than 10 drinks? Modify your code to cope with this (you decide how it should respond).
• Task 4: What happens if the user tries to add a drink when none is selected (in the drinks list box)? Modify your code to cope with this.
• Task 5: What happens if the user tries to remove a drink when none is selected (in the order list box)? Modify your code to cope with this.
• Task 6: Modify your code – so that the cost is continuously calculated and there is no need for the cost button.