fayrehouse
12 years agoFrequent Contributor
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
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