This tip is an extension to the last tip on custom LotusScript classes and covers derived classes. These are custom classes whose definition is partly inherited from another class definition. The derived class uses all the same properties and methods in the base class but can then be extended to add new functionality. |
Beginner LotusScript for
Notes Domino 6
Intermediate LotusScript for
Notes Domino 6
Advanced LotusScript for
Notes Domino 6
Notes Domino 6 LotusScript
Package
Using
Custom Classes in LotusScript - Part 2
Derived classes are custom classes whose definition is partly
inherited from another class (the base class.) The derived class uses all the
same properties and methods in the base class but can be extended to add new
functionality. This tip is the second part of a two part tip on custom classes.
To read part 1 click the following link:
Click
here to read Part 1 of this tip
This tip is for advanced
LotusScript programmers who are already
familiar with subroutines and functions. See the list of TLCC courses at the
start of this tip if you want to learn more about TLCC's LotusScript courses.
TLCC also offers a free demonstration version of our Beginner LotusScript
course.
Demonstration |
Demonstration Database The attached Database has a demonstration of the |
Using Derived Custom
Classes
Derived classes are simply a
class that "extends" another class. All of the "base" class properties and
methods are available to the derived class. Derived classes can be used to
create a new class that uses the definition of the base class and adds more
properties and methods unique to the derived class. In the last tip we used the
Person class below as part of the code samples.
1. | Class Person |
2. | Public firstname As String |
3. | Public lastname As String |
4. | Public address As String |
5. | Public city As String |
6. | Public state As String |
7. | Public phone As String |
8. | Public fullname As String |
9. | age As Integer |
10. | Sub New(fname As String, lname As String, address As String, city As String, state As String, phone As String) |
11. | firstname = fname |
12. | lastname = lname |
13. | Me.address = address |
14. | Me.city = city |
15. | Me.state = state |
16. | Me.phone = phone |
17. | fullname = fname + " " + lname |
18. | End Sub |
19. | Sub setAge(a As Integer) |
20. | age = a |
21. | End Sub |
22. | Function getAge() As Integer |
23. | getAge = age |
24. | End Function |
25. | Function addToAge(i As Integer) As Integer |
26. | age = age + i |
27. | addToAge = age |
28. | End Function |
29. | End Class |
The Person class can be extended to create
specialized classes for employees, customers, and/or suppliers. These extensions
are "derived" classes. The Person class would be considered the base class. A
simple derived class is shown below. This code was added to the Declarations
section in addition to the Person class above. Note how there is one new public
property for EmpNo (line 3) and one private property for salary (line 2.) There
are also two methods, getSalary and setSalary. In addition, all the properties
and methods of the Person class are available.
1. | Class Employee As Person |
2. | salary As Double |
3. | Public EmpNo As Single |
4. | Sub new(fname As String, lname As String, address As String, city As String, state As String, phone As String) |
5. | End Sub |
6. | Sub setSalary( s As Double) |
7. | salary = s |
8. | End Sub |
9. | Function getSalary() As Double |
10. | getSalary = salary |
11. | End Function |
12. | End Class |
1. | Sub Initialize |
2. | Dim cr As String |
3. | cr = Chr(13) |
4. | Dim John As New Employee("John","Smith","Main St.","Denver", "CO", "555-1212") |
5. | John.setSalary(45000) |
6. | Msgbox John.fullname & "'s salary is " & Format(John.getSalary(),"Currency") & "." |
7. | End Sub |
This code is demonstrated in the sample database as
the agent "Employee Class".
Using New in Custom
Classes
In the example above, the
New subroutine is empty of code statements. The New subroutine of the base
(Person) class is called and all the parameters of the New subroutine are
supplied to the New in the base class (remember, the code in the New subroutine
executes when the object is first created) are passed to the Base class.) In the
example above there is no need to have any extra code when the Employee class is
created. However, suppose the New subroutine in the derived class has to perform
additional processing when an object is created? Extra parameters can be added
to the New subroutine in the derived class. Code can then be added to the New
subroutine which is executed in addition to the code in the Base class. Whenever
the arguments in the New subroutine of the derived class do not match up with
the base class then the syntax of New is slightly different. The New subroutine
in the derived class has to list the actual argument names that are passed into
the base class. Below is the syntax from Lotus Designer
Help:
Sub New [
( [ argList ] ) ] [ ,
baseClass ( [ baseArgList ] ) ]
[ statements
]
End Sub
baseClass ( [ baseArgList ] )
Optional. The baseClass is the name of the class from which the
derived class is derived. This name must match the baseClass name in the Class statement for the
derived class.
The baseArgList is a comma-separated list of arguments for the sub New of the base class.
Note that these are actual arguments, not parameter declarations. This syntax
enables a call of the New sub for the derived class to furnish actual arguments
to the call of the New sub for the base class.
Suppose in our example above we want to add an
additional parameter when creating a new Employee to set the EmpNo. The class
definition is shown below. Note on line 4 the arguments for New are not the same
as the Person class (listed above.) There is an extra parameter for empno. Since
the arguments no longer match up with the base class than the aruguments have to
be listed in baseArgList. Note that these are not definitions but are the actual
parameter names. Line 5 sets the EmpNo property which was passed into
New.
1. | Class Employee As Person |
2. | salary As Double |
3. | Public EmpNo As Single |
4. | Sub new(fname As String, lname As String, address As String, city As String, state As String, phone As String, empno As Integer) , Person(fname, lname, address, city, state, phone) |
5. | Me.EmpNo = empno |
6. | End Sub |
7. | Sub setSalary( s As Double) |
8. | salary = s |
9. | End Sub |
10. | Function getSalary() As Double |
11. | getSalary = salary |
12. | End Function |
1. | Sub Initialize |
2. | Dim cr As String |
3. | cr = Chr(13) |
4. | Dim John As New Employee("John","Smith","Main St.","Denver", "CO", "555-1212", 32) |
5. | Msgbox John.fullname & "'s employee number is " & John.EmpNo & "." |
6. | End Sub |
This code is demonstrated in the "Extending New"
agent in the demonstration database.
The With Statement
A useful statement to use with custom classes
(including the Domino Object Model) is the With statement. The With statement
can be used to reference the properties and methods of an object without having
to list the name of an object. In the example below the With statement on line 6
uses the object reference variable for John. In the With statement whenever a
dot is used in front of a property or method it assumes the object to be
referenced is John.
1. | Sub Initialize |
2. | Dim cr As String |
3. | cr = Chr(13) |
4. | Dim John As New Employee("John","Smith","Main St.","Denver", "CO", "555-1212", 32) |
5. | Dim fullname As String |
6. | With John |
7. | .setSalary(67000) |
8. | fullname = .fullname |
9. | Msgbox fullname & "'s employee number is " & .EmpNo & "." |
10. | Msgbox .firstname & "'s salary is " & .getSalary & "." |
11. | End With |
12. | End Sub |
The With statement can be nested up to 16
levels.
This code is demonstrated with the "With Statement"
agent in the Demonstration database.
The With Statement can also be very useful when
dealing with the Notes Domino Objects as shown in the following code. It is used
on line 17 to provide a shortcut for the reference to "Doc".
1. | Sub Initialize |
2. | Dim session As New NotesSession |
3. | Dim dbCur As NotesDatabase |
4. | Set dbCur = session.CurrentDatabase |
5. | Dim collUnprocessed As NotesDocumentCollection |
6. | Set collUnprocessed = dbCur.UnprocessedDocuments |
7. | Dim doc As NotesDocument |
8. | 'Set up arrays to hold part name and price |
9. | Dim arrNames() As String |
10. | Dim arrPrices() As Double |
11. | Redim arrNames(0 To collUnprocessed.Count-1) |
12. | Redim arrPrices(0 To collUnprocessed.Count-1) |
13. | Dim i As Integer |
14. | i=0 |
15. | Set doc = collUnprocessed.getfirstdocument |
16. | Do Until Doc Is Nothing |
17. | With Doc |
18. | 'There is no need to reference Doc, just use the dot notation by itself |
19. | arrNames(i) = .partname(0) |
20. | arrPrices(i) = .partprice (0) |
21. | End With |
22. | i=i+1 |
23. | Set Doc = collUnprocessed.GetNextDocument(doc) |
24. | Loop |
25. | 'Do some further processing with the arrays.... |
26. | End Sub |