1

How do I convert the following C# code to VB.NET?

The conversion tool is not doing a good job.

private static readonly Dictionary<string, List<string>> ValidHtmlTags = new Dictionary<string, List<string>> {
    { "param", new List<string>() {"name","value"}},
    { "object", new List<string>() {"id","type"}},
    { "embed", new List<string>() {"src","type","wmode"}}
};
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vamsivanka
  • 792
  • 7
  • 16
  • 36

4 Answers4

10

I believe the answer is that VB.NET 3.5 does not support collection initialization syntax.

VB.NET in .NET 4 does support collection initializers as follows:

Dim days = New Dictionary(Of Integer, String) From
    {{0, "Sunday"}, {1, "Monday"}}

The previous code example is equivalent to the following code.

Dim days = New Dictionary(Of Integer, String)
days.Add(0, "Sunday")
days.Add(1, "Monday")
Eilon
  • 25,582
  • 3
  • 84
  • 102
  • Eilon - Its similar but little bit difference becz, I am using this dictionary to html syntax check. the first value "param" is the tag name and second one {"name,"value"} are the attributes of that tag. – vamsivanka Dec 30 '09 at 22:23
  • 1
    My point is only that VB.NET in .NET 3.5 doesn't support even this simple syntax, so it definitely doesn't support an even more complex syntax of nesting object initializers. In .NET 4 they have added support for this in VB.NET as you can see in the article I linked to. The article contains several examples of advanced scenarios, including scenarios similar to yours. – Eilon Dec 30 '09 at 22:28
6

You want something like this (for .NET 3.5):

Shared Sub New()
    Dim dict As New Dictionary(Of String, List(Of String))
    Dim l1 As New List(Of String)
    l1.Add("name")
    l1.Add("value")
    dict.Add("param", l1)
    Dim l2 As New List(Of String)
    l2.Add("id")
    l2.Add("type")
    dict.Add("object", l2)
    Dim l3 As New List(Of String)
    l3.Add("src")
    l3.Add("type")
    l3.Add("wmode")
    dict.Add("embed", l3)
    MyClass.ValidHtmlTags = dict
End Sub

Private Shared ReadOnly ValidHtmlTags As Dictionary(Of String, List(Of String))
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

There are a few decent C# <--> VB.NET converts online as well. I use http://www.developerfusion.com/tools/convert/csharp-to-vb/ to get:

Private Shared ReadOnly ValidHtmlTags As New Dictionary(Of String, List(Of String))() 

Then build each List(Of String) and add to ValidHtmlTags separately. eg.

Dim paramList As New List(Of String)()
paramList.Add("name")             
paramList.Add("value")          
ValidHtmlTags.Add("param", paramList)              

I'm not sure you can pass in a list of values into the List(Of String) constructor in VB.NET.

Arul Dinesh
  • 550
  • 4
  • 15
Jason Snelders
  • 5,471
  • 4
  • 34
  • 40
1
Private Shared ReadOnly ValidHtmlTags As Dictionary(Of String, List(Of String)) = New Dictionary(Of String, List(Of String))

Then somewhere in a Sub or a Function:

ValidHtmlTags.Add("param", New List(Of String))
ValidHtmlTags("param").Add("name")
ValidHtmlTags("param").Add("value")

ValidHtmlTags.Add("object", New List(Of String))
ValidHtmlTags("object").Add("id")
ValidHtmlTags("object").Add("type")

ValidHtmlTags.Add("embed", New List(Of String))
ValidHtmlTags("embed").Add("src")
ValidHtmlTags("embed").Add("type")
ValidHtmlTags("embed").Add("wmode")
Gabriel McAdams
  • 56,921
  • 12
  • 61
  • 77