![]() |
![]() |
|
![]() |
Passing Variables between Forms Using a Library - Part 1 Passing a Single Variable © 2001 Tom Krieg Introduction Often in Paradox it is necessary to pass a variable between two forms, or even between many forms which may be opened at different times and by different scripts and startup forms. For example, your application may begin with a startup form which calls a "sign on" form in which the user fills in his or her user name and a password which are then verified. Your application's forms may require that username to check whether the user is permitted to access the form and the data it contains. To avoid having to enter user name every time a form is opened, I pass the user name to every form in my application via a library. The forms then verify the user and allow or deny access. The library which contains this variable also happens to contain many methods shared between many forms, an example being a method which allocates a unique long integer primary key to new table records. I therefore open the library global to the desktop in my startup form (a main menu), which means the library (and the variables it contains) is cached until the startup form itself is closed. The first step in this technique for passing a variable between forms is to write the library methods which will receive and pass the variable. Library In the library's Var method, declare the variable you wish to set and pass between forms. In this example I'll call the variable strVariable and declare it as a string. Var strVariable String endVarNext, you'll need to write a method which takes the variable from the passing form and assigns it to the variable strVariable. I've called this method PassVar. Create a new method in the library and call it PassVar. This method will be called by the method or procedure which passes the variable to the library. method PassVar(strInputVar String) strVariable = strInputVar endMethodThen you'll need to write the method which returns the variable to any method or procedure requesting it. I've called this method ReadVar. method ReadVar() String Return strVariable endMethodNow check your syntax, save the library and close it. You'll need to declare the library in every form which either passes the variable to the library or reads the variable from the library. Let's begin by coding the form which passes the variable to the library for other forms to access later. The Form Which Passes The Variable In the Var method of the form, declare both the library and the variable to be passed to other forms. Var CustomLib Library strVariable String endVarIn the Uses method of the form, you'll need to declare an external call to an objectPAL library. Uses objectPAL PassVar(strVarToBePassed String) endUsesIn the init event of the form, open the library. Remember to tidy up in the close event. method init(var eventInfo Event) doDefault CustomLib.open("MyLibraryName") ;// where MyLibraryName is the name of your library endMethod method close(var eventInfo Event) if eventInfo.isPreFilter() then ;// This code executes for each object on the form: else ;// This code executes only for the form: if CustomLib.isAssigned() then CustomLib.close() endif endif endmethodLet's assume the user's entered a value in a form and it's been validated or you've read a value from a table, or you want to pass another value to the library and make it available to another form or forms. First, assign whatever value you want to pass, to the variable strVariable. strVariable = (whatever value you want to pass)When you want to pass the variable to the library (and subsequently to another form), call the library method PassVar, passing strVariable as an argument. CustomLib.PassVar(strVariable)Next, we'll code another form to read the variable from the library. Remember that any form can read this variable at any time because the library in which it's been set has been opened global to the desktop (unless, of course, another form or script has reset the variable, or the startup form itself has been closed). The Form Which Reads The Variable Again, In the Var method of the form, declare both the library and the variable to be read. Var CustomLib Library strVariable String endVarIn the Uses method of the form, you'll again need to declare an external call to an objectPAL library. Uses objectPAL ReadVar() String endUsesIn the init event of the form, open the library. Remember to tidy up in the close event. method init(var eventInfo Event) doDefault CustomLib.open("MyLibraryName") ;// where MyLibraryName is the name of your library endMethodI'm assuming that you want to read the variable from the libray in the init event because your form's behaviour may depend on the value of that variable, but you can do this in any object of the form. After the library "open" statement, just call the library method which returns the variable stored in the library, and assign the value returned to the variable you've declared. strVariable = CustomLib.ReadVar()So your complete init event will be: method init(var eventInfo Event) doDefault CustomLib.open("MyLibraryName") ;// where MyLibraryName is the name of your library strVariable = CustomLib.ReadVar() endMethod Summary It's sometimes necessary to allocate a value to a variable when an application starts and make that value available to many forms, methods and procedures throughout the application. This is one way of doing that. Part 2 - Passing Multiple Variables 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. ![]() |
![]() |
|