Microsoft KB Archive/129449

= PRB: Object Created w/NEW Instantiated When Passed as Argument =

Article ID: 129449

Article Last Modified on 12/9/2003

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q129449



SYMPTOMS
Visual Basic version 4.0 instantiates objects created with the "New" keyword in cases where Visual Basic version 3.0 does not.

This may apply to earlier versions of VBA (specifically 1.0) used in Microsoft Excel 5.0 (16-bit).



RESOLUTION
When passing an object (declared with the New keyword) to a procedure, Visual Basic 4.0 first instantiates the object (if it isn't already instantiated).

This behavior is different than Visual Basic version 3.0. In Visual Basic 3.0, the object isn't instantiated until a member of the object is referenced inside the called procedure. If the object is never referenced, then it is never instantiated. Because a form is not shown when it is instantiated anyway (it is only shown when the Show method is called), the only difference in behavior is that the Form_Load event runs at a different time in Visual Basic 4.0 than in Visual Basic 3.0.

In a TypeOf statement, if an object variable that was declared with the New keyword is being tested, then Visual Basic 4.0 will instantiate the object before doing the test. For example: Dim X As New Form1 If TypeOf X Is Form1 Then ... ' X WILL be instantiated

This is different from Visual Basic 3.0. Visual Basic 3.0 does not instantiate the object in this case. Visual Basic 3.0 can do this because it can determine the type of the object even if the object has not yet been instantiated.



STATUS
This behavior is by design.



Steps to Reproduce Behavior
 Start a new project in Visual Basic. Form1 is created by default.  Add the following code to the General Declarations section of Form1. Sub ABC Dim Y as New Form1 Call DEF(Y) ' The object is instantiated here in Visual Basic 4.0 End Sub

Sub DEF( X as Object) X.Backcolor = 10 'The object is instantiated here in Visual Basic 3.0 End Sub   Add the following code to the Form_Load event of Form1. Sub Form_Load MsgBox "I code therefore, I am!" Call ABC End Sub  Step through the code by pressing the F8 function key or clicking the Step button on the button bar.

The Form_Load event code will fire on the call to DEF in Sub ABC rather than on the code reference to X in DEF. Note that in both versions of Visual Basic, the above code produces runaway recursion and should not be used.

Keywords: kbprb KB129449

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.