How To Use JScript .NET to Traverse Through a Collection (306570)
The information in this article applies to:
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 TASKSUMMARY
This step-by-step article demonstrates how to use JScript .NET to traverse through a collection by using the "for ... in" syntax. This article uses a simple console application to illustrate this technique.
back to the top
Create an Application That Uses the "For...In" 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("red");
arlist.Add("blue");
arlist.Add("green");
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.
back to the top
Additional Information About the "For...In" Construction
You can use the "for ... in" construction to enumerate the following types of objects:
- "Expando" 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 [" "] 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
REFERENCES
For more information about the "for...in" statement in JScript .NET, see the following Visual Studio .NET Online Help file:
For more information about the expando properties in JScript .NET, see the following Visual Studio .NET Online Help file:
For more information about the IEnumerable interface, see the following Visual Studio .NET Online Help file:
NOTE: You must have Microsoft Visual Studio .NET installed to access the preceding files.
back to the top
Modification Type: | Minor | Last Reviewed: | 7/2/2004 |
---|
Keywords: | kbhowto kbHOWTOmaster kbScript KB306570 kbAudDeveloper |
---|
|