Richard,
Since I'm not entirely certain of your ObjectPAL experience, the following walkthrough may seem a bit oversimplified. If it does, I apologize.
In any event, you're basically looking at a three step process.
1) Create the library interface for the variable. Include methods for setting and reading your variable.
2) Prototype the library in your calling form
3) Add code to open and use the library.
The following walkthrough demonstrates each step by creating a library that stores and retrieves a password from a library variable.
To illustrate, let's assume you want to store two variables in your library: username and password.
Step 1: Creating the library:
1. From the main Paradox menu, choose File | New | Library. This creates a new library and opens it in an Editor design window.
2. Save your library to SETVARS.LSL. (It's a good idea to regularly save your library, so you won't lose code changes if it closes accidentally.)
3. Press Ctrl+Space to display the Object Explorer. Note that you have two tabs (in recent versions of Paradox): Methods and Events. In general, you add code to libraries by creating custom methods.
4. Select the Methods tab and then double-click <New Method> to display the New Method dialog. When it appears, type
setPassword and then choose OK. This give you an Editor window containing a blank custom method called setPassword.
5. Move the cursor between the parenthesis in the first line of your custom method and then type
APassword String. When finished, your first line (also called the declaration) should look something like this:
Code:
method setPassword( APassword String )
Congratulations, you've just added a parameter to the declaration of your new method. You'll see why we did this as we continue.
6. Now, add the following code to your method to svae the value that's passed in that parameter to a variable in the library.
Code:
method setPassword( aPassword String )
(Note that I've left the method's header and footer line to give you an idea of what it should like like when you're finished.)
7. Now, we need to declaration a variable to hold the password. As you may have noticed while filling in the code, we're calling that strPassword. (The str prefix is a naming convention designed to remind us the variable's type over time.)
To do this, press Ctrl+Space and then double-click
Var in the Methods list. When the Var window appears, declare strPassword as a String variable, as shown in the following code sample:
Code:
Var
strPassword String ; holds the user's password.
endVar[code]
You don't need to add the header or footer; I've left them to give you a sense of the finished product.
8. Now, create a Method called getPassword and declare it as follows:
[code]method getPassword() String
return strPassword
endMethod
(Refer to Step 4 above if you need a hiont for this step)
9. Save your library and close it.
OK; that's the hardest part. From here, it gets a bit easier.
Step 2: Prototyping a library in a form
To prototype methods in a library, you need to tell Paradox the name of the file containing your methods with the Uses statement. In this step, we'll create a form that supports the methods created in the previous step.
1. Create a new form and save it as VarsTest.fsl.
2. Add an unbound field object to your form and then name it fldPassword. (Remember that you name objects by right-clicking them and then choosing Properties from the shortcut menu.)
3. Add two buttons. Change to caption of the first to
Set Password and the other to
Get Password. for best results, name the first btnGetPass and the second btnSetPass.
4. Now, select the form's page object, press Ctrl+Space, select the Methods tab (if it isn't already selected), and then double-click
Uses. This opens the Uses window.
5. Declare your library as follows:
Code:
Uses ObjectPAL
"SAVEVARS.LSL"
endUses
6. Close the Uses window to save your declaration.
7. Save your form to avoid losing your changes.
Prototyping tells Paradox what to look for in the library. It uses this information to verify your code.
Now it's time to load and use your library.
Step 3: Loading and calling a library
Libraries need to associated with a library and loaded before they can be used. This step shows both tasks.
1. Select your form's page object. Press Ctrl+Space, select the Methods tab, open the
Var window, and then add the following:
2. With the page object still selected, press Ctrl+Space, select the Events tab, and then double-click
open(). When the Editor window appears, add the following code:
Code:
method open(var eventInfo Event)
if not lib.open( ":work:savevars.lsl" ) then
errorShow( "Can't Load Library", "Click [>>] for details." )
endIf
endMethod
3. Select the Get Password button, press Ctrl+Space, open the Events tab, and then open the
pushButton() event editor. When it appears, add the following code:
Code:
method pushButton(var eventInfo Event)
if fldPassword.isBlank() then
beep()
msgStop( "Can't Save Password",
"Reason: Please enter a password to save." )
fldPassword.moveTo()
else
lib.setPassword( fldPassword.Value )
endIf
endMethod
4. Select the GetPassword button and then add the following code to its
pushButton() event:
Code:
method pushButton(var eventInfo Event)
if not fldPassword.isBlank() then
fldPassword.Value = ""
sleep( 250 )
endIf
fldPassword.Value = lib.getPassword()
endMethod
5. Save your form and then run it. You should be able to save a password reload it.
Please note that this is only one way to tackle the problem. There are several other ways and I'm sure you'll think of several improvements as you work on this. Nevertheless, these are the basic tasks involved.
Hope this helps...
-- Lance
P.S. Please let me know if this helps, as I intend to expand it into a larger tutorial for my site.
