Creation of an object

Posted on 2006-04-17
Last Modified: 2010-04-07
Hi Experts, Could you please let me know the difference between the below 2 declarations.

dim x as object
set x = new object

and dim x as new object


Question by:nmretd
    LVL 92

    Assisted Solution

    by:Patrick Matthews
    Hi nmretd,

    In the first case, the object represented by x is not created until the Set statement executes.  In the second, the
    object is implicitly created right away.

    The first construction is preferable, because it provides greater control over when your object is created.



    Author Comment

    Thanks Patrick. Wondering why there are 2 ways though.. what are the advs and disadvs if any. Please guide. Example would be appreciable.

    LVL 9

    Accepted Solution

    If you:

    dim x as object
    set x = new object

    Then later: set x = nothing

    Then still later refer to x in your code, x will equal nothing.  However if you:

    dim x as new object

    Then later: set x = nothing

    Then still later refer to x in your code, a new instance of x will be created.  That is usually a bad thing.

    Author Comment

    Great guys. I am cleared.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now