hi all
i hava a piece vb code
i convert it to c# but when it convert to C# i have error in my program
can anybody help me?
vb code(this code is true)
Do Until vreadyState = 4
Application.DoEvents()
vreadyState = Me.WebBrowser1.ReadyState
Loop
Dim eleminput As HtmlElementCollection
Dim DocCol As HtmlDocument
DocCol = WebBrowser1.Document
eleminput = DocCol.GetElementsByTagName("input")
c# code
for (; (vreadystate == 4); )
{
Application.DoEvents();
vreadystate = Convert.ToInt32(this.webBrowser1.ReadyState);
}
HtmlElementCollection eleminput = default(HtmlElementCollection);
HtmlDocument DocCol = default(HtmlDocument);
DocCol = webBrowser1.Document;
eleminput =DocCol.GetElementsByTagName("input");
run this code
but my program has error in last line
"Object reference not set to an instance of an object."
how to change this code that solve this error?
Loading
Purushottam RathorePosted Apr 18, 2011, 7:12 AM
Please try to convert your code by using this converter
http://www.tangiblesoftwaresolutions.com/Product_Details/CSharp_to_VB_NET_Converter.html?gclid=CKyJ0rf2pagCFYZ66wodNzReHQ
Sam HobbsPosted Apr 18, 2011, 7:07 PM
I am suspicious of any C# code that uses DoEvents. DoEvents is something that VB programmers used when VB was unable to receive messages directly. DoEvents can be a convenient solution but usually there is a better solution that does not use it. In this case, the solution is probably as easy as double-clicking on the WebBrowser control at design time to create a DocumentComplete event handler instead of looping for a ReadyState of 4.
Suthish NairPosted Apr 18, 2011, 4:24 PM
VulpesPosted Apr 18, 2011, 4:13 PM
VB.Net differs from C# in that local variables are always given a default value if you don't assign them one. In contrast, the C# compiler won't let you use a local variable until it's been assigned a value and it's considered 'unassigned' in the meantime.
VB to C# translators have a problem with this and may come up with weird stuff like the above, though the better ones would substitute null instead.
So I don't think the explanation for the error is anything to do with the translation but, of course, DocCol will be null if there's currently no page loaded in the web-browser control.
Suthish NairPosted Apr 18, 2011, 3:18 PM
HtmlElementCollection eleminput = null;
HtmlDocument DocCol = null;
VulpesPosted Apr 18, 2011, 5:43 AM
It looks like DocCol is null. Are you sure that there is a page loaded in the webbrowser control at that point in the code?