Work Area Manager - Go Green with VFP
At my onsite gig I'm maintaining a VFP app that's been ported from FoxPro DOS to FoxPro Windows to VFP. It has a lot of old code in it, that suffice it to say, is suboptimal. Introducing new, well-written code into it is often challenging. The old code includes very little defensive coding and is full of assumptions about the environment it runs in. It's really dependant on what cursors are open, filters, indexes, record pointers and so on. The app is modal and there's a lot of "drill-down" type behavior, so things that the environment is depenendant upon may have longn ago in the call stack. After a lot of messing around with hard coding specfic states I needed to save and restore I finally decided to just create a class that would handle this stuff for me. If you remember the cSelect class from Codebook or VFE this is basically just an extended version of it that handles all open work areas. There's nothing complex about it, so I'm not going to get into the specifics of the code here.
In the zip I've linked here, is a class named WorkAreaManager. Basically before you're going to do something that tampers with the environment, you just create an instance of it. Then you can go about your merry way messing with the open cursors, opening new cursors, whatever it is you need to do. When the instance is destroyed (either explicitly or by letting it's variable fall out of scope), any cursors that were not previously opened are closed and the record pointer, filter, index order and alias are restored for any cursors that were previously open.
The only potential gotcha that I'm aware of is it doesn't handle temporary cursors if your code closes them, but that hasn't been an issue for me so far.
Give it a try.