Forum Discussion

S_Leonardo's avatar
S_Leonardo
Occasional Contributor
9 years ago

Trigger an event whenever a script is called

Hi,

i got some difficulties trying doing this. Whenever a method inside a script is called, i need to run a method that belongs to the same script. 

 

It's something like this:

 

Sub DoSomething1

  *Code

End Sub

 

Sub DoSomething2

  *Code

End Sub

 

Sub Preparation

  *Code

End Sub

 

Whenever i call DoSomething1 or DoSomething2 i want to call Preparation before. I do not like the idea of having to call Preparation in every method i create. Is there a way to use something like a event to trigger whenever a method inside this script is called?

 

Sorry if i'm not being clear enough.

Thanks!

6 Replies

  • djadhav's avatar
    djadhav
    Regular Contributor

    Whichever method calls DoSomething1 or DoSomething2, have a call to Preparation before you call any of them. That way you have only 1 call to preparation.

     

    Sub CallerFunction

    Call Preparation

    Call anyDoSomething

    End Sub

    • S_Leonardo's avatar
      S_Leonardo
      Occasional Contributor

      Thanks for the reply but this is exactly what i do not want to do. In this way i have to call Preparation before calling any other method.

       

      Like this:

       

      Sub CallerFunction

       Call Preparation

       Call DoSomething1

       Call Preparation

       Call DoSomething2

       Call Preparation

       Call DoSomethingX

      End Sub

       

      In this specific script i have 38 methods and i got other scripts that will need something like this in the future. So i really wanted something like events.

       

      Thanks!

  • djadhav's avatar
    djadhav
    Regular Contributor

    @harush wrote:

    Is there a way to use something like a event to trigger whenever a method inside this script is called?

     


    The best event or trigger is that a method has been called and rather than writing additonal event code, I would just call Preparation at the beginning of all DoSomethings.

     

    Sub DoSomething

        Call Preparation

        -

        -

    End Sub

  • NisHera's avatar
    NisHera
    Valued Contributor

    Dose the Preperation changes slighly depending on DoSomething1 or  DoSomething2 ?

     

    if not, having event would overkill or make it more complex. Just calling Preperation would be enough

     

    if do, I suggest OOP would be best option. but unfortunatly VB is not fully grown OOP language.

     

    anyway, allways good to follow KISS method. (Keep It Simple and Stupid)

  • m_essaid's avatar
    m_essaid
    Valued Contributor

    hi,

     

    I like the "OnlogMessage" event...

     

    I make a : Log.Message('MyTriggerSentence');

    And previously I added on the "OnLogMessage" event the following code :

     

    procedure GeneralEvents_OnLogMessage(Sender; LogParams);
    var
      props, prop;
    begin
      props:= aqObject.GetProperties(LogParams);
      while (props.HasNext) do
      begin
        prop:= props.Next;
        if(prop.Name = 'MessageText') then
        begin
          if(aqConvert.VarToStr(prop.Value) = 'MyTriggerSentence') then

            ... your code here...

    • m_essaid's avatar
      m_essaid
      Valued Contributor

      you can use as many trigger sentences as you want, you just need to organize that in a clean way