Use transaction from a sub function

By Adam Nagy

If you start a transaction inside your main function, then you do not need to create another one (a sub transaction) inside your sub function, you do not even need to pass the transaction to the sub function, you can simply use ObjectId.GetObject() instead, which will automatically use the outer transaction you started.

Public Sub MoveBlock( _
  ByRef blockRefId As ObjectId, _
    ByVal newLocation As Point3d)
 
  Dim blockRef As BlockReference = _
    blockRefId.GetObject(OpenMode.ForWrite)
 
  blockRef.Position = newLocation
 
End Sub
 
 _
Public Sub MoveBlockCommand()
  Dim ed As Editor = _
    Application.DocumentManager.MdiActiveDocument.Editor
 
  Dim perEntity As PromptEntityResult = _
    ed.GetEntity("Select block to move: " + vbCrLf)
  If perEntity.Status  PromptStatus.OK Then Return
 
  Dim pprPoint As PromptPointResult = _
    ed.GetPoint("Pick the new position for the block: " + vbCrLf)
  If pprPoint.Status  PromptStatus.OK Then Return
 
  Dim db As Database = HostApplicationServices.WorkingDatabase
 
  Using tr As Transaction = db.TransactionManager.StartTransaction
    MoveBlock(perEntity.ObjectId, pprPoint.Value)
 
    tr.Commit()
  End Using
End Sub

Comments

One response to “Use transaction from a sub function”

  1. Eyal Cohen Avatar
    Eyal Cohen

    Adam thanks.
    I hope this message finds you well (long time since your post).
    Following your insight
    (i.e. no necessity of both outer+inner trans objects):
    Suppose we have “outer” main procedures (e.g. commands subs),
    which call a variety of “inner” helper methods.
    Do you think there is a preferred practice:
    a. Start an “outer” transaction inside the main procedure? or
    b. create “inner” transactions inside each of the helper methods?
    Thanks in advance, Eyal.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading