Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global variables 1

Status
Not open for further replies.

CleoMan

Programmer
Jun 18, 2003
110
ZA
I would like to know how it is possible to go about using a library to create global variables.

When the user logs on to the program we want to capture his/her user code and password to use through the rest of the program. We cannot use a table to keep track of this information, so we need to store the values somewhere without beeing dependant of a form being open all the time.

So am I on the right track by wanting to use a library, and if so can someone please give me some advice as to do this?

As always gratefull
Richard
 
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 )
Code:
strPassword = aPassword
Code:
endMethod

(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
   &quot;SAVEVARS.LSL&quot;
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:

Code:
Var
   lib  Library
endVar

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( &quot;:work:savevars.lsl&quot; ) then
      errorShow( &quot;Can't Load Library&quot;, &quot;Click [>>] for details.&quot; )
   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( &quot;Can't Save Password&quot;,
               &quot;Reason: Please enter a password to save.&quot; )
      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 = &quot;&quot;
      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. :)
 
Well, Lance first off: That is a lot of info! Just all the effort in guiding me step by step is impressive. So just for that alone: Thanks!

Alright general question,if the form that loads the library and assigns the values to the library, is closed does that mean that the library itself loses the values?

Thanks again Lance

Richard

PS: I got the printing problems fixed ;-)
 
I got the password problem fixed now, so once again thanks for the help Lance!

-R-
 
As an alternative, we create a small table in the users :priv: directory and write the info there. We write the data to it on startup and destroy it when the user quits.

Mac :)

&quot;There are only 10 kinds of people in this world... those who understand binary and those who don't&quot;

langley_mckelvy@cd4.co.harris.tx.us
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top