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.


Beware of object scoping in Visual Basic

I’m doing some Visual Basic programming in Microsoft Word at the moment and spent some time on Friday trying to find out why all my objects (based on a Visual Basic class module) apparently had the same values. Apparently the following doesn’t work:

Dim col As New Collection
Dim i As Integer

For i=0 To 10
  Dim obj As New Person
  obj.Firstname = "Firstname" & i
  obj.Lastname = "Lastname" & i
  Call col.Add(obj)
Next

If you run the above code all the objects will hold Firstname10 and Lastname10 in their class variables. The solution is to do this instead:

Dim col As New Collection
Dim obj As Person
Dim i As Integer

For i=0 To 10
  Set obj = New Person
  obj.Firstname = "Firstname" & i
  obj.Lastname = "Lastname" & i
  Call col.Add(obj)
Next

The difference is that in the latter example the definition of the Person reference is done outside the loop while both the definition and the instantiation is done inside the loop in the first example. While both of the above cases are quite okay in LotusScript, and works as you would expect, you apparently have to define the reference outside the loop in Visual Basic for it to work. Why that would be the case is unclear to me…

Oh well… 🙂