简体   繁体   中英

VBScript setting an multiple objects to Nothing doesn't work when passing those into array

As I mentioned in the question, I have specific subroutine which let's say sets and utilizes multiple objects like below:

Sub Subroutine1
    Set a = CreateObject("Somthing")
    Set b = CreateObject("SomthingElse")
    Set c = CreateObject("SomthingOther")
    Set d = CreateObject("Another")
    ' Some operation done with those objects
    Call NothingifyObjects(Array(a, b, c, d))
    If a Is Nothing Then
        MsgBox "Yes"
    Else
        MsgBox "No"
    End If
End Sub

And I am trying to set all those to Nothing by passing as Array of Objects into another function:

Function NothingifyObjects(arrObjectArray)
    For i = 0 to UBound(arrObjectArray)
        Set arrObjectArray(i) = Nothing
    Next
    arrObjectArray = Null
End Function

But the MsgBox still prints No , why and how do I make this work with Function of minimum code lines?

You are initiating an Array containing 4 object references. Those 4 object reference values are copied from a, b, c and d.

You then pass that newly created Array into your function NothingifyObjects. NothingifyObjects takes your newly created Array and iterates over it, setting every element in the array to to "Nothing". This won't affect your original 4 local variables in the scope of Subroutine1 however since the Array only holds copies of a, b, c and d.

You could change your code to this:

Sub Subroutine1
    Set a = CreateObject("Somthing")
    Set b = CreateObject("SomthingElse")
    Set c = CreateObject("SomthingOther")
    Set d = CreateObject("Another")
    ' Some operation done with those objects
    Call NothingifyObjects(a, b, c, d)
    If a Is Nothing Then
        MsgBox "Yes"
    Else
        MsgBox "No"
    End If
End Sub

Function NothingifyObjects(a, b, c, d)
    Set a = Nothing
    Set b = Nothing
    Set c = Nothing
    Set d = Nothing
End Function

This will work since VBScript will pass ByRef by default.

If you really just want to reduce the number of code lines you have, all i can think of is:

Function NothingifyObjects(a, b, c, d)
    Set a = Nothing : Set b = Nothing : Set c = Nothing : Set d = Nothing
End Function

... which will work, maybe not as pretty to look at though.

VBscript does not support optional arguments so you have to set a predefined number of arguments for your function. Otherwise you could have made something like what you are suggesting.

If it was me i would just inline the Set to Nothing statement like so:

Sub Subroutine1
    Set a = CreateObject("VBScript.RegExp")
    Set b = CreateObject("VBScript.RegExp")
    Set c = CreateObject("VBScript.RegExp")
    Set d = CreateObject("VBScript.RegExp")
    ' Some operation done with those objects
    Set a = Nothing
    Set b = Nothing
    Set c = Nothing
    Set d = Nothing
    If a Is Nothing Then
        MsgBox "Yes"
    Else
        MsgBox "No"
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM