Greg Reichert Blog

My Blog is mainly about myself and software development in general, but mainly Visual FoxPro.

My Photo
Name:
Location: Memphis, Tennessee, United States

Tuesday, September 05, 2006

Late Binding of Objects

VFP 6.0+


After seeing the “My” object introduced to VFP 9.0, I decided to write a similar object for accessing many of the functions of FoxPro and windows called System.prg. In my first attempt, I encapsulated the FoxPro functions of SYS() and SET() to allow better readability. For example, Instead of the coding as SYS(3), the object version would be oSystem.UniqueName(). This helps make my coding more understandable. Well, after a while, I added other FoxPro functions like HOME, OS, VERSION, and etc. As the number of methods grew, so did the object. After a while, the time it took to instantiate the object grew also. Now recent I added methods that accessed the operating system using Win API and WMI queries. This was cool, but it was adding several seconds to the start up of my application. Something had to done to speed up the creation of the objects. The solution was not to create the individual object until they are accessed for the first time. The following snippet of code demonstrates how this is done.

loObject = CREATEOBJECT("MainClass")
? loObject.FirstObject.FirstMethod()

DEFINE CLASS MainClass as Container

FirstObject = .NULL.

PROCEDURE FirstObject_access
this.FirstObject = IIF(ISNULL(this.FirstObject), ;
CREATEOBJECT("FirstClass"), this.FirstObject)
RETURN this.FirstObject
ENDPROC

ENDDEFINE

DEFINE CLASS FirstClass as Container

PROCEDURE FirstMethod
RETURN "Hello World"
ENDPROC

ENDDEFINE

If we step through using the debugger, you will notice that after loObject is created, the property FirstObject is NULL. But when execute the second line in attempt to call FirstMethod, the FirstObject_Access method is called first, therefore creating the object for first. Then the method FirstMethod called afterwards. With several dozen objects, and hundreds of methods, this means of delaying the creation of the object until they are needed speeded up the startup of the application.

You can download the latest copy of this class here

0 Comments:

Post a Comment

<< Home