How to write the following code from VB.NET to C#?

Code is:

Public Class Form1

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim item As HtmlElementCollection
        item = WebBrowser1.Document.GetElementsByTagName("span")
        Dim ht As HtmlElement
        For Each ht In item
            MsgBox(ht.DomElement.attributes("class").value.ToString)
        Next
    End Sub
End Class

In C#, it does not find the .attributes("class").value.ToString() part.

Answers


The reason your code works in VB.Net and not in C# is that you are using Option Strict Off (perhaps implicitly by omitting Option Strict On). This tells the compiler to assume there will be an Attributes member in the DOMElement, which is an Object.

In C# there is no equivalent to Option Strict On in general but if you use the dynamic type you can do the same thing.

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    var item = webBrowser1.Document.GetElementsByTagName("span");
    foreach(HtmlElement ht in item)
    {
        dynamic element = ht.DomElement;
        MessageBox.Show(element.Attributes["class"].Value.ToString());
    }
}

This allows you to access properties of types you don't know at design-time as long as they will exist at run-time, similar to Object in VB.Net with Option Strict Off. Be careful though, since this allows you to write code somewhat recklessly (similar to VB :) so you should add some checking and exception handling in this case.


using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1

{

private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    HtmlElementCollection item = default(HtmlElementCollection);
    item = WebBrowser1.Document.GetElementsByTagName("span");
    HtmlElement ht = default(HtmlElement);
    foreach ( ht in item) {
        Interaction.MsgBox(ht.DomElement.attributes("class").value.ToString);
    }
}
}

I think the correct value would be here:

ht.GetAttribute("class")

The Complete Code:

HtmlElementCollection items;
items = WebBrowser1.Document.GetElementsByTagName("span");
HtmlElement ht;
foreach (ht in items) {
    MessageBox.Show(ht.GetAttribute("class"));
}

Your 'foreach' loop needs to have the variable type included in C#:

foreach (HtmlElement ht in item)
{
    MessageBox.Show(ht.DomElement.Attributes["class"].Value.ToString());
}

Also, since the 'DomElement' property of the HtmlElement class returns 'Object', you'll have to cast "ht.DomElement" to the appropriate type before invoking the 'Attributes' property. I just guessed that it was an indexer, but since 'DomElement' returns Object, it's just that - a guess.

e.g.,

foreach (HtmlElement ht in item)
{
    MessageBox.Show((SomeType)(ht.DomElement).Attributes["class"].Value.ToString());
}

Need Your Help

sleep() in Javascript

javascript html5 modal-dialog blocking

Suppose I want to block Javascript execution for certain time for some weird reason, how can I do that. There is no sleep() in JS. Pls don't say do a while() loop because that's bad. I can do a win...