12.3 Counter

This component creates a Counter object that can generate, store, increment, and retrieve any number of counters. A counter contains an integer that you can use with the Get, Increment, Set, and Remove methods of the Counters object. If a counter is created, it persists until you remove it. Counters do not get incremented automatically on an event like a page hit. They should be manually set or incremented using the Set and Increment methods.

Several pages on a specific site can retrieve or use the value of a single counter. For example, if you increment and display a counter named Clicks in a page called PAGE1.HTM, and you increment Clicks in another page called PAGE2.ASP, both pages will increment the same counter. If you hit page1.htm and increment Clicks to 3, hitting PAGE2.ASP will increment Clicks to 4. The next time you hit PAGE1.HTM, Clicks will increment to 5. All counters are stored in a single text file, COUNTER.TXT. This file should not be modified manually.

12.3.1 Object Diagram

Describes  the hierarchy of various objects in the Counter component

Object Name

Properties

Methods

Counter Object

None.

Get method

Increment method

Remove method

Set method

12.3.2 Counter Object

Creates, stores, increments, and retrieves any number of counters.

12.3.3 Get method

Returns the current value of the specified counter. If the counter does not exist, this method creates the counter and sets its value to 0.

Syntax

 object.Get(
   CounterName As String)
 

Parameters

CounterName
String specifying the name of a counter.

Return Values

Long. Represents the current value of the counter.

Example

For more details on displaying the values of a specific counter see Example.

12.3.4 Increment method

Increments the specified hit count for the current Web page by 1. If the specified counter does not exist, it creates one by the specified name and sets its value to 1.

Syntax

 object.Increment(
    CounterName As String)
 

Parameters

CounterName
String specifying the name of a counter.

Return Values

Long. Represents the current value of the counter.

Example

For more details on incrementing a counter and displaying its values see Example.

12.3.5 Set method

Takes the name of a counter and an integer, sets the counter to the value of the integer, and returns the new value. If the counter does not exist, this method creates it and sets it to the value of the integer specified.

Syntax

 object.Set(
   CounterName As String
   Value As Integer)
 

Parameters

CounterName
String specifying the name of a counter.
Value
New value for the counter specified in CounterName.

Return Values

None.

Example

For more details on setting the counter to a new value see Example.

12.3.6 Remove method

Removes the counter from the Counters object and deletes the counter from the COUNTER.TXT file.

Syntax

 object.Remove(
   CounterName As String)
 

Parameters

CounterName
String specifying the name of a counter.

Return Values

None.

Example

The following code file increments the counters "student", "teachers" and "parents" based on the given label value and displays the incremented values. It also gives the page visit count and removes the counter "student" from COUNTER.TXT file.

 <% Set counters = Createobject("MSWC.Counters") %>
 	<%
 	 Counters.Set("teacher",10) 
 ’Set the count of teacher to 10 %>
 <%
 ’Increment the count of specified field by 1based on the input
 ’label value.
 If label = "1" Then
 	    Counters.Increment("Student")
 	  Else
 	    If label = "2" Then
 	      Counters.Increment("teacher")
 	    Else
 	      If label = "0" Then
 	        Counters.Increment("parent")
 	      End If
 	    End If
 	  End If
 %>
 <P>
 	Current strength:
 	<P>
 	stu: <% =Counters.Get( "Student" ) 
 ’Displays the latest count for specified fields. %>
 	<P>
 	teach: <% = Counters.Get( "teacher" ) %>
 	<P>
 	par: <% = Counters.Get( "parent" ) %>	
 	<% Counters.Remove("student") 
 ’Deletes a counter from counter.txt file. %>
 	<P>
 	This web page has been viewed <%= Counters.Increment("hits") ’increments and displays the value of the counter%>
 	times.
 	</P>