How to gain reference to another form in running memory

D

Dean Slindee

This code was originally written in VS2003, when you needed to keep manual
references (hashtable) of form references in order to be able to refer to
another form already in memory.



Anyway, the question setup go like this:

1.. Assume that there is a hashtable that contains one reference to each
form loaded thus far.
Loaded like this in the form_Load event:

Private Sub frmFacilityHost_Load(.

Dim wr As WeakReference = New WeakReference(Me, False)

If Not hashForm.Contains(Me.Name) Then

hashForm.Add(Me.Name, wr)

End If



2.. Now, in another form, to get a reference to the above form, I use this
code:
Try

Dim wr As WeakReference

Dim frmFacilityHost As frmFacilityHost

wr = DirectCast(hashForm(cfrmFacilityHost), WeakReference)

frmFacilityHost = DirectCast(wr.Target, frmFacilityHost)

Call frmFacilityHost.FormPaint(CType(s1, String))

Catch exc As InvalidCastException

Call WinExcept.ExceptionHandler.Exception(Me.Name, exc)

Finally

'frmFacilityHost = Nothing

End Try



3.. QUESTION: I am assuming that the above frmFacilityHost is one and the
same area of memory as the form in #1. Is this correct? I further assume
that the commented line assigning frmFacilityHost = Nothing is not needed
and would cause a loss of form.


4.. Is this hashtable approach still needed in VS2005? What would the
replacement code look like?


5.. Should I be setting the wr = Nothing is either of these functions?


Thanks,

Dean S
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Dean said:
This code was originally written in VS2003, when you needed to keep manual
references (hashtable) of form references in order to be able to refer to
another form already in memory.

Anyway, the question setup go like this:

1.. Assume that there is a hashtable that contains one reference to each
form loaded thus far.
Loaded like this in the form_Load event:

Private Sub frmFacilityHost_Load(.

Dim wr As WeakReference = New WeakReference(Me, False)

If Not hashForm.Contains(Me.Name) Then

hashForm.Add(Me.Name, wr)

End If



2.. Now, in another form, to get a reference to the above form, I use this
code:
Try

Dim wr As WeakReference

Dim frmFacilityHost As frmFacilityHost

wr = DirectCast(hashForm(cfrmFacilityHost), WeakReference)

frmFacilityHost = DirectCast(wr.Target, frmFacilityHost)

Call frmFacilityHost.FormPaint(CType(s1, String))

Catch exc As InvalidCastException

Call WinExcept.ExceptionHandler.Exception(Me.Name, exc)

Finally

'frmFacilityHost = Nothing

End Try



3.. QUESTION: I am assuming that the above frmFacilityHost is one and the
same area of memory as the form in #1. Is this correct?

Yes, assuming of course that they are in the same application.
I further assume
that the commented line assigning frmFacilityHost = Nothing is not needed
and would cause a loss of form.

It's not needed. It doesn't affect the form, though, it only removes the
reference to the form.
4.. Is this hashtable approach still needed in VS2005? What would the
replacement code look like?

You still need a reference to the form that you want to access, and if
you want to do that using a list, you can.
A Dictionary<string, WeakReference> would be the logical choise in
framework 2.0.

Adding:

If Not formDictionary.ContainsKey(Me.Name) Then
Dim wr As WeakReference = New WeakReference(Me, False)
formDictionary.Add(Me.Name, wr)
End If

Accessing:

Dim wr As WeakReference
If formDictionary.TryGetValue("somename", wr) Then
Dim frmFacilityHost As frmFacilityHost
frmFacilityHost = TryCast(wr.Target, frmFacilityHost)
If frmFacilityHost IsNot Nothing Then
Call frmFacilityHost.FormPaint(CType(s1, String))
End If
End If

(No need to catch InvalidCastException, as it can't occur in this code.)
5.. Should I be setting the wr = Nothing is either of these functions?

No. There is no reason to clear a reference that is going out of scope
anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top