以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 递归函数的返回值 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=191196) |
-- 作者:sloyy -- 发布时间:2024/3/30 11:47:00 -- 递归函数的返回值 我编写了一个递归函数,因为要返回2个参数,所以我在全局变量里定义了2个数组 Public pxqs() As Integer Public pxjh() As String 调试的时候,为了监控看数据是否正确,在函数最末一段写了一段: For k As Integer=0 To pxqs.length-1 MessageBox.Show( pxqs(k) & ":" & pxjh(k) ) next 显示证明 输出正常 但是 当我在主程序调用时 Functions.Execute("多文件递归", dlg.SelectedPath,lg) Array.Sort(pxqs,pxjh) MessageBox.Show(pxqs.length) 显示 pxqs.length=0 取不到数据 哪里出问题了? |
-- 作者:有点蓝 -- 发布时间:2024/3/30 11:57:00 -- 完整代码发上来 |
-- 作者:sloyy -- 发布时间:2024/3/30 12:09:00 -- Dim kjqs As New List(Of Integer) Dim jh As New List(Of String) Dim lg As New List(Of String) Dim path As String = args(0) Dim ls = args(1) Dim gs() As String Dim strs,qs1 As String strs=Forms("main").Controls("TextBox2").text strs = strs.Replace(vblf,"") Dim ds() As String = strs.Split(vbcr) For Q As Integer = 0 To ds.Length - 1 Dim fs() As String= ds(Q).Split(":") If fs.Length = 2 Then Dim qs As String =trim(fs(0)) Dim KJHMS() As String=trim(fs(1)).split(" ") If kjhms.Length=6 Then For Each file As String In FileSys.GetFiles(path) If file.Contains(".txt") Then ls.add(file) strs = FileSys.ReadAllText(file) strs = strs.Replace(vblf,"") Dim rs() As String = strs.Split(vbcr) For i As Integer = 0 To rs.Length - 1 Dim cs() As String = rs(i).Split("=") If cs.Length = 2 Then Dim lins As String =cs(0) Dim N,j As Integer n=0 j=0 For j=0 To 5 If lins.Contains(KJHMS(j)) Then N=N+1 End If Next If cs(1).Contains(N) Then kjqs.add(qs) jh.add("错误点 " & file & " <" & i+1 & ">" ) End If End If Next End If Next Else MessageBox.Show("号码格式错误,请检查") Exit For End If Else MessageBox.Show(fs(0) & "数据格式错误,请检查") Exit For End If Next For Each p As String In FileSys.GetDirectories(path) \'循环调用自身代码 Functions.Execute("多文件递归", p, ls) Next pxqs =kjqs.ToArray() pxjh=jh.ToArray() For k As Integer=0 To pxqs.length-1 MessageBox.Show( pxqs(k) & ":" & pxjh(k) ) Next
|
-- 作者:有点蓝 -- 发布时间:2024/3/30 14:38:00 -- 这个是递归函数呀,假设递归调用了这个函数10次,pxqs 最终也只能等于最后一次调用函数的结果。如果要把这10次调用的结果合并到一起,应该使用集合,不要使用数组。而且也没有必要使用全局变量,把集合变量作为参数传入函数即可,类似【Dim ls = args(1)】的用法 |
-- 作者:sloyy -- 发布时间:2024/3/30 17:07:00 -- 谢谢有点蓝大师的提醒,我改了再试试 |