I have a snippet of code below that as far as I know should not work but does. I have always thought that when you create an object within a function that object loses scope and automatically releases its memory once the function ends. In this case the function returns the reference, ends, and the calling function is able to use the reference with no problem. Can someone tell me why?
' Used for all server objects
Public oServer As Object
Private Sub DoSomething()
Set oServer = CreateAndReturnServerObject("Fin2k.clsClass", ""
msgbox oServer.GetKidCount
Set oServer = Nothing
End Sub
Private Function CreateAndReturnServerObject(svr_obj As String, URL As String) As Object
Dim genRDS As New RDS.DataSpace
Dim gen As Object
Set gen = genRDS.CreateObject(svr_obj, URL)
Set genRDS = Nothing
Set CreateAndReturnServerObject = gen
End Function
' Used for all server objects
Public oServer As Object
Private Sub DoSomething()
Set oServer = CreateAndReturnServerObject("Fin2k.clsClass", ""
msgbox oServer.GetKidCount
Set oServer = Nothing
End Sub
Private Function CreateAndReturnServerObject(svr_obj As String, URL As String) As Object
Dim genRDS As New RDS.DataSpace
Dim gen As Object
Set gen = genRDS.CreateObject(svr_obj, URL)
Set genRDS = Nothing
Set CreateAndReturnServerObject = gen
End Function