PRB: Accessing Property in Initialize Event Causes Infinite LoopLast reviewed: August 4, 1997Article ID: Q171979 |
The information in this article applies to:
SYMPTOMSIf a property of an object is referred to in its own Initialize event, then Visual Basic will fire the Initialize event for that object so that it can be instantiated. An infinite recursion will result until stack space runs out and you receive the following error:
Run-time error '28': Out of stack spaceThis will occur for "built-in" objects, such as Forms, and objects created using Visual Basic classes.
CAUSEBefore a property of an object can be accessed, the object must be initialized. This is, in fact, the whole point of the Initialize event; it allows the developer to place the object in a satisfactory state by initializing private data before the properties and methods of an object are invoked. The properties and methods may have to rely on the internal data of the object being in a stable state. Consequently, the Initialize event must be completed before a property or method of an object can be accessed, and therefore it is not unexpected that the Initialize event is fired again when a property is referred to in the Initialize event.
RESOLUTIONIf you are trying to set a property of a Visual Basic built-in object, then it may be possible to refer to the property in an event that occurs later than the Initialize event. For example, rather than setting the Caption of a Form in the Initialize event, set it in the Load event or the Activate event. An alternative to referring directly to Form1, which is a global object pointer, is to use the Me keyword. When referring directly to Form1 in the Initialize event of the Form, the global object Form1 has not been created so Visual Basic tries to instantiate it. This process causes the infinite recursion. Use the Me Keyword to avoid this recursion. If you are trying to set a property for an object created with class modules in Visual Basic, it may be appropriate to create a Method for initializing data or setting initial values for properties, for which the client program would be responsible for calling after instantiating an object. (Perhaps a good analogous example of this is the OLE API function OLEInitialize, which client programs written in C must explicitly call before using OLE services). Another way to accomplish this is to set the private data on which a property relies in the Initialize event. For example, if a Visual Basic property of a class uses a private data member like so:
Private lPrivProp1 As Long Public Property Get lProp1() As Long lProp1 = lPrivProp1 End Property Public Property Let lProp1(vNewValue As Long) lPrivProp1 = vNewValue End Propertythen in the Initialize event of the class module in which this property is contained, it is not appropriate to try and set the public property of the class, but rather to set the private data member to a value.
STATUSThis is by design.
MORE INFORMATION
Steps To Reproduce the Problem
Steps To Work Around the Problem
|
Additional query words: hang
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |