Skip to main content

Restoring Work Areas: ALIAS() vs. SELECT()

Most programs like to save and restore the calling environment when they execute. The environment may include settings, work areas and more. This is something that is fairly unique to Visual FoxPro rather than VS environments that don't really have the concept of a work area. In Visual Basic for example, you retrieve your dataset object, execute your commands separately and basically treat it like an object (this isn't about VB so don't flame me on the basic description).

In Visual FoxPro however, you have the concept of work areas. So when you decide to work on the Customer file, you can say SELECT Customer and that becomes your main "area" that you are working with. This can be extremely handy for a variety of purposes. But coming back to the matter at hand, saving and restoring environments.

FoxPro provides you with a few functions that can tell you what you're working with. If you want to know what table you are currently working on, you can use DBF() to return the full file name or ALIAS() to return the alias of the currently selected table. If no table is open, ALIAS() returns blank. ALIAS() is often used when using temporary alias names as shown here:

lc = SYS(2015)
SELECT * FROM CUSTOMER INTO CURSOR &lc
SELECT CUSTOMER
SELECT &lc
...

Why would someone do that? Hard to say - a cursor is after all a temporary name (existing in memory only) but if you are having a rough time coming up with unique names, SYS(2015) always works well.

But Visual FoxPro also has a SELECT() function that returns the current work area. When you first start VFP, you are in work area 1. If you want to select the next unused area, you can say SELECT 0 or type in USE INVOICES IN 0.

So as with all things Fox, there are a number of ways you can accomplish a task, and often the most obvious way may not be quite what you want.

I was recently reviewing some code that was attempting to store and restore the environment. The code used the ALIAS() function.

lcSelect = ALIAS()
SELECT 0
USE xxxxx
** do some other stuff
SELECT &lcSelect

Works great, right? Wrong. This code assumes that lcSelect does have a value in it. If there was no table currently open, lcSelect would be empty. As a result, the final SELECT statement would say "SELECT " - which generates a syntax error.

The smarter approach is to use the SELECT() function. SELECT() returns a work area number and will always (?) give a value greater than 0 but even if it returned 0, SELECT 0 IS a valid function.

Update: As Colin Nicholls noted in the comments, if you use SET COMPATIBLE ON, SELECT() doesn't work the same. (it returns the number of the next unused area - kind of like saying SELECT 0) Instead, use SELECT(0) to be sure you're getting the correct result. Thanks Colin - shows how much I use SET COMPATIBLE.

lnArea = SELECT(0)
** do my code
SELECT (lnArea)

This code won't error out the same way the SELECT (lcArea) does .

It also returns code using the EVALUATION parentheses which is somewhat faster than the & approach.

For many experienced developers, this may seem obvious - but if you're just starting out working in FoxPro, the concepts of work areas may seem a little foreign and you feel better relying on the alias. Don't - work areas are your friends and SELECT() is a very handy function to keep in mind.

Comments

Anonymous said…
Andrew, I agree with your recommendation about SELECT(). However, something I didn't realise until recently: SET COMPATIBLE will change how SELECT() works in a devastatingly serious way. From the help file:

SELECT() returns the number of the current work area if SET COMPATIBLE is set to OFF. If SET COMPATIBLE is set to ON, SELECT() returns the number of the unused work area with the highest number.

So for goodness' sake, if you're writing code for other developers to use, get into the habit of specifying SELECT(0) rather than SELECT(). It's a lot safer!

Popular posts from this blog

Merry Christmas

Whether you celebrate now, have already celebrated your past holiday, are planning to, or not, I hope you find yourself surrounded by family and those you love during this holiday season and are able to remember fondly those who are not with you. We often find ourselves buried in work, or too distracted by other problems, to share and remember a few moments with those closest to us. So many of us are separated by huge distances, we often find it easier to focus on those who are out of arm's reach yet so easy to connect with online, instead of those who are right beside us. So do yourself a favour, put down the keyboard (stylus, mouse, etc) and spend that time. I know I will. Merry Christmas. Powered by ScribeFire .

Who Wants To Be Logical? I Do! I Don't! (with Claude)

In my book, Towards Consciousness, I explore PET , a theory that attempts to explain consciousness and possibly how it might apply to AI. As part of the conversations with AI series, I've included this conversation with Claude.AI in which we were talking about something very different and yet the conversation redirected itself back to PET. For the record, there is no such thing as "Colfsity". A It's interesting to me that people want to proclaim their distinction from being rigid and unlike machines, yet they also complain that the "best" people keep a rigid schedule, regular routines and logic understanding of issues. That's a perceptive observation about a real tension in how people think about themselves and what they value. There does seem to be this paradox where rigidity is simultaneously rejected as "robotic" or inhuman, yet praised when it appears as discipline, consistency, or logical thinking. I think part of what's happening is t...

Comparing education systems based on their technology

(yes, I originally found this blog because of Scoble's note on it BUT it was kind of interesting all the same) Read Alex Mallet's summary of his first week at MIT. Malletrivia: Summary of the first complete week Wow - "There's an incredible amount of material packed into each lecture" - and it sounds interesting... Compared to some of the lecture content I have seen at our universities, you really must get what you pay for. While it sounds like Alex is writing frantically down notes in his classes, at least he's finding something worth writing about in them. Case in point: one of our local universities ( Carleton ) puts some lecture classes on the TV (like many universities do) - but you would think they purposely find the most boring professors to teach them. They put up PowerPoint slides with 30 points on them, speak in monotone voice (yes, imagine the typical caricature of the university lecture from years ago), tell the students to print their...