Use
for %i in (1,2,3,4,5) do cscript nothing.vbs %i
to call
' ############################################################################
''# Nothing.vbs - demonstrates Nothing
' ############################################################################
Option Explicit
' ############################################################################
''#
' ############################################################################
Class cNothing
Public m_sName
Public m_oOther
Private Sub Class_Initialize()
WScript.Echo "cNothing::Class_Initialize() called."
End Sub
Private Sub Class_Terminate()
WScript.Echo "cNothing::Class_Terminate() called for " + m_sName + "."
End Sub
Public Function named( sName )
m_sName = sName
Set named = Me
End Function
End Class
' ############################################################################
''# main
' ############################################################################
Dim oFrs, oSec
Select Case WScript.Arguments( 0 )
Case 1
WScript.Echo "(1) Start of Main"
WScript.Echo "Frs.Class_Terminate will be called after EndOfMain"
Set oFrs = New cNothing.named( "Frs" )
WScript.Echo "End of Main"
Case 2
WScript.Echo "(2) Start of Main"
WScript.Echo "Thanks to nothing, Frs.Class_Terminate will be called before EndOfMain"
Set oFrs = New cNothing.named( "Frs" )
Set oFrs = Nothing
WScript.Echo "End of Main"
Case 3
WScript.Echo "(3) Start of Main"
WScript.Echo "Thanks to nothing, both destructors will be called before EndOfMain"
Set oFrs = New cNothing.named( "Frs" )
Set oSec = New cNothing.named( "Sec" )
Set oSec = Nothing
Set oFrs = Nothing
WScript.Echo "End of Main"
Case 4
WScript.Echo "(4) Start of Main"
WScript.Echo "After marriage (circular reference), nothing works no more."
WScript.Echo "The reaper reaps after EndOfMain"
Set oFrs = New cNothing.named( "Frs" )
Set oSec = New cNothing.named( "Sec" )
Set oFrs.m_oOther = oSec
Set oSec.m_oOther = oFrs
Set oSec = Nothing
Set oFrs = Nothing
WScript.Echo "End of Main"
Case 5
WScript.Echo "(5) Start of Main"
Set oFrs = New cNothing.named( "Frs" )
Set oSec = New cNothing.named( "Sec" )
Set oFrs.m_oOther = oSec
Set oSec.m_oOther = oFrs
WScript.Echo "other of Frs: " + oFrs.m_oOther.m_sName
WScript.Echo "other of Sec: " + oSec.m_oOther.m_sName
Set oSec = Nothing
On Error Resume Next
WScript.Echo "other of Sec: " + oSec.m_oOther.m_sName
WScript.Echo "*** " + Err.Description + " ***"
On Error GoTo 0
WScript.Echo "other of Sec (via Frs.m_oOther): " + oFrs.m_oOther.m_oOther.m_sName
WScript.Echo "QED: oSec still lives, but can't be accessed via oSec variable."
WScript.Echo "End of Main"
End Select
That will give you something like
C:\wis\_vbs\0506\dev\forum
cscript nothing.vbs 1
(1) Start of Main
Frs.Class_Terminate will be called after EndOfMain
cNothing::Class_Initialize() called.
End of Main
cNothing::Class_Terminate() called for Frs.
C:\wis\_vbs\0506\dev\forum
cscript nothing.vbs 2
(2) Start of Main
Thanks to nothing, Frs.Class_Terminate will be called before EndOfMain
cNothing::Class_Initialize() called.
cNothing::Class_Terminate() called for Frs.
End of Main
C:\wis\_vbs\0506\dev\forum
cscript nothing.vbs 3
(3) Start of Main
Thanks to nothing, both destructors will be called before EndOfMain
cNothing::Class_Initialize() called.
cNothing::Class_Initialize() called.
cNothing::Class_Terminate() called for Sec.
cNothing::Class_Terminate() called for Frs.
End of Main
C:\wis\_vbs\0506\dev\forum
cscript nothing.vbs 4
(4) Start of Main
After marriage (circular reference), nothing works no more.
The reaper reaps after EndOfMain
cNothing::Class_Initialize() called.
cNothing::Class_Initialize() called.
End of Main
cNothing::Class_Terminate() called for Frs.
cNothing::Class_Terminate() called for Sec.
C:\wis\_vbs\0506\dev\forum
cscript nothing.vbs 5
(5) Start of Main
cNothing::Class_Initialize() called.
cNothing::Class_Initialize() called.
other of Frs: Sec
other of Sec: Frs
*** Objekt erforderlich ***
other of Sec (via Frs.m_oOther): Frs
QED: oSec still lives, but can't be accessed via oSec variable.
End of Main
cNothing::Class_Terminate() called for Frs.
cNothing::Class_Terminate() called for Sec.
From that you see:
(1) Set oX = Nothing will decrement the reference counter of oX. If
this counter reaches 0, oX will be destructed and its memory
released. The destructor could do other important stuff like
closing files or destroying 'big' variables (e.g. a dictionary
containing many items). So being able to specify the moment
when destruction takes place is valuable (Scripting Guys and
their opinions not withstanding).
(2) Circular references make it more difficult to achieve this
goal. But it's not impossible. Can you add code to nothing.vbs
to get:
cscript nothing.vbs 6
(6) Start of Main
After marriage (circular reference) everything works if you work for it.
cNothing::Class_Initialize() called.
cNothing::Class_Initialize() called.
cNothing::Class_Terminate() called for Sec.
cNothing::Class_Terminate() called for Frs.
End of Main