The only real way to have a VFP singleton is as OLE Public class set to Multi Use.
While Init is the event triggered by CreateObject, its return value is not overriding the reference you get back into oRef1, oRef2, or whatever. Foxpro has no such concept as its init isn't a constructor. "This" is already referring to the new instance of the class and so Init already is the aftermath of the construction of an object.
I find this idea based on THIS_ACCESS:
[URL unfurl="true"]http://www.victorespina.com.ve/wiki/index.php?title=SingletonPattern_(VFP)[/url]
Before looking at that it's worth learning how THIS_ACCESS works as a special access method for an object itself. Unlike the property_access methods you can generate when defining a new member (property) of a class, this is for the object itself and gets invoked from access to any member with the member name as parameter. You get standard behavior when returning THIS from THIS_Access (notice you don't return THIS.&cMember) and that's used here to let access to any instance be redirected to a central instance. Also note, this isn't only working for property members, also for methods and events. Redirecting to another object the call to methods also is redirected - using the same parameterization as the original call to THIS was using.
The first instantiation already creates a second object stored as _SCREEN child object and any access to members (except a few methods and properties) is then redirected to this _SCREEN child object. I'd perhaps change this so the first instantiation puts itself (THIS) to a _SCREEN property instead of creating another instance of its class.
The downside of that is, that the THIS_ACCESS method doesn't work with OLE public classes. But then we have Multi Use instantiation for OLE classes.
In a way, this is okay, as OLE class usage always is slower than native class usage. But if you even look for a singleton spanning multiple VFP processes (eg preventing multiple starts of an application) the Multi Use OLE mechanism works that way and there are solutions using some kind of mutex: faq184-1767
PS: You can also find about other instances with AINSTANCE(), but this function only sees instances in variables and (what's not documented) only in variables in scope. I tried putting AINSTANCE into the init so the class init puts a reference to the first instance into a property, but that won't see previous instances stored in local variables, for example, so AINSTANCE() is of limited use.
Bye, Olaf.
Olaf Doschke Software Engineering