Object instantiation characteristics

Author Message
robwm

  • Total Posts : 118
  • Scores: 0
  • Reward points : 0
  • Joined: 3/10/2009
  • Location: Seattle, WA
  • Status: offline
Object instantiation characteristics Friday, December 16, 2011 3:50 AM (permalink)
0
Hi,
 
I have had this question burning in the back of my mind for some time now. When you instantiate an object (examples below), do you need to do this only once for the life of a script or does it need to be done again. For example, if I write the lines shown below at the beginning of the script will they need to be introduced again?
 
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = Wscript.CreateObject("WScript.Shell")
 
I have also noticed that many people set these to 0 at some point in the script. Can this be done at the end of the script or should it be done each time after you use the script? After you set these objects to 0, will they need to be instantiated again later if you need them?
 
What is best practice when it comes to instantiating objects?
 
Thanks,
Rob
 
#1
    ebgreen

    • Total Posts : 8227
    • Scores: 98
    • Reward points : 0
    • Joined: 7/12/2005
    • Status: offline
    Re:Object instantiation characteristics Friday, December 16, 2011 3:58 AM (permalink)
    0
    You only need to instantiate the object once for the life of the script. There may be issues around scope where you might want or need to instantiate the same object more than once, but in general it is not required. I doubt that you have seen people setting those objects to 0. I suspect instead that you have seen them set those objects equal to nothing which is not at all the same thing. That used to be a very important habit because VBScript was not particularly good about releasing memory for objects that were no longer in use. These days it is much better about doing that on it's own, but it certainly doesn't hurt anything to explicitly release the objects.
    "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
    Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
    http://www.visualbasicscript.com/m_47117/tm.htm
     
    #2
      robwm

      • Total Posts : 118
      • Scores: 0
      • Reward points : 0
      • Joined: 3/10/2009
      • Location: Seattle, WA
      • Status: offline
      Re:Object instantiation characteristics Friday, December 16, 2011 5:02 AM (permalink)
      0
      Oh right! It's early in the day for me...
       
      set oShell = Nothing
      set oFSO = Nothing
       
      I had a feeling that was how it worked based on my experience with them. I just wanted someone to lay down the law!
       
      Thank you!
       
      #3
        Wakawaka

        • Total Posts : 456
        • Scores: 23
        • Reward points : 0
        • Joined: 8/27/2009
        • Status: offline
        Re:Object instantiation characteristics Friday, December 16, 2011 5:36 AM (permalink)
        0
        When you sent an object to Nothing, you are basically remove the references your script has to the object.  This is turn will be recognized by the GarbageCollector to collect the object and finalize them which in turn will then free up the resources for the system to use elsewhere.  The only real reason to use it currently is to release an object which takes up a large amount of resources and your script still have work to do and perhaps with objects that access a database.  I know if you didn't set it to "Nothing" in the past, it could lock tables and such in the databases.
         
        You see it a lot at the end of scripts, which isn't really necessary since when the script ends all the resources lose the reference anyways and are GC'ed.
         
        EDIT:  I just repeated ebgreen, sorry :P
        <message edited by Wakawaka on Friday, December 16, 2011 5:41 AM>
         
        #4
          ebgreen

          • Total Posts : 8227
          • Scores: 98
          • Reward points : 0
          • Joined: 7/12/2005
          • Status: offline
          Re:Object instantiation characteristics Friday, December 16, 2011 6:26 AM (permalink)
          0
          All good. Never know if I have this stuff right or not anyway, so confirmation is always welcome. :)
          "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
          Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
          http://www.visualbasicscript.com/m_47117/tm.htm
           
          #5
            robwm

            • Total Posts : 118
            • Scores: 0
            • Reward points : 0
            • Joined: 3/10/2009
            • Location: Seattle, WA
            • Status: offline
            Re:Object instantiation characteristics Saturday, December 17, 2011 5:03 AM (permalink)
            0
            Thanks very much for taking the time to clarify this guys!
             
            #6

              Online Bookmarks Sharing: Share/Bookmark

              Jump to:

              Current active users

              There are 0 members and 1 guests.

              Icon Legend and Permission

              • New Messages
              • No New Messages
              • Hot Topic w/ New Messages
              • Hot Topic w/o New Messages
              • Locked w/ New Messages
              • Locked w/o New Messages
              • Read Message
              • Post New Thread
              • Reply to message
              • Post New Poll
              • Submit Vote
              • Post reward post
              • Delete my own posts
              • Delete my own threads
              • Rate post

              2000-2012 ASPPlayground.NET Forum Version 3.9