Forum Discussion

fayrehouse's avatar
fayrehouse
Frequent Contributor
12 years ago

foreach loops in JScript / C#Script...

Hi All,



I have the following bit of C# sample code "lifted" from the net...sds



foreach (Item myItem in findResults.Items)



"findResults" is of type Microsoft.Exchange.WebServices.Data.FindItemsResults while 



"findResults.Items" 



is of type System.Collections.ObjectModel.Collection



The problem is - I've tried both JScript and C#Script, I *cannot* specify the type of object "myItem" is in the foreach loop. And I've tried NOT specifying the type:



"foreach (item myitem in findResults.Items)"  <<< syntax error

"foreach (myitem in findResults.Items)" <<<< Object expected error at run time.



Can someone help as to how to write this foreach loop in C#Script / Jscript?



Thanks







  • Hi, Steve, JScript has no concept of a foreach loop such as that prescribed in C#, I would use the approach of an old school for loop




    for( var i = 0; i < findResults.Items.Count; i++ ) {


      myItem = findResults.Items[ i ];


      // Proces myItem...


    }



    The closest approximation to foreach is the for..in loop but this iterates over the properties of an object so can produce unexpected results and needs to be used carefully and with the hasOwnProperty() method as well as other safe guards
  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Apologies, the forum has decided to render the collection indexing as an italic, hopefully this will work...




    for( var j = 0; j < findResults.Items.Count; j++ ) {


      myItem = findResults.Items[ j ];


      // Proces myItem...


    }

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Actually, the square bracket notation I showed probably will not work, this does work though and may be of use




    var sa = dotNET.System_Collections.ArrayList.zctor();


    sa.Add( "a" );


    sa.Add( "b" );


    sa.Add( "c" );


    var s;


    for( var i = 0; i < sa.Count; i++ ) {


      s = sa.Item( i );


      Log.Message( s );


    }




    The following also works




    var l = sa.GetEnumerator();


    while( l.MoveNext() ) {


      s = l.Current;


      Log.Message( s );


    }


    I could not get for...in to work in any form, I kept gettting the following error




    ---------------------------


    TestComplete


    ---------------------------


    Microsoft JScript runtime error.


     


    Object doesn't support this action


     


    Error location:


    Unit: "ARC\Framework\Script\Unit2"


    Line: 292 Column: 1.


    ---------------------------


    OK   


    ---------------------------



    Hope this helps


     

  • simon_glet's avatar
    simon_glet
    Regular Contributor
    Hi Steve,



    You Wrote: "foreach (item myitem in findResults.Items)" <<< syntax error

    That is because you must declare a var type.



    You wrote: "foreach (myitem in findResults.Items)" <<<< Object expected error at run time.

    myItem was auto-declared as a var type and findResults is undefined/null.



    It seems that you might want to checkout the CLR bridge to add a reference to the dlls that contain the two classes you mentionned. Documentation is here



    Sincerely

     

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Hi, Steve, JScript has no concept of a foreach loop such as that prescribed in C#, I would use the approach of an old school for loop




    for( var i = 0; i < findResults.Items.Count; i++ ) {


      myItem = findResults.Items[ i ];


      // Proces myItem...


    }



    The closest approximation to foreach is the for..in loop but this iterates over the properties of an object so can produce unexpected results and needs to be used carefully and with the hasOwnProperty() method as well as other safe guards
  • fayrehouse's avatar
    fayrehouse
    Frequent Contributor
    Thanks all - indeed, I also failed on the for..in front... 



    Good old fashioned for (x = 0; x < y; x++) was the way that ultimately got there... 



    Arguably not the "cleanest" solution, but does what it says on the tin :)





  • You may find that some collection objects have their own iteration functions



    e.g. 




        var iterator = app.window.blah.itemcollection.iterator();


        while (iterator.hasNext()) {


            Log.Message(iterator.next().nodeName);


        }



    and so on. Check in the Object Browser if the object has these sorts of methods.