以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- [求助]多级字典如何存储? (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=159505) |
||||
-- 作者:小美菜 -- 发布时间:2020/12/28 21:55:00 -- [求助]多级字典如何存储? 我想通过“ Sub字典保存函数(MainDc As Dictionary(Of String,Object) , ParamArray Args() As Object)”这样的函数实现 Sub字典保存函数(tDc ,“政策”,"水利",“供水”, "大水村",“已经施工”),让tDc(“政策”)("水利")(“供水”)("大水村")=“已经施工”,不管其中哪个主键没有,都能自动添加 |
||||
-- 作者:有点蓝 -- 发布时间:2020/12/28 22:30:00 -- 试试 Dim tDc2 As Dictionary(of String,object) = tDc Dim s1() As String = Args If s1.length > 1 Then Dim s3 As String = s1(s1.length - 1) For i As Integer = 0 To s1.length - 2 If tDc2.ContainsKey(s1(i)) = False Then Dim dd As new Dictionary(of String,object) tDc2.Add(s1(i),dd) tDc2 = dd End If Next tDc2(s1(s1.length - 2)) = s3 End If
|
||||
-- 作者:小美菜 -- 发布时间:2020/12/30 22:52:00 -- 用 Dim oldDc As New Dictionary(Of String, Object) SetDicKeyVal( oldDc ,"A","AA","AAA","AAAA","VV" ) SetDicKeyVal( oldDc ,"A","AA","AAA","DDDD","RERERE" ) SetDicKeyVal( oldDc ,"A","AA","AAA","CCCC","CPCPCP" ) SetDicKeyVal( oldDc ,"A","BB","BBB","EEEE" ) SetDicKeyVal( oldDc ,"A","BB","CCC","FFFF" ) SetDicKeyVal( oldDc ,"A","BB","DDD","GGGG" ) Dim XDc As Dictionary(Of String, Object) = oldDc("A")("AA")("AAA") For Each tKey As String In xDc.Keys Output.Show(tKey) Next 结果是 AAAA DDDD CCCC 用 Dim oldDc As New Dictionary(Of String, Object) SetDicKeyVal( oldDc ,"A","AA","AAA","AAAA","VV" ) SetDicKeyVal( oldDc ,"A","AA","AAA","DDDD","RERERE" ) SetDicKeyVal( oldDc ,"A","AA","AAA","CCCC","CPCPCP" ) SetDicKeyVal( oldDc ,"A","BB","BBB","EEEE" ) SetDicKeyVal( oldDc ,"A","BB","CCC","FFFF" ) SetDicKeyVal( oldDc ,"A","BB","DDD","GGGG" ) Output.Show( oldDc("A")("BB")("DDD")) 结果报错:Dictionary(Of String, Object) 转换到类型“string”的转换无效,我之前用一直都是好的 |
||||
-- 作者:有点蓝 -- 发布时间:2020/12/31 9:30:00 -- SetDicKeyVal函数代码发上来 |
||||
-- 作者:小美菜 -- 发布时间:2020/12/31 10:39:00 --
附件就是,谢谢 |
||||
-- 作者:有点蓝 -- 发布时间:2020/12/31 11:38:00 -- Public Sub SetDicKeyVal(MainDc As Dictionary(Of String,Object) , ParamArray Args() As Object) \'As Dictionary(Of String,Object) Dim tDc2 As Dictionary(of String,object) = MainDc Dim s1() As object = Args If s1.length > 1 Then Dim s3 As String = s1(s1.length - 1) For i As Integer = 0 To s1.length - 3 If tDc2.ContainsKey(s1(i)) = False Then Dim dd As new Dictionary(of String,object) tDc2.Add(s1(i),dd) tDc2 = dd Else tDc2 = tDc2(s1(i)) End If Next tDc2(s1(s1.length - 2)) = s3 End If End Sub |
||||
-- 作者:小美菜 -- 发布时间:2020/12/31 23:03:00 -- 谢谢蓝老师,为何我的内部函数有的是好的,有的说找不到,奇怪了,在函数管理器中明明在的 |
||||
-- 作者:有点蓝 -- 发布时间:2020/12/31 23:12:00 -- 应该是某个函数有问题,导致编译异常,然后后面的函数就编译不了,所以找不到。看看是哪个名称的函数找不到,检查函数列表前面的函数代码。 另外全局代码有问题也可能导致函数编译异常
|
||||
-- 作者:小美菜 -- 发布时间:2021/1/1 17:30:00 -- 蓝老师的SetDicKeyVal,我看了一天都没有看懂,如果我在最后一级发现有相同键及值,则删除之,又该如何实现? |
||||
-- 作者:有点蓝 -- 发布时间:2021/1/3 9:52:00 -- 就是一个递归的操作,一级一级处理。 同一个路径不可能会出现相同键及值的,请举例说明 |