Paradox Community
Search:

 Welcome |  What is Paradox |  Paradox Folk |  Paradox Solutions |
 Interactive Paradox |  Paradox Programming |  Internet/Intranet Development |
 Support Options |  Classified Ads |  Wish List |  Submissions 


Paradox Programming Articles  |  Beyond Help Articles  |  Tips & Tricks Articles  


Getting Started - What Are Libraries and How Are They Used
© 2001 Jeff Cranston
Pure Surfing Endeavour, Inc.

Intended audience

Entry level programmers or Experienced Programmers who are new to Paradox®


Recommended Prerequisite knowledge
  • Comfortable using Paradox interactively
  • Ability to Create and modify basic design objects such as forms and buttons
  • Understand basic ObjectPal syntax

Background

During the course of using Paradox some individuals reach a point of wanting (or needing) more than is available through interactive use, so they learn to create design objects and modify their behavior by attaching ObjectPal code. Suddenly, a program is born (your baby). Just like babies, programs have a way of growing faster than expected and can become difficult to manage. Fortunately with Paradox we have libraries.


Objectives

After reading this material and performing the examples the user should be able to:
  1. Explain what a library is
  2. Know the benefits of using libraries
  3. Create a library
  4. Access a library from a form and execute a method in that library

1.0 What are libraries and why should I use them?

From the Paradox help file [Copyright © 1997 Corel Corporation]: A library is an object you can use to store commonly used ObjectPAL code which lets you easily share code among forms, scripts, and other libraries.

An easily accessed place to store code. Sounds very similar to a public library for books.

Using the example of a public library, You may enjoy a good book occasionally and could keep them neatly organized on a shelf in your house. You might develop a passion for reading and start collecting books. After a while you could have so many books they occupy nearly all the space in your house. You may even have several copies of the same book but lose track of them in all the chaos. Just getting from room to room becomes an exercise in careful navigation to prevent the stacks of books from crashing down upon you.

... Or you could use the public library, a facility specifically dedicated to storing books until they are needed. You go to the library, acquire the books you need and return them when you are through using them. The library keeps track of the books in an orderly manner that allows you to find them quickly and easily. If the library does not have a book you need, they can call another library and get it for you.

In a Paradox application you could have a few forms with just a few custom methods each. Later you add a few more (your baby is growing). Suddenly you end up like the passionate reader, lots of forms, lots of code. You spend more time trying to navigate your maze than being productive. You end up writing the same code in numerous places. Just getting from form to form becomes an exercise in careful navigation to prevent the application from crashing down upon you. Deja vu'?

... Or you could use libraries. An object specifically dedicated to storing code (methods, procedures, etc.) until it is needed. You open the library, use only the code you need and close when you are through. The library allows you to store the code in an orderly manner. You keep only one copy of commonly used code and call it whenever you need it. If the library does not have what you need, it can call another library and have it made available to you.

In Paradox the speed at which a form opens is directly related to the form's file size. The size of a form file is directly proportional to the amount of code in the form. Add lots of code to forms and the objects they contain and your application will slow considerably. Use libraries to hold the bulk of the code and have the forms call these routines as they are needed to solve this issue.

As you can see using libraries brings a lot of positive aspects to your application like speed, modularity and reusable code. They help keep your programming well structured and organized.


2.0 Creating and using a library

Coding to use libraries is not hard.

Many people find figuring out how from the help files to be difficult. So here is a simple start:

Place a button on a form. Inspect its mouseclick event and add this code:
method mouseClick(var eventInfo MouseEvent)
var
lib library
endvar

lib.open("TestLib.lsl")  ; open library
lib.TestMaster()         ; run the TestMaster method in TestLib library
lib.close()              ; close library when done
endMethod
Inspect the button's uses clause, add code so it looks like this:
Uses Objectpal
TestMaster()  ;objectPal method in TestLib library
endUses
Create a new library by selecting File|New|Library from the Paradox menu or by using the mouse to right click on "Library" in the left pane of the project viewer or ProView® tool and selecting "New". A library design window is opened on the Paradox desktop. The library window looks like a form window except it is blank, because libraries do not contain design objects. Save the library as TestLib.lsl. Open the object explorer on this library and create a new method named TestMaster. Edit TestMaster to look like this:
method TestMaster()
;display a message to user
msgInfo("Status", "The TestMaster method ran")
endmethod
Ensure the syntax is correct. Save the form and the library then close them. Open the form in run mode and click the button. If you did everything correctly you should see a message dialog box on screen with the caption "Status" and the message "The TestMaster method ran".

So what did we do? When you mouse click the button it opens a library (TestLib) and runs a method (TestMaster). The button knew of the existence of the method TestMaster in the TestLib Library because we added it to the button's uses clause. It is important to ensure the keyword ObjectPal is placed immediately after Uses so the program knows it is looking for and using an ObjectPal method. You did not see the library at runtime because it does not contain design objects.

Now you can make nice efficient forms that open fast but have large amounts of code available to them. An example of how to use this: In many programs users are presented with a main menu form from which to select tasks such as invoicing customers. If all the code for this task, as well as all the other things a business normally does is kept on a form, the form file could become very large and run slowly. By storing the code in libraries, the main menu calls a method from the library to perform the invoicing process. The library runs the code to manipulate data, open forms and print the invoice. Upon completion, the library is closed and the user is returned to the main menu. By separating your task into libraries (modularity) you can easily change, add or delete them.

Where can you go from here? See Getting Started-Passing variables between forms and libraries.


Summary

A library is an easily accessed object in which to store code.

Speed, modularity and reusable code are benefits of using libraries. Libraries help keep your applications well structured and organized. Storing code in libraries instead of on forms allows forms to open faster.

To create a library:

Use the Paradox menu File|New|Library
or
Right click Library in the left pane of the Project Viewer or ProView tool

To access a library from an object on a form and execute an objectPal method in that library:
  1. Type the library method name in the object's uses clause
  2. Ensure the keyword ObjectPal immediately follows Uses
  3. In the appropriate event for the object (such as mouseclick),
    Open the library, Call the method, Close the library upon completion

End notes

ProView is a project viewer alternative by Vladimir Menkin.


Helpful hints

Name your libraries, methods, procedures, variables, etc. with names that are associated with their purpose. This is a basic programming rule of thumb. InvoiceLib is a lot more meaningful than iLib, to you in 6 months or another programmer in 6 years.

Use aliases to refer to your Libraries. They work as well here as they do everywhere else in Paradox.

In the uses clause provide a comment noting the library that the method is from. When you have multiple libraries and methods this helps keep you organized. If somebody else has to work after you it saves them from going on a hunt.


Discussion of this article


 Feedback |  Paradox Day |  Who Uses Paradox |  I Use Paradox |  Downloads 


 The information provided on this Web site is not in any way sponsored or endorsed by Corel Corporation.
 Paradox is a registered trademark of Corel Corporation.


 Modified: 15 May 2003
 Terms of Use / Legal Disclaimer


 Copyright © 2001- 2003 Paradox Community. All rights reserved. 
 Company and product names are trademarks or registered trademarks of their respective companies. 
 Authors hold the copyrights to their own works. Please contact the author of any article for details.