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… 🙂

Anyone who have used the Deftype statement in LotusScript?

While looking for something completely else in the Domino Designer 7 Help I stumbled over the Deftype keyword. I find it hard to find any usage for it but then again I always code with Options Explicit/Declare and type all my variables but still… Anyone who have actually used it and can give an example?

Deftype Statements
Set the default data type for variables, functions, and properties whose names begin with one of a specified group of letters.
.

Interesting technote on maximum execution time of LotusScript agents

I was quite surprised the read the snippet from the cited technote below but after thinking about it, it makes sense. How would the AMgr know beforehand how long time the agent would run and hence how to keep the Terminate event inside the maximum execution time…

“By design, the Terminate event will execute after an agent has been halted for passing the time-out setting. The best coding practice would be to have only necessary clean-up operations within the Termination event. The main operations of the agent should reside in the Initialize event.”

Via the Lotus Support RSS feed: Terminate Event of a LotusScript Agent continues running after the Max Execution time (technote 1201324)