Forum Discussion

steve_hall's avatar
steve_hall
Contributor
13 years ago

Function Expected Error.... but only after a few calls!

Hi guys,



I have a function as pasted below. I have an array of objects that I pass in turn in calls to this function in a loop. The first few calls are absolutely fine, but then I get an error in the array loop, on the line that calls this function, saying "Function Expected" - well, it IS a function I'm calling, and the loop has already called it several times!



Can anyone suggest what I'm missing?!



Function:




function FindObjectCaption(TargetObject)


{


//  FindObjectCaption = null;


  if (aqString.Find(TargetObject.FullName, "d_browse", 0, false) > 0)


  {   


    switch (TargetObject.ObjectType)


    {


      case "Edit":


      case "Client":


        // insert caption finder here for browsers


      break;


      case "Button":


        FindObjectCaption = TargetObject.Caption


      break;


    }                


  }


  else


  {


    switch (TargetObject.ObjectType)


    {


      case "Edit":


      case "Client":


        if (aqString.SubString(TargetObject.Caption, aqString.GetLength(TargetObject.Caption) - 2, 2) == "_t")


        { 


          FindObjectCaption = TargetObject.Child(0).Caption + " Label";


        }


        else


        {


          var PropArray = Array ("Caption");


          var ValArray = Array (TargetObject.Caption + "_t");


          var LabelChildren = TargetObject.Parent.FindAllChildren(PropArray, ValArray, 1);


          LabelChildren = (new VBArray(LabelChildren)).toArray(); 


          if (LabelChildren.length > 0)


          {


            FindObjectCaption = LabelChildren[0].Caption;


          }


        }


        break;


      case "Button":


        FindObjectCaption = TargetObject.Caption


        break;


    }                


  }       


}

5 Replies

  • Fixed! Once again, I was confusing my VBscript and Jscript skills :)



    rather than "FindObjectCaption = ....." 



    I needed "Tempvarname = ....."

                     "return Tempvarname"





    :)
  • AlexKaras's avatar
    AlexKaras
    Icon for Champion Level 2 rankChampion Level 2
    Hi Leandro,



    The case was that in VBScript, in order to return value from a function, one must assign the returned value to the 'variable' which corresponds to the name of the function. E.g.

    Function FindObjectCaption(TargetObject)

      Dim i

      Dim obj



      i = 2

      Set obj = CreateObject("WScript.Shell")

    ...

      FindObjectCaption = i

      ' use

      ' Set FindObjectCaption = obj

      ' if the function returns an object

    End Function



    In JScript (C#Script and C++Script) 'return' keyword is used to return the value from the function:

    function FindObjectCaption(TargetObject)

    {

      var i;

      var obj;



      i = 2;

      obj = new Object("WScript.Shell");

    ...

      return i;

      ' use

      ' return = obj;

      ' if the function returns an object

    }

  • Do you have a variable the same name as your function?

    If so this may be the issue. A couple of months ago I also did this and nothing was working correctly. I changed the name of the function and everything worked fine. 
  • Sadly not the solution - I just searched the entire project code for "TargetObject" and "FindObjectCaption" - both only found as above... :|





  • leandropoblet's avatar
    leandropoblet
    Frequent Contributor
    Hi Steve,



    So I have the same problem, but I'm currently using C# Script language.

    I don't really understand the solution to this problem. If you can clarify it a bit more I'd apreciate it :)



    I'm also calling a 'find' function that I just moved to a separate file since I'll be using it quite a lot.