Option Strict On disallows late binding

D

Dean Slindee

I have a hash table that stores references to all open forms. If I
implement Option Strict On, then the following two bottom statements are
flagged for late binding. Is there a way to keep the hash table of form
references and still implement Option Strict On?

Public Const cfrmMain As String = "frmMain"

Public tblHash As New Hashtable

Dim frm As Form = CType(tblHash(cfrmMain).Target, Form)

Dim frmMain As frmMain = frm

Thanks,

Dean Slindee
 
H

Herfried K. Wagner [MVP]

Dean Slindee said:
I have a hash table that stores references to all open forms. If I
implement Option Strict On, then the following two bottom statements are
flagged for late binding. Is there a way to keep the hash table of form
references and still implement Option Strict On?

Public Const cfrmMain As String = "frmMain"

Public tblHash As New Hashtable

Dim frm As Form = CType(tblHash(cfrmMain).Target, Form)

Dim frmMain As frmMain = frm

\\\
Dim frmMain As frmMain = _
DirectCast(DirectCast(tblHash(cfrmMain), <...>).Target, frmMain)
///

Instead of '<...>' specify the type of the item contained in the hashtable.
 
R

Roger

I have a hash table that stores references to all open forms.

Isn't there a Forms array or something like it?
ie: Application.Forms
Roger
 
D

Dean Slindee

Hope you have another approach, because unfortunately I cannot get the
DirectCast to work.
Problem is, there is no .Target method associated with DirectCast. I have
tried:
frmMain = DirectCast(DirectCast(tblHash(cfrmMain), Form).Target,
frmMain) 'syntax incorrect .Target not valid

frmMain = DirectCast(DirectCast(tblHash(cfrmMain), Form), frmMain)
'syntax OK, InvalidCastException

This simplied DirectCast raises an InvalidCastException, like the statement
above:

Dim frm As Form

frm = DirectCast(tblHash(cfrmMain), Form)

In review, I am trying to avoid a late bind while using Option Strict On.

Perhaps Microsoft should invent an Option NotSoStrict On (cheers)

Thanks,

Dean Slindee
 
L

Larry Lard

Can I ask what this .Target property is?

Also, if this is failing:
Dim frm As Form

frm = DirectCast(tblHash(cfrmMain), Form)

it would be interesting to see the code where you put things INTO the
hashtable in the first place.
 
H

Herfried K. Wagner [MVP]

Dean Slindee said:
Hope you have another approach, because unfortunately I cannot get the
DirectCast to work.
Problem is, there is no .Target method associated with DirectCast. I have
tried:
frmMain = DirectCast(DirectCast(tblHash(cfrmMain), Form).Target,
frmMain) 'syntax incorrect .Target not valid

frmMain = DirectCast(DirectCast(tblHash(cfrmMain), Form), frmMain)
'syntax OK, InvalidCastException

It seems that you are not storing 'Form' objects in the hashtable. Post the
code you are using to add items to the 'Hashtable'.
 
D

Dean Slindee

Here is how I load the hash table with form references:
Public tblHash As New Hashtable

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

If Not tblHash.Contains(cfrmMain) Then

tblHash.Add(cfrmMain, wr)

End If

The whole reason that I store references to forms in the hashtable is to be
able to fire events on any of those forms in the hashtable from the
currently active form. Perhaps there is a better way to keep track of these
form references, but this works as is. It just does not pass Option Strict
On because it is a late binding reference. To me this seems like an classic
(unavoidable) late binding situation, but perhaps you know of a better way
to keep track of form references...

Here is an example of the code that calls an event on a referenced form
after getting the late binding reference:

Dim etvw As New
System.Windows.Forms.TreeViewEventArgs(frmMain.tvwAdmin.SelectedNode,
TreeViewAction.ByMouse)

Call frmMain.tvwAdmin_AfterSelect(Me, etvw)

Thanks for looking,

Dean Slindee
 
H

Herfried K. Wagner [MVP]

Dean Slindee said:
Here is how I load the hash table with form references:
Public tblHash As New Hashtable

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

If Not tblHash.Contains(cfrmMain) Then

tblHash.Add(cfrmMain, wr)

Instead of casting to 'Form' cast to 'WeakReference' first. Then cast the
value of the 'WeakReference''s 'Target' property to the form type.
 
D

Dean Slindee

As you suggested, and for the record, this works:
Dim wr As WeakReference

Dim frmMain As frmMain

Try

wr = DirectCast(tblHash(cfrmMain), WeakReference)

frmMain = DirectCast(wr.Target, frmMain)

Thanks Herfried,

You are a great resource!
 

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