Creating custom classes in LotusScript, part 1

Source: Internet
Author: User

The Lotus script language in Domino is very powerful once you find out that you can create M classes with it. i'm going to write some articles about creating great helper classes, and start off with the basics of creating user classes. I am not going to explain why object oriented programming is better than procedure programming...

The definition of a class looks like this:

 
Public class carend class

The public tells that this class will be seen outside the agent or scriptlibrary, where the class is created. you can also make a private class, that only the agent or script library can see, where the class is defined:

 
Private class carend class

To create an instance of a class, you use the New Keyword:

 
Dim mycar as new car ()

Or:

Dim mycar as carset mycar = new car ()

To add functionality to a class, you can create functions, subroutines, property getters and property getters. I will not explain the last ones, as they easily can be transformed into functions and subs. to add a function, that returns a value, you write like:

 
Public class car public function gettopspeed () as integer end functionend class

To add a subroutine, that does not return a value, you write:

 
Public class car public sub start () end subend class

There are two special subroutines, delete and new:

 
Public class car public sub new () end sub public sub Delete () end subend class

The new subroutine, also called constructor, is called whenever you use the new keyword to create a new instance of the class. The delete subroutine is called when there are no references to the object.

To add arguments that can be passed to subroutines and functions, just add them like you do to any ordinary function or Subroutine:

 
Public class car public sub new (topspeed as integer) end subend class

To add instance member variables, declare them inside the class, without the usual Dim:

 
Public class car topspeed as integerend class

To use instance member variables, you handle them like any other variable. A special case is show below, when a function has an argument variable, that has the same name as the instance variable. the keyword me is used to denote the current object, and when saying me. topspeed, we mean the instance member variable topspeed, and not the function argument variable:

 
Public class car topspeed as integer public sub new (topspeed as integer) me. topspeed = topspeed end sub public function gettopspeed () as integer gettopspeed = topspeed'can also be written as: 'Me. gettopspeed = me. topspeed end functionend class

To remove an object from memory, you use the delete keyword. This removes the actual object from memory, and all references that are using the object can not use it any more.

 
Dim mycar as new car (120) delete mycar

If you just want to relase a reference to an object, you use the nothing keyword. This will only remove the specified reference, but the object is still in memory, until all references are removed.

 
Dim mycar as new car (120) set mycar = nothing

You can test if an object reference is still valid, by comparing the object reference to nothing:

 
Dim mycar as new car (120) 'Do some work... if mycar is nothing _ Then error 2000, "object reference not valid"

To check what type an object reference is, you can use the ISA keyword. This is useful, when you don't know at compile time what type of object you handle:

 
Dim defined as variantif username = "Donald" then set myvehicle = new helicopter () else set myvehicle = new bicycle () end ifif myvehicle Isa "helicopter" _ Then print "fly away! "

The problem with the above is, that if you want to assign non-objects to the same variable, you must test if the variable is an object, before trying to use ISA or testing if it is nothing:

 
Dim defined as variantif username = "Donald" then set myvehicle = new helicopter () else if username = "Mickey" then set myvehicle = new bicycle () else myvehicle = "illegal user? "End ifif isobject (myvehicle) then if myvehicle Isa" helicopter "_ Then print" fly away! "Else print" no object, probably strange user... "end if

To get the name of the class that was used to create an object instance, use the keyword typename:

 
Dim mycar as new car (120) 'Will print "car": Print "the type of object is:" & typename (mycar )...

OK, let's do some useful with our new knowledge, let's create a working car class. The start sub must be called before the accelerate () method is called.

 
Public class car topspeed as integer currentspeed as integer started as intger public sub new (topspeed as integer) me. topspeed = topspeed end sub public sub start () started = true end sub public Sub Stop () currentspeed = 0 started = false End sub public function accelerate () as integer if not started _ Then error 2000, "Start the before driving" If currentspeed => topspeed _ Then error 2000, "Top speed reached" currentspeed = currentspeed + 10 accelerate = currentspeed end function public function gettopspeed () as integer gettopspeed = topspeed end function public function getcurrentspeed () as integer getcurrentspeed = currentspeed end functionend class

To use our class, we can write:

 
Dim car as new car (120) call car. start () do while car. getcurrentspeed () <car. gettopspeed () print "current speed is:" & car. accelerate () loopprint "top speed" & car. gettopspeed () & "reached! "Call car. Stop () print" car parked! "Set car = nothing

There is more to learn about classes in Lotus script, so stay tuned for the next article! Meanwhile, check the following links:

"Using the object-oriented features of Lotus script" on Lotus developer domain, great explanation of all (?) Advantages!
"User-defined classes" in the domino designer help 5.0.3
The unfinished LotusScript book by Julian Robichaux
Bill Buchan's Lotus Notes programming tips

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.