Amit
asked on
How to implement a recursive code
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.P arent.Info
oReport.Info.Parent.Info.P arent.Info .Parent.In fo
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
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.P
oReport.Info.Parent.Info.P
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Indeed. I guess my brain is starting to fry from staring at code all day ;)
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.Par ent = New clsReport
Debug.Print X.Hidden
X.Parent.Parent.Parent.Hid den = True
Debug.Print X.Hidden
End Sub
Kevin
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.Par
Debug.Print X.Hidden
X.Parent.Parent.Parent.Hid
Debug.Print X.Hidden
End Sub
Kevin
ASKER
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
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
ASKER
Hi idle mind,
How would you implement it using a while loop ?
thanks
-anshu
How would you implement it using a while loop ?
thanks
-anshu
ASKER
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(objRepor t.Info.Par ent)
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
-------------------------- -
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(objRepor
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
--------------------------
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Too much .NET for me I think!
Good call zorvek :D
Good call zorvek :D