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

Last reviewed: September 29, 1997
Article ID: Q129449
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0

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.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. 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
    
    

  3. Add the following code to the Form_Load event of Form1.

          Sub Form_Load ()
             MsgBox "I code therefore, I am!"
             Call ABC
          End Sub
    
    

  4. 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          : APrg APrgOther VB4ALL VB4WIN vbwin
Technology        : kbvba
Version           : WINDOWS:3.0 4.0
Platform          : WINDOWS
Issue type        : kbprb


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.