Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Enumerator Object

Enables enumeration of items in a collection.

varName = new Enumerator([collection])

Arguments

varName
Required. The variable name to which the enumerator is assigned.
collection
Optional. Any object that implements the IEnumerable interface, such as an array or collection.

Remarks

Every collection is automatically enumerable in JScript .NET. Consequently, you do not need to use the Enumerator object to access members of a collection. You can access any member directly using the for...in statement. The Enumerator object is provided for backwards compatibility.

Collections differ from arrays in that the members of a collection are not directly accessible. Instead of using indexes, as you would with arrays, you can only move the current item pointer to the first or next element of a collection.

The Enumerator object, which provides a way to access any member of a collection, behaves in a manner similar to the For...Each statement in VBScript.

You can create a collection in JScript by defining a class that implements IEnumerable. Collections can also be created using another language (such as Visual Basic) or by an ActiveXObject object.

Example 1

The following code uses the Enumerator object to print the letters of the available drives and their names (if available):

// Declare variables.
var n, x;
var fso : ActiveXObject = new ActiveXObject("Scripting.FileSystemObject");
// Create Enumerator on Drives.
var e : Enumerator = new Enumerator(fso.Drives);
for (;!e.atEnd();e.moveNext()) {      // Loop over the drives collection.
   x = e.item();
   if (x.DriveType == 3)              // See if network drive.
      n = x.ShareName;                // Get share name
   else if (x.IsReady)                // See if drive is ready.
      n = x.VolumeName;               // Get volume name.
   else
      n = "[Drive not ready]";
   print(x.DriveLetter + " - " + n);
}

Depending on the system, the output will look like this:

A - [Drive not ready]
C - DRV1
D - BACKUP
E - [Drive not ready]

Example 2

The code in Example 1 can be rewritten to work without using the Enumerator object. Here, members of an enumeration are accessed directly.

// Declare variables.
var n, x;
var fso : ActiveXObject = new ActiveXObject("Scripting.FileSystemObject");
// The following three lines are not needed.
//    var e : Enumerator = new Enumerator(fso.Drives);
//    for (;!e.atEnd();e.moveNext()) {
//       x = e.item();
// Access the members of the enumeration directly.
for (x in fso.Drives) {               // Loop over the drives collection.
   if (x.DriveType == 3)              // See if network drive.
      n = x.ShareName;                // Get share name
   else if (x.IsReady)                // See if drive is ready.
      n = x.VolumeName;               // Get volume name.
   else
      n = "[Drive not ready]";
   print(x.DriveLetter + " - " + n);
}

Properties

The Enumerator object has no properties.

Methods

Enumerator Object Methods

Requirements

Version 3

See Also

new Operator | for...in Statement

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.