很难描述,但我正在尝试使用List集合作为SortedList集合中的参数并检索这些值。我相信我的设置是正确的,因为它不会返回任何错误,但我无法检索值(没有返回任何内容)。有什么想法吗?
这是我的代码:
Dim MySortedList As New SortedList(Of Int16, List(Of String))
Dim MyInnerList As New List(Of String)
MyInnerList.Add("Item 1a")
MyInnerList.Add("Item 1b")
MySortedList.Add(1, MyInnerList)
MyInnerList.Clear()
MyInnerList.Add("Item 2a")
MyInnerList.Add("Item 2b")
MySortedList.Add(2, MyInnerList)
MyInnerList.Clear()
Dim testlist As New List(Of String) 'not sure if needed.
For Each kvp As KeyValuePair(Of Int16, List(Of String)) In MySortedList
testlist = kvp.Value
For Each s As String In testlist
Response.Write(s & "<br>")
Next
Next
答案 0 :(得分:1)
在添加到main / SortedList后清除了MyInnerList
:
MyInnerList.Clear()
由于它是一个对象,SortedList
中存储的值也被清除(它们是相同的):
Dim MySortedList As New SortedList(Of Int16, List(Of String))
Dim MyInnerList As New List(Of String)
MyInnerList.Add("Item 1a")
MyInnerList.Add("Item 1b")
MySortedList.Add(1, MyInnerList)
' create a new list object for the next one
MyInnerList = New List(Of String)
MyInnerList.Add("Item 2a")
MyInnerList.Add("Item 2b")
MySortedList.Add(2, MyInnerList)
Dim testlist As List(Of String) 'New is not needed.
For Each kvp As KeyValuePair(Of Int16, List(Of String)) In MySortedList
testlist = kvp.Value
' For Each s As String In kvp.Value will work just as well
For Each s As String In testlist
Console.Write(s & "<br>")
Next
Next
输出:
项目1a
项目1b
项目2a
项目2b