Results 1 to 4 of 4
  1. #1
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Set object (from function) to Nothing? (Office XP SP-2)

    Suppose I have a function that returns an object. For example:

    Function CreateThatStyle() As Word.Style

    Now suppose I have a procedure that calls the function but has no need for the returned object, so I just use this line:

    Call CreateThatStyle

    Will this potentially leave a lingering object after the procedure ends? In other words, should I go to the trouble of declaring an object variable that I don't need and assigning the return value of the function to it, for the sole purpose of setting it to Nothing for clean-up purposes? -- like this:

    Dim oS As Word.Style
    Set oS = CreateThatStyle
    Set oS = Nothing

    Or is no "clean-up" left undone if I simply call the function using the Call statement?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Set object (from function) to Nothing? (Office

    This is a really interesting question; I have no idea. But why use a Function in this case and not a Sub, or change the return value to a Boolean (e.g., blnSuccess)?

  3. #3
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Set object (from function) to Nothing? (Office XP SP-2)

    The following from MSDN should clarify :
    <hr> Several object variables can refer to the same actual object. When Nothing is assigned to an object variable, that variable no longer refers to an actual object. When several object variables refer to the same object, memory and system resources associated with the object to which the variables refer are released only after all of them have been set to Nothing, either explicitly using Set, or implicitly after the last object variable set to Nothing goes out of scope.<hr>
    Most likely in your case, once your function has completed, the variable is implicitly cleared.

    Andrew C

  4. #4
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Set object (from function) to Nothing? (Office XP SP-2)

    Thanks for the response. I'm operating under the assumption that it remains the "recommended practice" to explicitly set object variables to Nothing rather than rely on Visual Basic to release the corresponding resources when all the references go out of scope. If that understanding is out of date or otherwise incorrect and I can stop being so darned fastidious, I'd love to hear it.

    But assuming 9 out of 10 leading VB gurus continue to advise explicitly setting object variables to Nothing (including, to take the most common example, an object variable whose scope is limited to a single procedure), my question was whether calling a function with an object as its (potential) return value even creates an object reference if I call it with the Call statement and don't assign its return value to anything (or otherwise refer to its return value in any way).

    I assume nobody thinks this is an issue with built-in VBA functions -- e.g., I assume nobody worries about the possibility of a lingering object if they open a new Word document with the line Documents.Add (rather than oDoc = Documents.Add) -- and I'd hope the same applies to homemade functions, but I don't know for sure.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •