下一个循环中的可变声明语法错误

发布于 2025-02-13 19:44:27 字数 1345 浏览 1 评论 0原文

以下代码正常工作。

Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
    Dim Dictionary1 As New Dictionary(Of String, Integer)
    Dim Dictionary2 As New Dictionary(Of String, Integer)
    Dim Dictionary3 As New Dictionary(Of String, Integer)
    Dim Dictionary4 As New Dictionary(Of String, Integer)
    Dim Dictionary5 As New Dictionary(Of String, Integer)

    Dictionary1.Add("George", "36")
    Dictionary2.Add("George", "36")
    Dictionary3.Add("George", "36")
    Dictionary4.Add("George", "36")
    Dictionary5.Add("George", "36")
End Sub

我试图像下一个循环那样缩短上述代码,例如以下代码。

但是以下代码给了我一个错误。

Private Sub Button2_Click(sender As Object, e As RoutedEventArgs) Handles Button2.Click
    Dim Dictionary1 As New Dictionary(Of String, Integer)
    Dim Dictionary2 As New Dictionary(Of String, Integer)
    Dim Dictionary3 As New Dictionary(Of String, Integer)
    Dim Dictionary4 As New Dictionary(Of String, Integer)
    Dim Dictionary5 As New Dictionary(Of String, Integer)

    For i As Integer = 1 To 5
        ("Dictionary" & i).Add("George", "36")
    Next
End Sub

那么,如何解决以下错误?

Following code is working properly.

Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
    Dim Dictionary1 As New Dictionary(Of String, Integer)
    Dim Dictionary2 As New Dictionary(Of String, Integer)
    Dim Dictionary3 As New Dictionary(Of String, Integer)
    Dim Dictionary4 As New Dictionary(Of String, Integer)
    Dim Dictionary5 As New Dictionary(Of String, Integer)

    Dictionary1.Add("George", "36")
    Dictionary2.Add("George", "36")
    Dictionary3.Add("George", "36")
    Dictionary4.Add("George", "36")
    Dictionary5.Add("George", "36")
End Sub

I have tried to shorten the above code like the below code via for next loop.

But the below code gives me an error.

Private Sub Button2_Click(sender As Object, e As RoutedEventArgs) Handles Button2.Click
    Dim Dictionary1 As New Dictionary(Of String, Integer)
    Dim Dictionary2 As New Dictionary(Of String, Integer)
    Dim Dictionary3 As New Dictionary(Of String, Integer)
    Dim Dictionary4 As New Dictionary(Of String, Integer)
    Dim Dictionary5 As New Dictionary(Of String, Integer)

    For i As Integer = 1 To 5
        ("Dictionary" & i).Add("George", "36")
    Next
End Sub

So, how can I solve the following error?

Error in the picture

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

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

发布评论

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

评论(1

小苏打饼 2025-02-20 19:44:27

字典列表

    Dim Dictionary1 As New Dictionary(Of String, Integer)
    Dim Dictionary2 As New Dictionary(Of String, Integer)
    Dim Dictionary3 As New Dictionary(Of String, Integer)
    Dim Dictionary4 As New Dictionary(Of String, Integer)
    Dim Dictionary5 As New Dictionary(Of String, Integer)

    Dim ds As New List(Of Dictionary(Of String, Integer))
    ds.Add(Dictionary1)
    ds.Add(Dictionary2)
    ds.Add(Dictionary3)
    ds.Add(Dictionary4)
    ds.Add(Dictionary5)

    For Each d As Dictionary(Of String, Integer) In ds
        d.Add("George", 37)
    Next

List of Dictionary

    Dim Dictionary1 As New Dictionary(Of String, Integer)
    Dim Dictionary2 As New Dictionary(Of String, Integer)
    Dim Dictionary3 As New Dictionary(Of String, Integer)
    Dim Dictionary4 As New Dictionary(Of String, Integer)
    Dim Dictionary5 As New Dictionary(Of String, Integer)

    Dim ds As New List(Of Dictionary(Of String, Integer))
    ds.Add(Dictionary1)
    ds.Add(Dictionary2)
    ds.Add(Dictionary3)
    ds.Add(Dictionary4)
    ds.Add(Dictionary5)

    For Each d As Dictionary(Of String, Integer) In ds
        d.Add("George", 37)
    Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文