Tip

10 advanced LotusScript tips

  1. Understand binding
  2. Code for performance
  3. Use lists and classes
  4. Use class inheritance
  5. Use platform-specific LotusScript code with classes
  6. Use version-specific LotusScript code with classes
  7. Use LSI_Info()/GetThreadInfo
  8. Use the execute command
  9. Use advanced logging
  10. Mixing Java and LotusScript

  Advanced LotusScript Tip #1. Understand binding 

There are two types of binding: early binding and late binding.

Early binding is set by the compiler and works well because it uses type checking, works quickly and is easy to use. An example of early binding might be:


Dim S as String

Late binding is set at runtime. It is very flexible, but doesn't use type checking. Unfortunately, the performance isn't as good as early binding and you might run into some runtime errors.


Dim V as variant
Dim S as new NotesSession
set V = S.CurrentDatabase
print V.getTitle()

  Advanced LotusScript Tip #2. Code for performance 

When you're coding for performance, always remember that expensive operations include opening Lotus Notes databases, and views and documents with lots of fields. So, when you're collecting data, remember to cache views wherever possible and use NotesViewEntry instead of opening documents.

As an example, let's say you have a Lotus Notes database with 100,000 documents in it. This would take seven hours to actually open every document in the Lotus Notes database, if you don't code for performance and use views. If you do code for performance, it will only take you 60 minutes to open these using NotesView and only 12 minutes if you use NotesViewEntry!

  Advanced LotusScript Tip #3. Use lists and classes 

It's good practice to use LotusScript lists and classes because classes bind complex data and operations. Lists can look these up quickly in memory. For a quick example, here's how we might extend our Person class:


dim People list as Person
dim PeopleByUNID list as Person
Dim P as new Person
("Joe Bloggs/ACME", "010101010201020")
....
set People(P.getName) = P
set PeopleByUNID(P.getUNID) = P
if (isElement(People("Joe Bloggs/ACME"))) then _
Print "Joe's UNID is: " + 
People("Joe Bloggs/ACME").getUNID
if (isElement(PeopleByUNID("010101010201020"))) then _
Print "UNID '010101010201020' is: " + _
PeopleByUNID("010101010201020").getName

  Advanced LotusScript Tip #4. Use class inheritance 

Class inheritance allows us to "Extend" classes to add functionality. For example:


class StaffPerson as Person
private strStaffID as String
sub new(strNewPerson 
as String, strNewUNID as String)
end sub
public function setStaffNumber(newNo as String)
strStaffID = newNo
end function
public function getStaffNumber as String
getStaffNumber = me.strStaffID
end function
end class

  Advanced LotusScript Tip #5. Use platform-specific code with classes 


Dim s as new NotesSession
Dim mem as variant
select case s.platform
case "Windows/32"
set mem = new getMemW32()
case "AIX"
set mem = new getMemAIX()
case else
Print "Platform not supported"
set mem = nothing
end case
if (not mem is nothing) then
call mem.printMemory()

Class getMem
function getMem() as long
getMem = 0
end function
sub printMemory
print me.getMem()
end sub
end class

Class getMemW32 as getMem
function getMem() as long
getMem = getWindowsMemory()
end function
end class

Class getMemAIX as getMem
function getMem() as long
getMem = getAIXMemory()
end function
end class

  Advanced LotusScript Tip #6. Use version-specific code with classes 


Dim s as new NotesSession
dim vCU as variant
select case s.version
case 5
set vCU = new createUser()
case 6
set vCU = new createUserv6()
case else
Print "Version not supported"
set vCU = nothing
end case
if (not vCU is nothing) then _
call vCU.CreateUser(....)

Class createUser
function createUser(...) as integer
....
end function
end class

Class createUserv6 as createUser
function createUser(...) as integer
....
end function
end class

  7. Use LSI_Info()/GetThreadInfo 

You can use the LSI_INFO() command to get some runtime information. Be aware though that this information is superceded by the GetThreadInfo command.

If you use GetThreadInfo(11), that will return you the calling class. If you use GetThreadInfo(10), that will return you the function name. And these are just the beginning.

Through error trapping, we can track where we came from. We don't have to pass lists of parameters to error trapping code. It also prevents coding errors through using the copy and paste method.

Here is an example of this in use, preceded by the calling code:


' calling code...
...
ExitFunction:
exit function
errorhandler:
Call RaiseError()
resume exitFunction
end function
Function RaiseError()
Dim thisType As String
Dim es as String
thisType = Typename(Me)
' Not a class, use the calling module instead
If (thisType = "") Then thisType = Getthreadinfo(11)
es = thisType & "::" & Getthreadinfo(10) & ": "
If (Err = 0) Then
es = es + "Manually raised an error"
Else
es = es + 
"Run time error: (" + Trim(Str(Err)) + ") " + _
Error$ + " at line: "+ Trim(Str(Erl))
End If
Print es
end function

  Advanced LotusScript Tip #8. Use the execute command 

By using the execute command, you can run LotusScript from a string. Doing this accommodates version/platform differences at runtime. Here's an example:


Dim executeString as String
executeString = |
print "Hello world"
dim s as new NotesSession
dim db as NotesDatabase
set db = s.currentDatabase
print "Current Database name is: " + db.Title
|
execute (executeString)

  Advanced LotusScript Tip #9. Use advanced logging 

By using the OpenNTF "OpenLog" solution, you can make simple LotusScript library additions to your code, provide "called from," "error," and "line number" functionality. Our system now works on error trap and displays all objects in memory.

  Advanced LotusScript Tip #10. Mixing Java and LotusScript 

By mixing Java and LotusScript together, you can really get the most out of each scripting language. The trick is to use each language to its strengths. For example, Java is good for Web service, network I/O, and multithreaded operations. LotusScript is the traditional Lotus Notes development language and works in the user interface.

Mixing the two languages together is easy -- just call an agent, passing a Lotus Notes document.

You should also know that this works both ways, as you can call Java from LotusScript. This is called LS2J. An example is below:


// Create a Script Library of type "Java" called xlib
// containing the following function:
public class calculator {
public int add(int a, int b) { return a + b; }
public int div(int a, int b) { return a / b; }
public int mul(int a, int b) { return a * b; }
public int sub(int a, int b) { return a - b; }
}
Option Public
Use "xlib"
Uselsx "*javacon"
Sub Initialize
Dim mySession As JavaSession
Dim myClass As JavaClass, 
calculator As JavaObject, a,b,c As Integer
Set mySession = New JavaSession()
Set myClass = mySession.GetClass("calculator")
Set calculator = myClass.CreateObject()
a = 10
b = 5
c = calculator.mul(a,b)
MessageBox "a * b = " & c
End Sub


TUTORIAL: 30 LOTUSSCRIPT TIPS

 Home: Introduction
 Part 1: 10 fundamental LotusScript tips
 Part 2: 10 everyday LotusScript tips
 Part 3: 10 advanced LotusScript tips
 Part 4: More LotusScript learning resources

This was first published in March 2007

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.