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

发布于 2024-08-16 14:29:40 字数 393 浏览 6 评论 0原文

如何将以下 C# 代码转换为 VB.NET?

转换工具做得不太好。

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"}}
};

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"}}
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我早已燃尽 2024-08-23 14:29:40

我相信答案是VB.NET 3.5不支持集合初始化语法。

.NET 4 中的 VB.NET 支持集合初始值设定项 如下:

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

前面的代码示例与下面的代码等效。

Dim days = New Dictionary(Of Integer, String)
days.Add(0, "Sunday")
days.Add(1, "Monday")

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")
烟凡古楼 2024-08-23 14:29:40

你想要这样的东西(对于.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))

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))
星星的轨迹 2024-08-23 14:29:40
Private Shared ReadOnly ValidHtmlTags As Dictionary(Of String, List(Of String)) = New Dictionary(Of String, List(Of String))

然后在 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")
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")
兔姬 2024-08-23 14:29:40

有一些不错的 C# <--> VB.NET 也可以在线转换。我使用 http://www.developerfusion.com/tools/convert/csharp -to-vb/ 获取:

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

然后构建每个 List(Of String) 并分别添加到 ValidHtmlTags。例如。

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

我不确定您是否可以将值列表传递到 VB.NET 中的 List(Of String) 构造函数中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文