Results 1 to 10 of 10

Thread: option strict on disallow late binding

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    option strict on disallow late binding

    I have a webbrowser in the form, ele is a HtmlElement on the page in the browser. now, I want to get ele's value (if it exists). if I turn off option strict, then
    If (ele.DomElement.value() IsNot Nothing) Then
    val = ele.DomElement.value.ToString.ToLower()
    End If

    this works. but if I turn on option strict, then it has error for "disallow late binding".

    I guess I should use ctype to convert ele.domElement, but what type should I convert it to? htmlelement will not work.


    thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: option strict on disallow late binding

    You have to reference the COM library MSHTML.dll to be able to refer to the actual type that DomElement would return, as the documentation for the HtmlElement.DomElement property clearly states. I'll say it again: always read the documentation first.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    Re: option strict on disallow late binding

    I actually checked online before asking question. the problem I have is: once I cast ele.DomElement to IHTMLElement (or IHTMLElement2, 3...), I can not find that they have the property for .value (corresponding to ele.DomElement.value()).

    Maybe need cast it to other class/interface?


    thanks

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: option strict on disallow late binding

    Add the reference but don't change the code, then put a breakpoint on that line and run the project. When the debugger breaks, what does it say the type of ele.DomElement is?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: option strict on disallow late binding

    I just created a new project and added a reference to MSHTML.dll. I then opened the Object Browser and expanded the relevant nodes. Presumably, if you're getting the 'value' property, this object represents an 'input' element in the HTML code. I looked down the list of types and paid attention to those that appeared relevant. I found the IHTMLInputElement interface, which has a 'value' property. I'm guessing that that's the type you want.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    Re: option strict on disallow late binding

    thanks. that's a good idea.

    it turns out that I may have to use late binding. for example, I need search an object with name "f", I do not know the object is an IHTMLInputElement or IHTMLButtonElement, or else. So, I can not simply cast it to one of them.

    thanks, really appreciate the help

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: option strict on disallow late binding

    If you know that it's got a 'value' property then you presumably know that it must be one of a subset of types that has a 'value' property. In that case you do something like this:
    vb.net Code:
    1. Dim input = TryCast(element, IHTMLInputElement)
    2.  
    3. If input IsNot Nothing Then
    4.     'Use input here.
    5.  
    6.     Return
    7. End If
    8.  
    9. Dim button = TryCast(element, IHTMLButtonElement)
    10.  
    11. If button IsNot Nothing Then
    12.     'Use button here.
    13.  
    14.     Return
    15. End If
    16.  
    17. 'Etc.
    If you think that that's too laborious then you could certainly stick with late-binding. If you do though, make sure that you set Option Strict On for the project and then create one or more separate code files, declaring a partial class if appropriate, to contain just the code that specifically requires late-binding. You can then set Option Strict Off for just those files.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    Re: option strict on disallow late binding

    thanks. I will go for late binding because there are too many places to do this cast (the code was developed by others and has many lines, )

    thanks again

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: option strict on disallow late binding

    The fact that it's in a lot of places doesn't necessarily mean it's a lot of work either way. All you need is one function. If you want to use early-binding all the way:
    vb.net Code:
    1. Friend Function GetElementValue(ByVal element As HtmlElement) As String
    2.     Dim domElement As Object = element.DomElement
    3.     Dim input = TryCast(domElement, IHTMLInputElement)
    4.  
    5.     If input IsNot Nothing Then
    6.         Return input.vlaue
    7.     End If
    8.  
    9.     Dim button = TryCast(domElement, IHTMLButtonElement)
    10.  
    11.     If button IsNot Nothing Then
    12.         Return button.value
    13.     End If
    14.  
    15.     '...
    16.  
    17.     Return Nothing
    18. End Function
    Wherever you want the value from an HtmlElement, you simply call that method and pass the HtmlElement as an argument. If you want to use late-binding then, as I said, create a separate code file and set Option Strict Off at the top, then add this function:
    vb.net Code:
    1. Friend Function GetElementValue(ByVal element As HtmlElement) As String
    2.     Return element.DomElement.value
    3. End Function
    Now you can leave Option Strict On for the project and you retain its benefits for the rest of your code. Again, simply call that function anywhere you need the value from an HtmlElement. It should probably be a simple Find & Replace operation to add the method calls.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: option strict on disallow late binding

    If you want to use late binding put the code that uses it in it's on Partial Class file and only turn Option Strict off in that file. Shouldn't be more than one method, right?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width