-1

How do I rewrite this code as VB.net? Online conversion tools gag when it tries to convert the Return part of the functions.

private static DataColumn[] ParseColumns(string tableHtml)
{
    MatchCollection headerMatches = Regex.Matches(
        tableHtml,
        HeaderPattern,
        ExpressionOptions);

    return (from Match headerMatch in headerMatches
            select new DataColumn(headerMatch.Groups[1].ToString())).ToArray();
}

private static DataColumn[] GenerateColumns(MatchCollection rowMatches)
{
    int columnCount = Regex.Matches(
        rowMatches[0].ToString(),
        CellPattern,
        ExpressionOptions).Count;

    return (from index in Enumerable.Range(0, columnCount)
            select new DataColumn("Column " + Convert.ToString(index))).ToArray();
}

Specifically, the online tools will give me something like this:

    Private Shared Function GenerateColumns(rowMatches As MatchCollection) As DataColumn()
     Dim columnCount As Integer = Regex.Matches(rowMatches(0).ToString(), CellPattern, ExpressionOptions).Count

     Return (From index In Enumerable.Range(0, columnCount)New DataColumn("Column " + Convert.ToString(index))).ToArray()   End Function

Its this last part that is incorrect:

(From index In Enumerable.Range(0, columnCount)New DataColumn("Column " + Convert.ToString(index))).ToArray()

and I don't understand what this C# code is trying to do -- it looks like LINQ or something like that.

Thanks!

toha
  • 5,095
  • 4
  • 40
  • 52
Jeff
  • 1,362
  • 14
  • 17
  • Do not use Regex for processing html. Html is valid xml and for example `XDocument` or `XElement` classes will be better approach – Fabio Aug 11 '16 at 03:01
  • @Fabio: in the general case, that's true, but if you know the nature of the content you are parsing, and it is a limited scope, regexes or other forms of matching/extraction are fine. – siride Aug 11 '16 at 03:08
  • @siride, parsing HTML with Regex can bring different problems - for example this: [http://stackoverflow.com/a/1732454/1565525](http://stackoverflow.com/a/1732454/1565525). Using Xml parsers will be more clear, readable and maintainable way to process html. – Fabio Aug 11 '16 at 03:14
  • @Fabio: yes, we've all seen that stupid answer. It's linked everywhere on the internet, and it's right only in the general sense. Again, know your domain. You don't have to use the most powerful tool to solve a given problem. Otherwise, we'd be building full compiler toolchains to deal with every new data format. If you are, say, screen-scraping consistently formatted tables of data, a regex is probably fine. – siride Aug 11 '16 at 03:18

1 Answers1

3

That is LINQ code. The great thing about it is that it is entirely syntactic sugar for a chain of method calls. The from is used to introduce a loop variable (effectively) from an enumerable, and the select generates a result. The latter actually corresponds to a method Select(). You can rewrite the C# LINQ code as follows:

Enumerable.Range(0, columnCount).Select(index => new DataColumn("Column " + index)).ToArray();

This creates an array of DataColumn objects, one for each number from 0 to the given columnCount. You can use basically the same syntax in VB, except the lambda will look a little different:

Enumerable.Range(0, columnCount).Select(Function(index) New DataColumn("Column " & index)).ToArray()

I assume that's all you needed help with.

siride
  • 200,666
  • 4
  • 41
  • 62
  • Thanks! Based on this, I also found that these also work: "Return (From headerMatch In headerMatches Select New DataColumn(headerMatch.Groups(1).ToString())).ToArray()" and "Return (From index In Enumerable.Range(0, columnCount) Select New DataColumn("Column " & Convert.ToString(index))).ToArray()" – Jeff Aug 11 '16 at 07:19