IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
ASP Classic
Search ASP Classic Group:

Advanced
-Collapse +Expand ASP Classic Store

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBWebsite Scri...ASP ClassicOOP   Print This     
  From the January 2016 Issue of Prestwood eMag
 
ASP Classic OOP:
ASP Classic Member Visibility (Private, Public)
 
Posted 15 years ago on 3/11/2009
ASP Classic Code Snippet:
 A flashcard from our ASP Classic Flashcards Library
 A code snippet from our ASP Classic Code Snippets Page

KB101956

General Info: Class Visibility Specifiers

In OOP languages, members of a class have a specific scope that indicates visibility. Standard visibility includes private, protected, and public. Private members are usable by the defining class only (fully encapsulated). They are invisible outside of the class except by friendly classes. Protected members are usable by the defining class and descendant classes only (plus friendly classes). Public members are usable wherever its class can be referenced.

Languages Focus: Member Visibility

Traditional member visibility specifiers for fully OOP languages are private, protected, and public. Many modern OOP languages implement additional member visibilities.

Additional member modifiers are documented under the Member Modifiers topic.

ASP Classic Member Visibility

The member visibility modifiers are Private and Public. If not specified, the default is Public. Private and Public have the usual meaning. Private members are visible only within the class block. Public members are visible within the class and outside of the class.

Syntax Example:
Class Cyborg
  Private FSerialNumber  
  Public FCyborgName
  
  Public Function IntroduceYourself() 
Response.Write("Hi, I do not have a name yet.")
End Function
End Class

 

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().

More Info

Definition:  Class Visibility Specifiers

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1105A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101956 Counter
20119
Since 3/11/2009
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]