Note: ASP Classic does not have nor need protected visibility because ASP does not support inheritance.
Public Properties Use a Private Member Field
A common technique for implementing a class property in ASP is to use a public property with a private member field to store the value.
Class Cyborg
Private FCyborgName
Public Property Get CyborgName()
CyborgName = FCyborgName
End Property
Public Property Let CyborgName(pCyborgName)
FCyborgName = pCyborgName
End Property
End Class
Working Example
Here is a working example using the class above. This example demonstrates implementing a public property, a public method, and a private member field. In addition, it demonstrates creating an object instance, using the members, and finally cleaning up by setting our object instance variable to nothing.
<%@LANGUAGE=VBScript%>
<%Option Explicit%>
<html>
<body>
<h1>Introduce Yourself</h1>
<%
'Declare object instance variable.
Dim Cameron
'Create object instance from class.
Set Cameron = new Cyborg
'Use members.
Cameron.CyborgName = "Cameron"
Cameron.IntroduceYourself()
'Cleanup object instance so we don't have memory leaks.
Set Cameron = Nothing
%>
</body>
</html>
<%
'Class code block.
Class Cyborg
'Class member field.
Private FCyborgName
'Class property - get value.
Public Property Get CyborgName()
CyborgName = FCyborgName
End Property
'Class property - set value.
Public Property Let CyborgName(pCyborgName)
FCyborgName = pCyborgName
End Property
'Class method.
Public Function IntroduceYourself()
Response.Write("Hello, my name is " & CyborgName & ", it's nice to meet you!")
End Function
End Class
%>
Business Objects
A business object in this case means a class that represents a problem domain element that has attributes and methods. Examples might include a member, customer, vendor, employee, invoice, item, newsletter, etc.
Suggested implementation details:
- Use one class per file so you can include only the code that's required for a given script.
- Adopt a naming convention so you can easily see and use a business object. Something like c_TableName.inc where TableName is the name of the database table.
- Initially have one public member field for each field in the table. Convert these public member fields to properties as needed. As the class grows and becomes more complete, you'll notice that you'll want to create a property of the same name and move the member field visibility from public to private (add "F" for field in front of the name as demonstrated in the above example).
- Initialize all member fields in the Class_Initialize event. Set strings to "", numbers to 0, etc.
- Create a common method name to load data, perhaps called LoadFromTable(). In that way, you can have other loading methods from other types of data such as LoadFromForm().