Geekpedia Programming Tutorials






Objects in ASP

How to create a custom object in ASP (done just now for a mate in need...).

On Monday, September 27th 2004 at 06:46 PM
By Matt W (View Profile)
****-   (Rated 3.6 with 8 votes)
Contextual Ads
More ASP Resources
Advertisement


<%
Option Explicit

'
' Class Example just for Neil... <-- a geezer new to ASP
'=======================================
' Imagine an object as a mini program in its own right that gives
' a larger parent program a controlled amount of access.
'
' Basically any code you do inside an object won't affect the rest
' of your code other than by the rules you set.  =O)
'
' Good for security, re-useability and ease of use in the medium/
' long-term of a project.  Basic standard includes that contains 
' loads of function/sub code snippets become cumbersome and messy 
' over time.  Objects solve that issue.
'
' I like keeping 1 object per include... =)  makes it easy to just
' include what I need... =)
'
'
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 MyData
        '
        ' 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
%>
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by gopi on Wednesday, June 14th 2006 at 11:21 AM

Very good for begineers like me!


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs ASP Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons