Defining member properties and methods

Properties and methods are tied to their class and can be used only with an object belonging to that class. You define properties and methods inside the Class statement.

  • Property

    A pair of functions used to manipulate protected variables, that is, Private members of a user-defined class to which the application has no direct access.

  • Method

    A sub or function that performs operations on objects.

Class syntax, containing a member variables, a Sub, Function, and Property

The Stack class uses several properties and methods to perform simple push and pop operations on a stack data structure.

Class Stack 
  Private idx As Integer
  Stack List As Variant
  Public stackName As String
  Private Sub CheckStack ' Sub is visible only within 
                         ' the class.
    If idx% = 0 Then Error 999
  End Sub
  Sub New
    idx% = 0        ' Initialize idx.
  End Sub

   Private Property Set topValue As Variant
     CheckStack
     Stack(idx%) = topValue  ' Set the top value on the stack.
   End Property
  
   Private Property Get topValue As Variant
     CheckStack
     topValue = Stack(idx%)  ' Get the top value on the stack.
   End Property

   ' Same as Get for topValue.
   Function Top
      Top = topValue   ' Call the topValue Get method.
   End Function

   Sub Push(v)         ' Push a value on the stack.
     idx% = idx%+1
     topValue = v
   End Sub

   Function Pop        ' Pop a value off the stack.
     Pop = topValue
     Erase Stack(idx%)
     idx% = idx%-1
   End Function

   ' Read-only property. There is no Set for Count.
   Property Get Count
     Count = idx%       ' Count the values on the stack.
   End Property

End Class

Dim St As New Stack
Call St.Push("An item on the stack")
Call St.Push("Another item on the stack")
Print "# of items on the stack is ";St.Count
Print "TopValue is ";St.Top

Declaring Sub New and Sub Delete (initializing and deleting objects)

Within a class definition you can create two special subs. They are always Public; you cannot declare them as Private.

  • Sub New

    A sub that LotusScript® executes automatically when an object is created. Sub New executes when LotusScript® executes a Dim statement with the New keyword, or executes a Set statement, referring to the class for which the Sub New is defined. You create Sub New by defining a sub named New and the parameters to initialize the newly created object. A class can have only one Sub New.

  • Sub Delete

    A sub that LotusScript® executes automatically when the object for which it is defined is deleted. You create a Sub Delete by defining a sub named Delete, without specifying parameters. A class can have only one Sub Delete.

You can use these subs as events in your scripts. For example, you could create a File class that uses Sub New to open a file and Sub Delete to close a file. Similarly, you could create a PrintJob class that uses Sub New to start a new page, align text, and set the margins, and that uses Sub Delete to terminate the print job.

Sub New in the example script initializes the member variables of the CustomerAccount object. The Set statement that creates a new Account object also passes three arguments required by the Sub New for the Account class. Sub New assigns the values of the arguments to the three member variables of the newly created object: balance@, acctNum&, and customerNum&.

Class Account
  balance As Currency
  acctNum As Long
  customerNum As Long
' Declare Sub New.
  Sub New (newBal As Currency, newAcctNum As Long, _
    newCustNum As Long)
     balance@ = newBal@
     acctNum& = newAcctNum&
     customerNum& = newCustNum&
     Print "New Parms=";balance@, acctNum&, customerNum&
  End Sub
' Declare Sub Delete.
  Sub Delete
     Print "Deleting account record for customer: ";customerNum
  End Sub
End Class
'.....
Dim CustomerAccount As Account
' Create the object.
Set customerAccount = New Account(1234.56, 10001991, 5412)           
Delete customerAccount     ' Explicitly delete the object.