【发布时间】:2012-03-12 09:10:14
【问题描述】:
我需要对字段进行一些比较和改组,(我来自更多的 PHP 背景,并且使用数组完全不同,正如在此处阅读许多内容后建议的那样,我知道 VB.NET 使用 LIST 和 Dictionary 更容易。
我有一个字典(字符串,字典(字符串,字符串)),它填得很好,但是在我尝试读出来之后,它总是空的....
下面我填写一些内容。我正在谈论的 var matchwholename VALUES 部分内的 fillDictionary。 我填写了 fillDictionary 并将其添加到 matchesWholename 之后我清除它,因为它处于循环中,并且还会有更多。
Dim matchesTotal As New List(Of String)
Dim fillDictionary As New Dictionary(Of String, String)
Dim matchesWholename As New Dictionary(Of String, Dictionary(Of String, String))
Try
If ds.Tables("matchesWholename").Rows.Count > 0 Then
For Each dr As DataRow In ds.Tables("matchesWholename").Rows
If Not matchesWholename.ContainsKey(CStr(dr("key"))) Then
fillDictionary.Add("programme", CStr(dr("programme")))
fillDictionary.Add("remark", CStr(dr("remark")))
fillDictionary.Add("key", CStr(dr("key")))
fillDictionary.Add("displayname", CStr(dr("prop_value")))
Console.WriteLine("count before insert {0}", fillDictionary.Count)
matchesWholename.Add(CStr(dr("sanction_key")), fillDictionary)
End If
If Not matchesTotal.Contains(CStr(dr("key"))) Then
matchesTotal.Add(CStr(dr("key")))
End If
fillDictionary.Clear()
Next
End If
Catch ex As Exception
Console.WriteLine("Err: {0}", ex.Message)
End Try
当我稍后尝试像这样读出时,它什么也没有返回...... 我实际上是否将填充字典的副本发送到其中?还是我的 clear() 方法清除了其中的内容?
Dim pair As KeyValuePair(Of String, Dictionary(Of String, String))
Dim subpair As KeyValuePair(Of String, String)
For Each pair In matchesWholename
' Display Key and Value.
For Each subpair In pair.Value
Console.WriteLine("{0}, {1}, {2}", pair.Key, subpair.Key, subpair.Value)
Next
Next
【问题讨论】:
标签: vb.net