次の方法で共有


StringEnumerator.Reset メソッド

列挙子を初期位置、つまりコレクションの最初の要素の前に設定します。

Public Sub Reset()
[C#]
public void Reset();
[C++]
public: void Reset();
[JScript]
public function Reset();

例外

例外の種類 条件
InvalidOperationException 列挙子が作成された後に、コレクションが変更されました。

解説

Reset は、列挙子をコレクションの先頭、つまり最初の要素の前に移動します。 Reset を実行した後に、コレクションの最初の要素に列挙子を進めるには、 Current の値を読み取る前に MoveNext を呼び出す必要があります。

使用例

[Visual Basic, C#, C++] StringEnumerator のプロパティとメソッドのいくつかの例を次に示します。

 
Imports System
Imports System.Collections.Specialized

Public Class SamplesStringEnumerator

   Public Shared Sub Main()

      ' Creates and initializes a StringCollection.
      Dim myCol As New StringCollection()
      Dim myArr() As [String] = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"}
      myCol.AddRange(myArr)

      ' Enumerates the elements in the StringCollection.
      Dim myEnumerator As StringEnumerator = myCol.GetEnumerator()
      While myEnumerator.MoveNext()
         Console.WriteLine("{0}", myEnumerator.Current)
      End While
      Console.WriteLine()

      ' Resets the enumerator and displays the first element again.
      myEnumerator.Reset()
      If myEnumerator.MoveNext() Then
         Console.WriteLine("The first element is {0}.", myEnumerator.Current)
      End If 

   End Sub 'Main

End Class 'SamplesStringEnumerator 


'This code produces the following output.
'
'red
'orange
'yellow
'green
'blue
'indigo
'violet
'
'The first element is red.


[C#] 
using System;
using System.Collections.Specialized;

public class SamplesStringEnumerator  {

   public static void Main()  {

      // Creates and initializes a StringCollection.
      StringCollection myCol = new StringCollection();
      String[] myArr = new String[] { "red", "orange", "yellow", "green", "blue", "indigo", "violet" };
      myCol.AddRange( myArr );

      // Enumerates the elements in the StringCollection.
      StringEnumerator myEnumerator = myCol.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "{0}", myEnumerator.Current );
      Console.WriteLine();

      // Resets the enumerator and displays the first element again.
      myEnumerator.Reset();
      if ( myEnumerator.MoveNext() )
         Console.WriteLine( "The first element is {0}.", myEnumerator.Current );

   }

}

/*
This code produces the following output.

red
orange
yellow
green
blue
indigo
violet

The first element is red.

*/

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Collections::Specialized;

int main() {

   // Creates and initializes a StringCollection.
   StringCollection* myCol = new StringCollection();
   String* myArr[] = { S"red", S"orange", S"yellow", S"green", S"blue", S"indigo", S"violet" };
   myCol->AddRange(myArr);

   // Enumerates the elements in the StringCollection.
   StringEnumerator* myEnumerator = myCol->GetEnumerator();
   while (myEnumerator->MoveNext())
      Console::WriteLine(S"{0}", myEnumerator->Current);
   Console::WriteLine();

   // Resets the enumerator and displays the first element again.
   myEnumerator->Reset();
   if (myEnumerator->MoveNext())
      Console::WriteLine(S"The first element is {0}.", myEnumerator->Current);

}


/*
This code produces the following output.

red
orange
yellow
green
blue
indigo
violet

The first element is red.

*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

StringEnumerator クラス | StringEnumerator メンバ | System.Collections.Specialized 名前空間 | MoveNext | Current | IEnumerator.Reset