.NET

Reply
Active Contributor
matt.worland
Posts: 34
Registered: ‎11-13-2006
Message 1 of 3 (182 Views)
Accepted Solution

Exit Recursive function .net

182 Views, 2 Replies
05-29-2014 10:27 AM
    ' 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?

matt worland
ADN Support Specialist
Balaji_Ram
Posts: 693
Registered: ‎03-21-2011
Message 2 of 3 (121 Views)

Re: Exit Recursive function .net

06-03-2014 03:01 AM 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

Active Contributor
matt.worland
Posts: 34
Registered: ‎11-13-2006
Message 3 of 3 (83 Views)

Re: Exit Recursive function .net

06-09-2014 01:28 PM 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.

matt worland
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.