<< Beware of object scoping in Visual Basic | Home | Learn to multiply big numbes >>

Properties as top level members of script libraries

Is anyone actually using the possibility of defining a Property as a top level member in a LotusScript script library? Apparent you are able to define a property as a top level member of a script library just as it is possible with a Sub or Function. The below code will actually compile and run ("SomeProperty" is not a variable but actually a Property defined in the Declarations section).

Sub Initialize
   SomeProperty = "HelloWorld"
   Msgbox SomeProperty
End Sub

Below is how the code actually looks in Domino Designer with "value" being a String variable defined in the Declarations section. Although possible I wonder if anyone actually uses it as I think it makes the code a little difficult to read.




Avatar: Jens Polster

I prefer getter and setter methods

I don't use public properties at all. I prefer to use getter and setter methods for better encapsulation. I'd rather have the class itself decide if and how a property might be modified.
Avatar: Mikkel Heisterberg

I prefer getter and setter methods

Well I didn't mean public class members but actually properties defined in a script library at the same level as subs and functions. Try writing "Public Property Get SomeProperty As String" in the Declarations section of a script library and see what happens once you press Enter.

Re: Properties as top level members of script libraries

Used in a custom class, it can provide a slightly better overview of class's functionality. Outside classes it's rather useless. It's like using "Let" statement for setting variables: it works, but why bother?
Dim x As Integer
Let x=3
Avatar: Thomas Bahn

Re: Properties as top level members of script libraries

We do not use Properties at all, since you can't name the private attribute the same as the property. We prefer the "Java syntax" and real functions and subs as getters and setters. But if you consider using script libraries as a possibility to implement the Singleton design pattern, it would be "normal" to use Getters and Setters for its attributes. Stretching this idea a little further, you could think of a script library "SomeClass" containing just one class definition "SomeClass" as the Class object of this class. Then its attributes and "methods" would be the static attributes and static methods of this class. For example: ' script library SomeClass Public Class SomeClass Public Sub New() ' ... End Sub End Class Private instance As SomeClass Private instancesCreated As Long Function GetInstance() As SomeClass If (instance Is Nothing) Then Set instance = New SomeClass() End If instancesCreated = instancesCreated + 1 Set GetInstance = instance End Function You can't make the constructor of SomeClass private, and since there are no namespaces, you probably can't name the sub just "GetInstance" without getting conflicts, but I think, you get the picture.
Avatar: Mikkel Heisterberg

Re: Properties as top level members of script libraries

Actually you can distinguish the members and the property using the Me keyword inside class scope subs, functions and properties (e.g. Me.firstname). I find your concept of using global properties and subs / functions of a script library to create singleton-style objects intriguing and interesting. Good point although the use is probably restricted. Thanks for chiming in... ;-)
Avatar: Thomas Bahn

Re: Properties as top level members of script libraries

What I meant is this:
Public Class TestClass
  Private testAttribute As Integer

  Public Property Get testAttribute As Integer
   testAttribute = Me.testAttribute
  End Property
End Class

This class won't compile with following error:
Name previously declared: TESTATTRIBUTE

So I use this:

Public Class TestClass
  Private testAttribute As Integer

  Public Function GetTestAttribute As Integer
   GetTestAttribute = Me.testAttribute
  End Function
End Class

Ciao
Thomas

Avatar: Alain H Romedenne

Re: Properties as top level members of script libraries

I have used Properties at library level numerous times and motivated such practice in those places:
  1. Static Properties and Methods in LotusScript Classes
  2. Write Secure Code Using Defensive Coding
Cordially
Alain

Add a comment Send a TrackBack