Overview

Don't try running this code - it's psudo code meant as an example of how Classes work. If anyone is interested I will post some real ones for you to have a play with. I thought that for the purposes of this example it would be fun to try to write a human resources class to handle employee hiring and firing. Let's say that Software company XYZ have decided to sack all of their human resource managers and hire instead people who might, with a little bit of luck and best of three attempts, be able to find their arse with both hands. And let's say that you have been tasked with writing a web based piece of software to replace the pointy haired maggots .

Why would we want to use classes?

Classes allow us to reuse code. We can store our common routines in classes and reuse it around our site as often as we need. This cuts both debugging time (because after a while you will have tested your base classes into the ground) and development time (because after a while you can start off virtually any project with half of the dog-work complete).

Instantiation

To use our class in our web based scripts we need to create an instace of our class. The following example shows how we can instantiate an instance of our clsHumanResources class and assign it to the HR variable. Once instantiated we can call the methods and properties defined in the class in the format:

HR.<method>(<Properties>)
Code:
'Include the path to our class file
 
<%
'Instantiate an instance or clsHumanResources
'and assign it to the variable HR
Set HR = NEW clsHumanResources
%>
Publicly exposed members of the class

Whenever I talk about publicly exposed members people always seem to have some sort of coughing fit . Cant imagine why. But this concept is quite important.

When the class has been instantiated only those methods defined as public are availible to the calling script. Private members can only be called from within the class itself. This is important because you wouldn't want someone to accidentally access the hr.salary vlaue for your CEO. This information is private and therefore must be a private member of the class.

Publicly exposed members (stop sniggering) can be used to change the private variable stored within the class. For instance, though we don't want to return the HR.salary value, we may wish to change that value. We can provide public members (inrement_salary and decrement_salray) to allow acces to this kind of functionallity.

The publicly exposed members in the clsHumanResources are:

Hire_Employee
The Hire_Employee method is called to begin the hiring process, which calls private methods to arrange interviews and negotiate salarys. When the method has found and hired a suitable employee it returns the employee number of the new employee.

Initialise
Becasue the Internet is a stateless environment the scope of any variable is only as long as the execution of the calling script. Each time the class is instantiated you would call the initialise method to load the private employee variables into the class

Save
If the calling script makes changes to the information held in the class if must save those new values before the calling script quits and the resourses that it used are released. This is accomplished by calling ths Save method.

Issue_Warning
The clsHumanResources class contains a function for Sack_Employee, but I didn't want to make this member public, just in case managers decided to sack people because they looked funny. So to sack someone you have to issues them with a warning. This method accepts a severity level so that gross_misconduct (defined as a constant) can result in automatic dismissal. If the Waring count gets to three then the private Sack_employee method is fired.

Employee_Quit
When an employee quits the class check the retirement age constant to see if the employee has reached retirement age. If they have then the class fires the Gold_Clock method, else proceeds to process their paperwork and cancel their salary.

Decrement_Salary/Increment Salary
These should be self explaintory.

Supporting Classes

Within classes it is possible to reference other classes. It is possible to have multi tiers of generic classes all contributing to the final result, and reducing the amount of coding per problem that you face. These classes are instantated and referenced in the same way as shown above.

clsSecurityGuard
The clsSecurityGuard has one public member. That member is Escort_Off_Premises. If it helps you visualise you can imagine that the security guards name is Bernard.

clsWhosBoss
I almost called this the office_politics class. Certainly the logic of such a class would be tortured at best.

The Human resources Class

Code:
'Include the supporting class files
 
 
<%

'Initialise supporting classes
'You can have classes within classes within classes
Set WhosBoss = NEW clsWhosBoss
Set SecurityGuard = NEW clsSecurityGuard

'Constants - these have global scope
Const Glass_Celing_Salary = 60000
Const Retirement_Age = 60

'These constants are used with the p_severity value
'of the issue_warning method
Const Misdemeanor = 1
Const Misconduct = 2
Const Gross_Misconduct = 3

'Start of class code
Class clsEmployee

'Public Variables------------------------------------------------->
'Public variables are accessed using the get and let statements
'm_Current_Project holds a reference to the public variable at
'local scope level
Private m_Current_Project

'Public Variables------------------------------------------------->
'These variables are NOT accessible to the calling program
Private Employee_number
Private Employee_age
Private Job_Title
Private Employee_Gender
Private Salary
Private WarningCount

'Public Functions------------------------------------------------->

Function Hire_Employee(p_Job_title, p_Starting_Salary, p_Start_Date)

	do
		do until found(suitable_candidate)
		loop
	loop until interview_result(p_Candidate_Number) = true

	'...
	'Set private Employee variables here
	'...
	Save

	Hire_Employee = employee_number

End Function

'Private Functions------------------------------------------------->

Private Function Interview_Result(p_Candidate_Number)

	if Is_Human(p_Candidate_Number) and & _
	social_skills(p_Candidate_Number) > 1 then
		if Negotiate = true then
			Interview_Result = true
		else
			SecurityGuard.Escort_Off_Premises(p_Candidate_Number)
		end if
	else
		SecurityGuard.Escort_Off_Premises(p_Candidate_Number)
	end if
			

End Function

Function Negotiate(p_Top_Figure, p_Bottom_Figure)

	do until p_Top_Figure = p_Bottom_Figure or Interview_Length > 2 hours 
	loop

	if p_Top_Figure = p_Bottom_Figure then
		Negotiate = true
	else
		Negotiate = false
	end if

End Function

'Public Methods------------------------------------------------->
'These methods can be accessed by the calling routine

Sub Initialise(p_Employee_Number)

	Employee_number = p_Employee_Number
	'...
	'some code here to load the other private vars into the class
	'...

End Sub


Sub Save()

	'...
	'some code here to load the other private vars into the class
	'...

End Sub


Sub Issue_Warning(p_Severity)

	WarningCount = WarningCount + 1

	Select Case p_severity
		case Misdemeanor
			'Warning issued - no further action
		case Misconduct
			'Warning Issued
			Decrement_Salary(1000)
		case Gross_Misconduct
			'Dissmissal on spot
			Sack_Employee(p_Employee_Number)
	End select

		If WarningCount = 3 then
			Sack_Employee(p_Employee_Number)
		end if
End Sub


Sub Employee_Quit(p_Employee_Number)

	If Employee_age => Retirement_Age then
		'Employee has reached retirement age
		Gold_Clock(p_Employee_Number)
	else
		Stop_Wages(p_Employee_Number)
	end if
	
End Sub

Sub Increment_Salary(p_Value)

	'Code Added by by PHB January 1919
	'to reflect new social realities
	Select case Employee_Gender
		case "Female"
			if p_Value + Salary > Glass_Celing_Salary then
				Salary = Glass_Celing_Salary
			else
				Salary = p_Value + Salary
			end if
		case Else
			Salary = p_Value + Salary
	end select

End Sub


Sub Decrement_Salary(p_Value)

	Salary = salary - p_value

End Sub

'Private Methods------------------------------------------------->
'These methods are private - they can only be called 
'from within this class

Private Sub Sack_Employee(p_Employee_Number)
'This method is private because yoo can't just sack someone
'You first have to call the Issue warning method which will
'call the 
	
	Stop_Wages(p_Employee_Number)
	SecurityGuard.Escort_Off_Premises(p_Employee_Number)

End Sub

Private Sub Gold_Clock(p_Employee_Number)

	'Code edited by PHB Arpil 1st 1980
	'Salary = salary / 2
	'Throw_Party(p_Employee_Number)
	'Give_Gift(p_Employee_Number)

	Stop_Wages(p_Employee_Number)
	SecurityGuard.Escort_Off_Premises(p_Employee_Number) 
	'Make sure employee doesn't steal any pens on way out

End Sub

Private Sub Stop_Wages(p_Employee_Number)

	Issue_Pink_Slip(p_Employee_Number)
	Decrement_Salary(Salary)
	Hire_Employee(Job_Title, (Salary/2), date(now))

End Sub


'Property Change event------------------------------------------------->
'NOTE web based class do no have events, so I raise a call to a function 
'that allows you to set the str_Current_Project to "cancel" to abort the
'changes.

Public Property Get Current_Project
   Current_Project = m_Current_Project
End Property

Public Property Let Current_Project(str_Current_Project)
   If OnChange_Current_Project(str_Current_Project) <> "cancel" then
      m_Current_Project = str_Current_Project
   end if
End Property

Function OnChange_Current_Project(New_Value)

	'I'd like to think the following were true:
	'If value_to_company(project(new_value)) > & _
	'value_to_company(Current_project) then
	'But in fact this is what happens:

	if Whosboss.BossOf(Employee_Number) = & _
	Whosboss.Whos_Asking(Employee_Number) then
		New_Value = New_Value
	else
		New_Value = "cancel"
	end if

End Function

End Class
%>
If you enjoyed this article you might also like these others:
Backing up the IIS metabase.
Building your own IDS tripwire.
Credit card security
Dumping SQL data to a text file
Hunting down skript kiddies
Search Engine submission 'exploit'
Forced shutdown of a remote nt/2k server
Securing an installation of IIS 4. (No, seriously)
Remote DSN Connections, using WinAPIs and the registry
Scripting Internet Connections Under Window$