|
-
Aug 24th, 2010, 12:23 AM
#1
Thread Starter
Hyperactive Member
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
-
Aug 24th, 2010, 12:39 AM
#2
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.
-
Aug 24th, 2010, 01:16 AM
#3
Thread Starter
Hyperactive Member
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
-
Aug 24th, 2010, 01:29 AM
#4
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?
-
Aug 24th, 2010, 01:39 AM
#5
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.
-
Aug 24th, 2010, 01:59 AM
#6
Thread Starter
Hyperactive Member
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
-
Aug 24th, 2010, 02:05 AM
#7
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:
Dim input = TryCast(element, IHTMLInputElement) If input IsNot Nothing Then 'Use input here. Return End If Dim button = TryCast(element, IHTMLButtonElement) If button IsNot Nothing Then 'Use button here. Return End If '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.
-
Aug 24th, 2010, 02:25 AM
#8
Thread Starter
Hyperactive Member
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
-
Aug 24th, 2010, 02:43 AM
#9
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:
Friend Function GetElementValue(ByVal element As HtmlElement) As String Dim domElement As Object = element.DomElement Dim input = TryCast(domElement, IHTMLInputElement) If input IsNot Nothing Then Return input.vlaue End If Dim button = TryCast(domElement, IHTMLButtonElement) If button IsNot Nothing Then Return button.value End If '... Return Nothing 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:
Friend Function GetElementValue(ByVal element As HtmlElement) As String Return element.DomElement.value 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.
-
Aug 24th, 2010, 10:16 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|