The LotusScript language in Domino is very powerful once you find out that you can create custom classes with it. I´m going to write some articles about creating great helper classes, and start off with the basics of creating user classes. I am NOT going to explain why object oriented programming is better than procedure programming...
The definition of a class looks like this:
Public Class Car
End Class
The Public tells that this class will be seen outside the agent or scriptlibrary, where the class is created. You can also make a Private class, that only the agent or script library can see, where the class is defined:
Private Class Car
End Class
To create an instance of a class, you use the New keyword:
Dim myCar as New Car()
or:
Dim myCar as Car
Set myCar = New Car()
To add functionality to a class, you can create functions, subroutines, property getters and property getters. I will not explain the last ones, as they easily can be transformed into functions and subs. To add a function, that returns a value, you write like:
Public Class Car
Public Function getTopSpeed() as Integer
End Function
End Class
To add a subroutine, that does not return a value, you write:
Public Class Car
Public Sub start()
End Sub
End Class
There are two special subroutines, delete and new:
Public Class Car
Public Sub new()
End Sub
Public Sub delete()
End Sub
End Class
The new subroutine, also called constructor, is called whenever you use the New keyword to create a new instance of the class. The delete subroutine is called when there are no references to the object.
To add arguments that can be passed to subroutines and functions, just add them like you do to any ordinary function or subroutine:
Public Class Car
Public Sub new(topSpeed As Integer)
End Sub
End Class
To add instance member variables, declare them inside the class, without the usual Dim:
Public Class Car
topSpeed As Integer
End Class
To use instance member variables, you handle them like any other variable. A special case is show below, when a function has an argument variable, that has the same name as the instance variable. The keyword Me is used to denote the current object, and when saying Me.topSpeed, we mean the instance member variable topSpeed, and not the function argument variable :
Public Class Car
topSpeed As Integer
Public Sub new(topSpeed as Integer)
Me.topSpeed = topSpeed
End Sub
Public Function getTopSpeed() as Integer
getTopSpeed = topSpeed
' Can also be written as:
' Me.getTopSpeed = Me.topSpeed
End Function
End Class
To remove an object from memory, you use the Delete keyword. This removes the actual object from memory, and all references that are using the object can not use it any more.
Dim myCar as New Car(120)
Delete myCar
If you just want to relase a reference to an object, you use the Nothing keyword. This will only remove the specified reference, but the object is still in memory, until ALL references are removed.
Dim myCar as New Car(120)
Set myCar = Nothing
You can test if an object reference is still valid, by comparing the object reference to Nothing:
Dim myCar as New Car(120)
' Do some work...
If myCar Is Nothing _
Then Error 2000, "Object reference not valid"
To check what type an object reference is, you can use the IsA keyword. This is useful, when you don´t know at compile time what type of object you handle:
Dim myVehicle as Variant
If userName = "Donald" Then
Set myVehicle = New Helicopter()
Else
Set myVehicle = New Bicycle()
End If
If myVehicle IsA "Helicopter" _
Then Print "Fly away!"
The problem with the above is, that if you want to assign non-objects to the same variable, you must test if the variable is an object, before trying to use IsA or testing if it is Nothing:
Dim myVehicle as Variant
If userName = "Donald" Then
Set myVehicle = New Helicopter()
Else If userName = "Mickey" Then
Set myVehicle = New Bicycle()
Else
myVehicle = "Illegal user?"
End If
If IsObject(myVehicle) Then
If myVehicle IsA "Helicopter" _
Then Print "Fly away!"
Else
Print "No object, probably strange user..."
End If
To get the name of the class that was used to create an object instance, use the keyword Typename:
Dim myCar As New Car(120)
' Will print "Car":
Print "The type of object is: " & Typename(myCar)...
Ok, let´s do some useful with our new knowledge, let´s create a working Car class. The start sub must be called before the accelerate() method is called.
Public Class Car
topSpeed As Integer
currentSpeed As Integer
started As Intger
Public Sub new(topSpeed As Integer)
Me.topSpeed = topSpeed
End Sub
Public Sub start()
started = True
End Sub
Public Sub stop()
currentSpeed = 0
started = False
End Sub
Public Function accelerate() As Integer
If Not started _
Then Error 2000, "Start the before driving"
If currentSpeed => topSpeed _
Then Error 2000, "Top speed reached"
currentSpeed = currentSpeed + 10
accelerate = currentSpeed
End Function
Public Function getTopSpeed() As Integer
getTopSpeed = topSpeed
End Function
Public Function getCurrentSpeed() As Integer
getCurrentSpeed = currentSpeed
End Function
End Class
To use our class, we can write:
Dim car As New Car(120)
Call car.start()
Do While car.getCurrentSpeed() < car.getTopSpeed()
Print "Current speed is: " & car.accelerate()
Loop
Print "Top speed " & car.getTopSpeed() & " reached!"
Call car.stop()
Print "Car parked!"
Set car = Nothing
There is more to learn about classes in LotusScript, so stay tuned for the next article! Meanwhile, check the following links:
"Using the object-oriented features of LotusScript" on Lotus Developer Domain, great explanation of all (?) advantages!
"User-defined classes" in the Domino Designer Help 5.0.3