Introduction to PowerBuilder

HomePrevious Lesson: Triggering an Event
Next Lesson: Cascaded Events and Functions

Triggering Events Dynamically

Sometimes, events/functions do not exist at compile time. Take the example of writing a generic function (as shown below) which saves DataWindows in a given window. Before saving each DataWindow, this function executes ue_presave event before and ue_postsave after saving the DataWindow. We expect the DataWindow in the passed window to have these events at run-time. In situations like these, we can use the DYNAMIC keyword, so that compiler doesn't check for the existence of the event or function and their arguments & return types.
//Window iWindow
datawindow l_dw
integer li_NoOfControls, li_Counter, li_RetStatus
li_NoOfControls = UpperBound(iWindow.Control[])
For li_Counter = 1 to li_NoOfControls
   Choose Case iWindow.Control[ li_Counter ].TypeOf() 
      Case DataWindow!
         l_dw = iWindow.Control[ li_Counter ]
         If l_dw.ModifiedCount() + &
              l_dw.DeletedCount() > 0 Then
            li_RetStatus = l_dw.Event DYNAMIC &
                         TRIGGER ue_PreSave() 
            If li_RetStatus <> 0 Then Return -100
            li_RetStatus = l_dw.Update(True)
            // Error Handling
            l_dw.Event DYNAMIC ue_PostSave() 
            If li_RetStatus <> 0 Then Return -100
         End If
      Case Else
         // Other code
   End Choose
Next
Return 0

Calling events dynamically is slower than calling static event/functions. That is because, a check is to be done at run-time for event/function existence. So, use DYNAMIC keyword only when necessary.

What happens if an event doesn't exist at run-time when using DYNAMIC keyword? In this matter it works silently. It won't generate any errors that abort the application, instead it returns -1 as the return value. So, make sure you check for the return values.
HomePrevious Lesson: Triggering an Event
Next Lesson: Cascaded Events and Functions