OpenDOM

Wednesday, March 21, 2007

Static Properties & Methods in LotusScript Classes


In Java and JavaScript languages, static properties and static methods are shared by all instances of a given class. Non static members relate to objects instantiated from a class and are relevant to a single instance of that class thus File objects share pathSeparator operating system dependent static property, while getAbsolutePath( ), canWrite( ) properties differ from a File object to another as the delete( ) method yields separate results for every object. Utilizing static members does not require to instantiate objects e.g. java.io.File class pathSeparator property can be referenced typing File.pathSeparator or java.io.File.pathSeparator.

Creating class level properties or methods in LotusScript may be useful. Consider a Platform class with properties such as Newline and supportsCOM. Although this class exhibits different information depending on the operating system, Newline and supportsCOM results are identical across all instances of the Platform class for a given Notes client or Domino server. Imagine a LotusScript.math package holding a Trigo utility class with sin(), cos() methods and a PI static property whose precisions are greater than that of native LotusScript Pi constant and functions.

Although static members are not available in LotusScript to the extent Java and Javascript languages provide them, you can implement similar constructs with identical coding syntax. As an exemple I shall use LotusScript.lang.Interpreter class and its Release property, available from OpenDOM open source project at www.openntf.org site to illustrate my point.

Private Release As String
Release = lsi_info( 6 )

While these two lines are the simplest way to compute a given LotusScript interpreter release, I like to protect my code from any sins including my own and I tend to adopt the following defensive coding technique :

Static Private Property Get INTERPRETER_RELEASE As String
Static this As String
If this = "" Then this = Lsi_info( 6 )
INTERPRETER_RELEASE = this
End Property ' LotusScript.lang.Interpreter.RELEASE property

LotusScript simplified Interpreter public class spells as follows :

Public Class Interpreter '
Property Get RELEASE As String '
Me.RELEASE = INTERPRETER_RELEASE
End Property ' LotusScript.lang.Interpreter.RELEASE
End Class ' LotusScript.lang.Interpreter Class

Note every objects instantiated from the Interpreter class share a common RELEASE property that can be qualified static as in Java and JavaScript. The Interpreter.RELEASE syntax can be implemented in "LotusScript.lang" script library as :

Static Public Property Get Interpreter As Interpreter
Static this As Interpreter
If this Is Nothing Then Set this = New Interpreter
Set Interpreter = this
End Property ' LotusScript.lang.Interpreter class

Such ability can be extended to LotusScript.lang.Interpreter.RELEASE fully qualified class name coding two extra classes and two properties as in :

Public Class lang '
Public Property Get Interpreter '
Set Me.Interpreter = LANG_INTERPRETER
End Property ' LotusScript.lang.Interpreter
End Class ' LotusScript.lang.* Package
Public Class LotusScript '
Public Property Get lang As lang '
Set Me.lang = LOTUSSCRIPT_LANG
End Property ' LotusScript.lang.* Package
Public io As Variant ' As io__ '
Public net As Variant ' As net__ '
End Class ' LotusScript.* Package

Static Private Property Get LOTUSSCRIPT_LANG As lang
Static this As lang
If this Is Nothing Then Set this = New lang
Set LOTUSSCRIPT_LANG = this
End Property ' LotusScript.lang.* package
Static Public Property Get LotusScript As LotusScript
Static this As LotusScript
If this Is Nothing Then
Set this = New LotusScript
End If
Set LotusScript = this
End Property ' LotusScript.* package

Any agents, forms or views now require few lines of code to fully reuse "LotusScript.lang" package library :

Use "LotusScript.lang"
Msgbox LotusScript.lang.Interpreter.RELEASE,, _
"LotusScript.lang.Interpreter.RELEASE"
' OR
With LotusScript.lang
Msgbox Interpreter.RELEASE,, _
"Interpreter.RELEASE"
End With

Labels: , , , , ,

Links to this post:

Create a Link

<< Home