检索嵌套列表VB.NET

时间:2017-01-25 03:42:10

标签: asp.net vb.net for-loop nested-loops

我在处理列表时有点生疏。我宣布了2个公开名单

Public Shared individualDetails As New List(Of String)
Public Shared catList As New List(Of List(Of String))

添加按钮,将字符串添加到individualLists

Protected Sub btnAddLevel_Click(sender As Object, e As EventArgs) Handles btnAddLevel.Click

    individualDetails.Add(ddlCountry.SelectedItem.Value)
    individualDetails.Add(txtAvailability.Text)
    individualDetails.Add(txtRemarks.Text)

    catList.Add(individualDetails)

End Sub

用户可以执行多项添加以添加到catList

 Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    For i As Integer = 0 To catList.Count

        For x As Integer = 0 To individualDetails.Count

             MsgBox(individualDetails(x))

        Next x
    Next i
End Sub

我遇到内部for循环问题。我做了调试,我知道如果我添加两次,individualDetails将包含6个项目。

请问如何向catList添加多个列表,而不是将catList组合列表添加到string lorem = @"Lorem ipsum dolor ,Fizz! amet, consectetur adipiscing elit. Nulla laoreet .Fizz! facilisis. Mauris :Buzz! efficitur nisi. Morbi consectetur hendrerit pretium. Ut tempor mauris ,Buzz! consequat convallis. Quisque lacinia felis in ex lacinia, luctus malesuada purus tristique. Nullam tincidunt dolor sit amet metus vehicula, Foo amet fringilla lacus ultrices. Maecenas semper .Fizz! le justo molestie, non ultrices Buzz! consequat. Donec :Foo? facilisis tellus. Nunc pulvinar egestas turpis. Donec sed ex rhoncus, Fizz dolor eget, aliquam orci..Foo! tincidunt pellentesque dolor sed mollis."; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10000; ++i) { sb.Append(lorem); } int lensb = sb.Length; Random rnd = new Random(1); for (int i = 0; i < 100000; ++i) { sb[rnd.Next(lensb)] = '!'; sb[rnd.Next(lensb)] = '?'; sb[rnd.Next(lensb)] = '.'; sb[rnd.Next(lensb)] = ','; sb[rnd.Next(lensb)] = ':'; sb[rnd.Next(lensb)] = lorem[rnd.Next(lorem.Length)]; } string sample = sb.ToString(); ?我错误地添加了这些列表吗?

1 个答案:

答案 0 :(得分:0)

catList是列表的容器。容器中只有一个列表individualDetails。您的btnAddLevel事件处理程序不断向该列表添加新条目,而不是创建新列表。

individualDetails不应该是Public Shared变量;它应该是您的事件处理程序的本地:

Protected Sub btnAddLevel_Click(sender As Object, e As EventArgs) Handles btnAddLevel.Click
    Dim individualDetails As New List(Of String)

    individualDetails.Add(ddlCountry.SelectedItem.Value)
    individualDetails.Add(txtAvailability.Text)
    individualDetails.Add(txtRemarks.Text)

    catList.Add(individualDetails)

End Sub

您需要更改调试代码才能查看列表;使用像

这样的东西
For i As Integer = 0 To catList.Count - 1
    Dim individualDetails = catList(i)

    For x As Integer = 0 To individualDetails.Count - 1 

         MsgBox(individualDetails(x))

    Next x
Next i

您可以对代码进行其他改进,但这应该可以帮助您。