Solved

How to implement a recursive code

Posted on 2010-09-20
10
304 Views
Last Modified: 2012-06-27
Hi,

I have the follwing object say  

oReport

and each object has a parent which I can find from the info property like following

oReport.Info.Parent

now think like a folder structure in windows you store a word file in a folder which can reside into some other folder

so the word file is oReport and then the folder in which it is kept is

oReport.Info.Parent

now my goal is to traverse all the parents of an object like this untill the parent becomes null

oReport.Info.Parent
oReport.Info.Parent.Info.Parent.Info
oReport.Info.Parent.Info.Parent.Info.Parent.Info

I need to this for the following reason of the business logic. A report can be considered hidden if the following things are true

oReport.Info.Hidden=true or any of its parent is hidden so I need to loop through all the parents of the report recursivley untill the parent is null and find out if the report is hidden or one of its parents is hidden.

Hope I am clear enough
0
Comment
Question by:anshuma
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
Comment Utility
I can't see why you would want to keep searching once you found any hidden parent. I coded this according to that assumption.
Function HasHiddenAncestor(ByRef obj As Variant) As Boolean
    Dim result As Boolean

    If obj Is Nothing Then
        result = False
    Else If obj.Info.Hidden Then
        result = True
    Else
        result = HasHiddenAncestor(obj.Info.Parent)
    End If

    Return result
End Function

Open in new window

0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
Comment Utility
A recursive solution isn't actually necessary either...a simple while loop would do!  =)
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Indeed. I guess my brain is starting to fry from staring at code all day  ;)
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
Comment Utility
Recursive code is not necessary but is a great example of how to use objects. Here is how I would implement a recursive solution using classes. Note that I did not implement an Info class, just the Report class.

The Report class code:

Option Explicit

Public Parent As clsReport

Private mHidden As Boolean

Public Property Get Hidden() As Boolean
    If mHidden Then Hidden = True
    If Not Parent Is Nothing Then
        If Parent.Hidden Then Hidden = True
    End If
End Property

Public Property Let Hidden(ByVal NewValue As Boolean)
    mHidden = NewValue
End Property


A test routine:

Public Sub Test()

    Dim X As clsReport
   
    Set X = New clsReport
    Set X.Parent = New clsReport
    Set X.Parent.Parent = New clsReport
    Set X.Parent.Parent.Parent = New clsReport
    Set X.Parent.Parent.Parent.Parent = New clsReport
   
    Debug.Print X.Hidden
   
    X.Parent.Parent.Parent.Hidden = True

    Debug.Print X.Hidden

End Sub

Kevin
0
 

Author Comment

by:anshuma
Comment Utility
Hi Kaufmed,

You are correct, as soon as it finds that one of the parent is hidden then the object should be classified as hidden

So you can implement it like this also.

Stop processing as soon as a hidden parent is found

thanks
-anshu
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:anshuma
Comment Utility
Hi idle mind,

How would you implement it using a while loop ?

thanks
-anshu
0
 

Author Comment

by:anshuma
Comment Utility
I am getting a compile error when I implement the recursive code

Function HasHiddenAncestor(ByRef objReport As IDSSReportDefinition) As Boolean
    Dim result As Boolean

    If objReport Is Nothing Then
        result = False
    Else If objReport.Info.Hidden then
        result = True
    Else
        result = HasHiddenAncestor(objReport.Info.Parent)
    End If

    Return result
End Function


the error is following and it happens at this line of the code

Else If objReport.Info.Hidden then


---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Must be first statement on the line
---------------------------
OK   Help  
---------------------------
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Something like:

        oReport.Info.Hidden = False ' Assume it's not hidden until proven otherwise
        Dim curReport As Report = oReport.Info.Parent
        While Not IsNothing(curReport)
            If curReport.Info.Hidden Then
                oReport.Info.Hidden = True ' Found one!  Set the flag on the original instance and exit the loop...
                Exit While
            End If
            curReport = curReport.Info.Parent
        End While
0
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 150 total points
Comment Utility
The Return statement is not good.

Function HasHiddenAncestor(ByRef objReport As IDSSReportDefinition) As Boolean
    If objReport Is Nothing Then
        HasHiddenAncestor = False
    Else If objReport.Info.Hidden then
        HasHiddenAncestor = True
    Else
        HasHiddenAncestor = HasHiddenAncestor(objReport.Info.Parent)
    End If
End Function

Kevin
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Too much .NET for me I think!

Good call zorvek  :D
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now