Classes in ASP

Classes in ASP

-

-

-

<%
Option Explicit

Class MyClass
'
' Properties (local variables)
'  - You can make them public, but that is insecure and stupid.
'
Private i_MyData ' Used internally, but given secured access via Property Let/Get
Private s_String     ' Only used internally
'
'-------------------------------
' Property Accessots
' - These are the rules for i_MyData (Let/Get)
'
Public Property Let MyData(valueIn)
'
' valueIn is the data given to property (as in... property = valueIn)
'
If valueIn>777 Then valueIn = 777
If valueIn<111 Then valueIn = 111
'
i_MyData = valueIn
End Property
Public Property Get MyDat
'
' This allows data to be retrived [as in... Response.write(myObj.MyData) ]
'
MyData = i_MyData
End Property

'
'-------------------------------
' Constructors/Deconstructors/
'  - These fire when creating/destroying object
'
Private Sub Class_Initialize
'
' Use this to set local variables..
'
i_MyData = 777 ' Monkeys gone to heaven...
s_String = "No data here... "
'
Response.Write("myClass has started<br>")
End Sub
'
Private Sub Class_Terminate
'
' Use this to clean up
'
Response.Write("myClass has stopped<br>")
End Sub

'-------------------------------
' Methods
'
Public Sub DoSomething()
'
' internal Sub (called a method)
'
Response.Write(s_String & i_MyData & "<br>")
End Sub

Public Function NonsenseMaths(NumberIn)
'
' internal Function (also called a method)
'
If NumberIn>20 Then NumberIn = 20
If NumberIn<5  Then NumberIn = 5
'
NonsenseMaths = i_MyData * NumberIn
End Function
End Class
'
'================================================================
'
' Now for use...
'
Dim sStick ' This will hold our new object

Set sStick = New MyClass
Response.write(sStick.MyData)
Response.write("<br>")
sStick.MyData = 666 ' the devil (booo!)
Response.write(sStick.MyData)
Response.write("<br>")
sStick.DoSomething()
Response.write(sStick.NonsenseMaths(9))
Response.write("<br>")
Set sStick = Nothing
%>

Leave a Reply