How to check if a window or container control is still open?

This issue has caught me out a few times before I cottoned on. To illustrate, if I have the following code that instantiates a new window and saves a reference to it

mDownloadWindow = New DownloadWindow //mDownloadWindow is a property and DownloadWindow has Implicit Instance set to Off

After mDownloadWindow has done something then is closed, I would have assumed that mDownloadWindow would now = Nil, but it doesn’t.

So if I have code to check that the window is still open before trying to manipulate it, I’ve been using

If mDownloadWindow <> Nil Then 'do something only if mDownloadWindow is still open End If

But I discovered that this doesn’t work as mDownloadWindow <> Nil even after being closed. To make it work, I have to remember to put mDownloadWindow = Nil in the close event. This workaround is fine, but I’m sure I will forget to put it in at some stage and it will trip me up again. Is there a better way to check if a window or container control is still open?


Check out

Thank you Michael! Exactly what I was looking for!

Ok, still getting my head around the concept of strong and weak references, but getting there…

By changing my code to this, I can allow only 1 DownloadWindow to be open:

[code]//mDownloadWindow is a property As WeakRef
If mDownloadWindow = Nil Or mDownloadWindow.Value = Nil Then
Var dw As New DownloadWindow
mDownloadWindow = New WeakRef(dw)

If dw.InitializeWindow Then
MessageBox(“There was an error setting up the download”)
End If
End If[/code]