Microsoft KB Archive/306570

= How To Use JScript .NET to Traverse Through a Collection =

Article ID: 306570

Article Last Modified on 7/2/2004

-

APPLIES TO


 * Microsoft JScript .NET

-



This article was previously published under Q306570





This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.

No formal product support is available from Microsoft for this Beta product. For information about how to obtain support for a Beta release, see the documentation that is included with the Beta product files, or check the Web location from which you downloaded the release.

IN THIS TASK
SUMMARY
 * Create an Application That Uses the &quot;For...In&quot; Syntax to Traverse Through a Collection
 * Additional Information About the &quot;For...In&quot; Construction

REFERENCES



SUMMARY
This step-by-step article demonstrates how to use JScript .NET to traverse through a collection by using the &quot;for ... in&quot; syntax. This article uses a simple console application to illustrate this technique.

back to the top

Create an Application That Uses the &quot;For...In&quot; Syntax to Traverse Through a Collection
 From the Windows Start menu, click Run, and then type notepad to open Notepad.  Copy the following code, right-click it, and then click Copy. In Notepad, click Paste on the Edit menu to insert the code into the document: import System; import System.Collections;

var arlist : ArrayList;

arlist = new ArrayList; arlist.Add(&quot;red&quot;); arlist.Add(&quot;blue&quot;); arlist.Add(&quot;green&quot;); for (var k : String in arlist) {   Console.WriteLine(k); }                    On the File menu, click Save. In the Save As Type drop-down list box, click All Files. In the File Name text box, type Collection.js. Finally, click Save. Compile the file from the command line. For example, type the following command at a command prompt:

jsc.exe /out:collection.exe collection.js

 Run the application.</li></ol>

back to the top

Additional Information About the &quot;For...In&quot; Construction
You can use the &quot;for ... in&quot; construction to enumerate the following types of objects:
 * &quot;Expando&quot; properties of an object: Property names are enumerated. Expando properties are properties that can be added and removed dynamically at run time. To access expando properties, you must use the [&quot; &quot;] notation.
 * All arrays: Indices are enumerated.
 * Objects that implement the IEnumerable or IEnumerator interfaces: Whatever the enumerator returns is enumerated.

Note that normal properties cannot be enumerated.

back to the top

<div class="references_section">