.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Exit Recursive function .net

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
matt.worland
428 Views, 2 Replies

Exit Recursive function .net

    ' Recursively grabs ObjectIDs of xrefs
    ' used by HandleToOID
    Private Shared Function ChildrenGet(i_root As GraphNode, i_Tx As Transaction, myHNDL As Handle, myOwnerHNDL As Handle) As ObjectId
        For o As Integer = 0 To i_root.NumOut - 1
            Dim child As XrefGraphNode = TryCast(i_root.Out(o), XrefGraphNode)
            If child.XrefStatus = XrefStatus.Resolved Then
                Dim bl As BlockTableRecord = TryCast(i_Tx.GetObject(child.BlockTableRecordId, OpenMode.ForRead), BlockTableRecord)
                If bl.Handle = myOwnerHNDL Then
                    'we found the parent object, now find myHandle 
                    Dim myXrefDB As Database = bl.GetXrefDatabase(False) ' check for example on what the number means
                    Using XTrans As Transaction = myXrefDB.TransactionManager.StartTransaction
                        Try
                            Dim myEntOID As ObjectId = myXrefDB.GetObjectId(False, myHNDL, 0)
                            MsgBox(myEntOID.ObjectClass.Name & vbCr & bl.Handle.ToString & vbCr & myEntOID.ToString)
                            Return myEntOID
                        Catch myex As System.Exception
                            MsgBox(myex.Message)
                        End Try
                    End Using
                    Exit For
                Else
                    ChildrenGet(child, i_Tx, myHNDL, myOwnerHNDL)
                End If
            End If
        Next
    End Function

 Im a bit stuck on the code above. 

Depending on how deep the xref nesting goes, it appears that .net will step back out of the code one level at a time.

I can hit this line with a valid ObjectId: Return myEntOID

  

But when I hit that point, it steps through to End Function, Like I would think it would.

 

Then goes back to: 

Else

ChildrenGet(child, i_Tx, myHNDL, myOwnerHNDL)

 

Is this normal .net behaviour, How do I exit the function with my value?

If my reply was helpful, please give a "Thumbs Up" or "Accept as Solution"
2 REPLIES 2
Message 2 of 3
Balaji_Ram
in reply to: matt.worland

Hi Matt,

 

I think this is a general behavior with any recursive function.

 

If you want the recursion to end, you may try using a boolean value that gets passed into the function. This will let the top level recursions to know that the value that you are interested in has been found and it no longer needs to invetigate further. If so, the function can simply return which willl make it go back one level up and this will repeat until your recursion ends.

 

Regards,

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

Message 3 of 3
matt.worland
in reply to: Balaji_Ram

Thank you, Im just figuring out what all of the .net functions do I guess. I assumed, the RETURN would return me and exit out of the function.

 

Thanks again for the explanation.

If my reply was helpful, please give a "Thumbs Up" or "Accept as Solution"

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost