// StringCollection.cpp : main project file.

#include "stdafx.h"
#using <System.dll>

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

// prototypes...
void PrintValues1(StringCollection^ myCol);
void PrintValues2(StringCollection^ myCol);
void PrintValues3(StringCollection^ myCol);

int main(array<System::String ^> ^args)
{
	Console::WriteLine("StringCollection Class Properties and Methods");
	Console::WriteLine();
	// Create and initializes a new StringCollection.
	StringCollection^ myCol = gcnew StringCollection;
	
	// Add a range of elements from an array to the end of the StringCollection.
	array<String^>^myArr = {"RED","orange","yellow","RED","green","blue","RED","indigo","violet","RED"};
	myCol->AddRange(myArr);

	// Display the contents of the collection using for each. This is the preferred method.
	Console::WriteLine("Displays the elements using for each:");
	PrintValues1(myCol);

	// Display the contents of the collection using the enumerator.
	Console::WriteLine("Displays the elements using the IEnumerator:");
	PrintValues2(myCol);
	
	// Display the contents of the collection using the Count and Item properties.
	Console::WriteLine( "Displays the elements using the Count and Item properties:" );
	PrintValues3(myCol);

	// Add one element to the end of the StringCollection and insert another at index 3.
	myCol->Add("* white");
	myCol->Insert(3, "* gray");
	Console::WriteLine("After adding \"* white\" to the end and inserting \"* gray\" at index 3:");
	PrintValues1( myCol );

	// Remove one element from the StringCollection.
	myCol->Remove("yellow");
	Console::WriteLine("After removing \"yellow\":");
	PrintValues1(myCol);

	// Remove all occurrences of a value from the StringCollection.
	int i = myCol->IndexOf("RED");
	while (i > -1)
		{
			myCol->RemoveAt(i);
			i = myCol->IndexOf("RED");
		}

	// Verify that all occurrences of "RED" are gone.
	if (myCol->Contains("RED"))
		Console::WriteLine("*** The collection still contains \"RED\".");

	Console::WriteLine("After removing all occurrences of \"RED\":");
	PrintValues1(myCol);

	// Copy the collection to a new array starting at index 0.
	array<String^>^myArr2 = gcnew array<String^>(myCol->Count);
	myCol->CopyTo(myArr2, 0);
	Console::WriteLine("The new array contains:");
	for (i = 0; i < myArr2->Length; i++)
		{
			Console::WriteLine("   [{0}] {1}", i, myArr2[ i ]);
		}
	Console::WriteLine();

	// Clears the entire collection.
	myCol->Clear();
	Console::WriteLine("After clearing the collection:");
	PrintValues1(myCol);

    return 0;
}

// Uses the for each statement which hides the complexity
// of the enumerator. The for each statement is the preferred
// way of enumerating the contents of a collection.
void PrintValues1(StringCollection^ myCol)
{
	for each (Object^ obj in myCol)
		Console::WriteLine("   {0}", obj);
	Console::WriteLine();
}

// Uses the enumerator. 
void PrintValues2(StringCollection^ myCol)
{
   StringEnumerator^ myEnumerator = myCol->GetEnumerator();
   while (myEnumerator->MoveNext())
      Console::WriteLine("   {0}", myEnumerator->Current);

   Console::WriteLine();
}

// Uses the Count and Item properties.
void PrintValues3(StringCollection^ myCol)
{
   for (int i = 0; i < myCol->Count; i++)
      Console::WriteLine("   {0}", myCol[ i ]);
   Console::WriteLine();
}


