简体   繁体   English

将此C#词典转换为VB.NET

[英]Convert this C# dictionary to VB.NET

How do I convert the following C# code to VB.NET? 如何将以下C#代码转换为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"}}
};

I believe the answer is that VB.NET 3.5 does not support collection initialization syntax. 我相信答案是VB.NET 3.5不支持集合初始化语法。

VB.NET in .NET 4 does support collection initializers as follows: .NET 4中的VB.NET确实支持集合初始化程序 ,如下所示:

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")

You want something like this (for .NET 3.5): 您想要这样的东西(对于.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))

There are a few decent C# <--> VB.NET converts online as well. 也有一些不错的C#<-> VB.NET也可以在线转换。 I use http://www.developerfusion.com/tools/convert/csharp-to-vb/ to get: 我使用http://www.developerfusion.com/tools/convert/csharp-to-vb/获得:

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

Then build each List(Of String) and add to ValidHtmlTags separately. 然后构建每个List(Of String)并分别添加到ValidHtmlTags。 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. 我不确定您是否可以将值列表传递到VB.NET中的List(Of String)构造函数中。

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: 然后在Sub或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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM