Objects in ASP

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

Content:


<%
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
%>
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top