Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.
AINSTANCE()
This function lets you find all the instances of any class. It puts the list into an array. Because you can instantiate a class as many times as you want, it seems like it would be convenient to be able to find all the instances. However, we have yet to actually use this function other than to test it.
nInstanceCount = AINSTANCE( ArrayName, cClassName )
Parameter |
Value |
Meaning |
ArrayName |
Array Name |
The array to hold the instance references. |
cClassName |
Character |
The name of the class for which to find instances. Both VFP base classes and user-defined classes can be specified. |
nInstanceCount |
Positive Number |
The number of items in the array, which equals the number of references to cClassName found. |
0 |
No instances of cClassName were found. |
Unlike the similar ASELOBJ()
function, each element of the array holds the name of an object, not an object reference to the object. Using indirect variable references or macro substitution, you can operate on the objects themselves.
Although you can do something like the previous example, there’s a much easier way to deal with a collection of objects. Simply use an array to hold the object references in the first place. Visual FoxPro allows you to do something like this:
DIMENSION aForms[3]
FOR nCnt = 1 TO 3
aForm[nCnt] = CREATEOBJECT('form')
ENDFOR
Watch out for one thing with AINSTANCE()
. If several variables refer to the same object, you still get one array item for each variable. So, if you’re using the references to change all objects of the class, you may hit some of them more than once. On the other hand, this feature makes AINSTANCE()
ideal for ensuring that you actually release an object when you want to.
Note that AINSTANCE()
shares the same first four letters as AINSERT(); therefore you must use at least five letters for AINSTANCE()
.
AINSTANCE()
may be helpful when you’re dealing with cases where you haven’t created the objects yourself, but you need to find out what’s out there—for example, in a Builder or a handler of some sort.
Note that AINSTANCE()
can’t be used to find out about OLE classes such as Project and File. However, the Application Object
offers collections that let you check on those as needed.
AClass(), AMembers(), Application, Array Manipulation, ASelObj(), CreateObject()