递归函数不返回

时间:2011-11-28 15:57:26

标签: vb.net recursion return

我希望有人可以在这里帮助我,我有一个递归函数,它没有返回true或false,正如我所希望的那样。该函数为其成员循环通过Active Directory组,然后在遇到成员资格中的任何组时调用自身以获取其成员。我试图根据是否遇到任何错误但根本没有任何运气来返回true或false。它似乎只是挂起并且永远不会返回到启动递归函数的主调用子。以下是我正在使用的代码:

    Private Sub StartAnalysis(ByVal grp As String, ByVal grpdn As String, ByVal reqid As String)

    Dim searchedGroups As New Hashtable

    'prior work before calling sub

    searchedGroups.Add(grp, 1)
    Dim iserror As Boolean = GetGroupMembers(grpdn, searchedGroups, reqid)

    If iserror = False Then
        'do stuff
    Else
        'do stuff
    End If

    'cleanup

End Sub


    Public Function GetGroupMembers(ByVal groupSearch As String, ByVal searchedGroups As Hashtable, ByVal requestID As String) As Boolean

    Dim iserror As Boolean = False

    Try
        Dim lastQuery As Boolean = False
        Dim endLoop As Boolean = False
        Dim rangeStep As Integer = 999
        Dim rangeLow As Integer = 0
        Dim rangeHigh As Integer = rangeLow + rangeStep

        Do
            Dim range As String = "member"
            If lastQuery = False Then
                range = String.Format("member;range={0}-{1}", rangeLow, rangeHigh)
            Else
                range = String.Format("member;range={0}-*", rangeLow)
                endLoop = True
            End If

            Dim group As SearchResult = QueryObject(groupSearch, range)
            Dim groupCN As String = group.Properties("cn")(0).ToString
            If group.Properties.Contains(range) Then
                For Each member As Object In group.Properties(range)
                    Dim user As SearchResult = QueryObject(member.ToString, "member")
                    Dim userCN = user.Properties("cn")(0).ToString
                    If Not user.Properties.Contains("member") Then
                        Dim userMail = String.Empty
                        If user.Properties.Contains("mail") Then
                            userMail = user.Properties("mail")(0).ToString
                        End If

                        userCN = userCN.Replace("'", "''")
                        Dim qry As String = _
                            "INSERT INTO group_analysis_details (request_id, member_name, member_email, member_group) " & _
                            "values ('" & requestID & "', '" & userCN & "', '" & userMail & "', '" & groupCN & "')"

                        Dim sqlConn As SqlConnection = New SqlConnection(cs)
                        Dim sqlCmd As SqlCommand = New SqlCommand(qry, sqlConn)
                        sqlConn.Open()
                        sqlCmd.ExecuteNonQuery()
                        sqlConn.Close()
                        sqlCmd.Dispose()
                        sqlConn.Dispose()
                    Else
                        If Not searchedGroups.ContainsKey(userCN) Then
                            searchedGroups.Add(userCN, 1)
                            iserror = GetGroupMembers(user.Properties("distinguishedname")(0).ToString, searchedGroups, requestID)
                            If iserror = True Then Return iserror
                        Else
                            searchedGroups(userCN) += 1
                        End If
                    End If
                Next
            Else
                lastQuery = True
            End If

            If lastQuery = False Then
                rangeLow = rangeHigh + 1
                rangeHigh = rangeLow + rangeStep
            End If

        Loop While endLoop = False
        Return iserror
    Catch ex As Exception
        myEvents.WriteEntry("Error while analyzing the following group: " & groupSearch & vbCrLf & vbCrLf & _
            "Details of the error are as follows: " & ex.Message, EventLogEntryType.Error)
        Return True
    End Try

End Function

希望有人可以指出我可能在哪里犯错误就是这个。

谢谢, 罗恩

1 个答案:

答案 0 :(得分:0)

通常,如果您使用'Do ... Loop While'并在循环内手动设置退出条件,则很容易陷入无限循环,导致程序挂起。

看起来你并没有在所有情况下设置endloop = True。尝试将其更改为Exit Do,并为您拥有的各种条件添加一个。为了使它恰到好处,需要进行一些反复试验。

另外,为了让您的生活更轻松,请将数据库插入代码转换为单独的函数,并在需要时调用它。