Forum Discussion

rkarger's avatar
rkarger
Occasional Contributor
14 years ago

AddMethod method of Object object calls the new method when it's added

I am trying to use the AddMethod method to add a new method to an object that I want to call later and do NOT want to call when it's being added.  AddMethod calls the new method anyway.  This would be annoying but usable if the result from AddMethod was what the new function returned, but it isn't.  Is there a way to add a method to an object in a script without calling the method at the time of creation?

4 Replies

  • Hi Rebecca,



    I don't see the behavior you describe using the following script. AddMethod doesn't run the method being added; the method is only executed when called directly - just as expected.

    // Unit1



    function Main()

    {

      ODT.Classes.Clear();



      ODT.Classes.Declare("MyClass");

      var obj = ODT.Classes.New("MyClass");



      Log.Message("Before AddMethod")

      obj.AddMethod("Test", "Unit1.Test");

      Log.Message("After AddMethod");



      Log.Message("Now calling the newly added method:")

      obj.Test();

    }



    function Test()

    {

      Log.Message("Hello from Test!");

    }


    Could you please post here your test project so we can take a closer look at what's going on? If you can't post it on a public forum, you can send it privately to our Support Team using this form.
  • rkarger's avatar
    rkarger
    Occasional Contributor
    Not sure how to separate my code, sorry.



    My object structure was a class called Test which holds an array of checkpoint objects which each hold a trigger object.  The trigger object is different based on the type of input it gets so I need to add methods to the object based on what type it is.  When any Run"Type"Trigger method is called it logs ""Type" Trigger Running" and every time this code ran I would get a log saying something like:



    Adding Method

    Running Digital Trigger

    Calling Trigger's Method

    Running Digital Trigger



    I hoped after I discovered the problem that I could just get the returned results from the function that was called through AddMethod but it did not give me the output, instead I got what I expected which was the method object.  The problem with that is that I need to call the function only once and if it runs without giving me the output my test doesn't run properly.  I've since worked around it but I hope this helps you replicate and solve this issue.



    VBScript:



    Function RunTrigger

      If This.type = "click" Then

        Log.Message "Adding Method"

        Call This.AddMethod("RunClickTrigger", MainUnit.RunClickTrigger")

        Log.Message "Calling Trigger's Method"

        RunTrigger = This.RunClickTrigger

      ElseIf This.type = "drag" Then

        Log.Message "Adding Method"

        Call This.AddMethod("RunDragTrigger", MainUnit.RunDragTrigger")

        Log.Message "Calling Trigger's Method"

        RunTrigger = This.RunDragTrigger

      ElseIf This.type = "digital" Then

        Log.Message "Adding Method"

        Call This.AddMethod("RunDigitalTrigger", MainUnit.RunDigitalTrigger")

        Log.Message "Calling Trigger's Method"

        RunTrigger = This.RunDigitalTrigger

      ElseIf This.type = "analog" Then

        Log.Message "Adding Method"

        Call This.AddMethod("RunAnalogTrigger", MainUnit.RunAnalogTrigger")

        Log.Message "Calling Trigger's Method"

        RunTrigger = This.RunAnalogTrigger

      ElseIf This.type = "serial" Then

        Log.Message "Adding Method"

        Call This.AddMethod("RunSerialTrigger", MainUnit.RunSerialTrigger")

        Log.Message "Calling Trigger's Method"

        RunTrigger = This.RunSerialTrigger

      ElseIf This.type = "wait" Then

        Log.Message "Adding Method"

        Call This.AddMethod("RunWaitTrigger", MainUnit.RunWaitTrigger")

        Log.Message "Calling Trigger's Method"

        RunTrigger = This.RunWaitTrigger

      'If the type value is not one of the specified types, log it

      Else

        Log.Message "Unknown Trigger Type"

        Log.Message This.type  

      End If

    End Function
  • Hi Rebecca,



    The script you posted contains syntax errors on the AddMethod lines - an unmatched quote after MainUnit.Run'Type'Trigger:


    Valid syntax is:
    Call This.AddMethod("RunClickTrigger", "MainUnit.RunClickTrigger")

    Call This.AddMethod("RunClickTrigger", MainUnit.RunClickTrigger)
    but only the former statement is logically correct.

    The latter is incorrect and can actually cause the behavior you described. That is, MainUnit.RunClickTrigger without quotes is treated as a routine call rather than the routine name. Is this the case?
  • rkarger's avatar
    rkarger
    Occasional Contributor
    Hi,



    Yes, the code had a syntax error - I had already removed it from my code to work around the problem and had to rewrite it, and I missed that.



    What I had was AddMethod("Name", MainUnit.Name) so that seems to be where my problem was.  I didn't realize it was calling the method there.  Thank you for your help!