views:

149

answers:

4

one more question on converting c# to vb. conversion tool is not doing a good job. Please let me know. Thanks

  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"}}
};
+1  A: 

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.

Jason Snelders
Jason - New List(Of String) {"name", "value"} is not working. I tried to change the curly braces to ( , but still its not working.
vamsivanka
I don't think VB.Net supports collection initializers until v4.0
Gabriel McAdams
@vamsivanka - Sorry, jumping between C# and VB a lot at the moment so momentary confusion. Have updated my answer to show a better solution.
Jason Snelders
Jason Thanks for the input. it works great.
vamsivanka
+5  A: 

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
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
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
+1  A: 
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
Thanks Gabriel. It works great.
vamsivanka
I'm glad my answer solved your problem
Gabriel McAdams
+6  A: 

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