Options strict and late binding

J. Scott Elblein

Well-known member
Joined
Dec 14, 2006
Messages
166
Location
Chicago
Programming Experience
10+
I have this declared up top.
VB.NET:
Public oldDocument As mshtml.IHTMLDocument

And this function.
VB.NET:
    Public Function GetFocusedElement() As mshtml.IHTMLElement

        Dim focusedElement As mshtml.IHTMLElement = Nothing

        If oldDocument.selection.type,mshtml. = "None" Then
            focusedElement = CType(oldDocument.selection.createRange(), mshtml.IHTMLTxtRange).parentElement
        ElseIf oldDocument.selection.type = "Control" Then
            focusedElement = CType(oldDocument.selection.createRange(), mshtml.IHTMLControlRange).item(0)
        End If

        Return focusedElement

    End Function

on the "OldDocument" lines, I get the error "option strict disallows late binding" when I have Option strict on. How do I fix this?
 
For late binding you have to turn Option Strict Off, I would put all the late binding code into it's own class/module and at the very top of just that file put:
VB.NET:
Option Strict Off
which'll turn it off for just that 1 file. Leave it on for the entire project though.
 
I have a similar problem and i was wondering if it is possible to do it with option strict on?

When strict is on, i'm not sure how to get info back from a structure i created and bound to a combo box (cmbExchange).

The button btnAUDAmount calls the conversion of a few different currencies into Australian dollars.

the bit it hates is cmbExchange.SelectedItem.ToValue

I can just switch it off as was mentioned here but it seems to defeat the purpose ... if it's the only way, then at least knowing that will help!!
 
Last edited by a moderator:
I have a similar problem and i was wondering if it is possible to do it with option strict on?

When strict is on, i'm not sure how to get info back from a structure i created and bound to a combo box (cmbExchange).

The button btnAUDAmount calls the conversion of a few different currencies into Australian dollars.

the bit it hates is cmbExchange.SelectedItem.ToValue

I can just switch it off as was mentioned here but it seems to defeat the purpose ... if it's the only way, then at least knowing that will help!!
If you are going to ZIP and attach projects, please ensure all binary files are removed first, which usually means deleting the bin and obj folders. Also, attaching entire projects should be a last resort. Posting the relevant code directly should be the first option.

Your problem isn't similar because, while you ARE trying to use late-binding, you don't need to. All you need is a cast. The SelectedItem property is type Object, because it can refer to any type of Object. If you want to access members of the object's actual type then you need to cast as that type first:
VB.NET:
DirectCast(myComboBox.SelectedItem, SomeType)
that returns a SomeType reference to the object, so you can access members of the SomeType type. You can access the reference directly from the DirectCast statement or you can assign the result to a variable and use that.
 
Back
Top