Click to See Complete Forum and Search --> : ArrayList


aijazasoomro
January 8th, 2009, 05:33 PM
Hi All,

I have tried to access the ArrayList Elements by following Code: ( This is Visual C++ .Net Code)

ArrayList^ myAL = gcnew ArrayList;

myAL->Insert( 0, "The" );
myAL->Insert( 1, "fox" );
myAL->Insert( 2, "jumps" );
myAL->Insert( 3, "over" );
myAL->Insert( 4, "the" );
myAL->Insert( 5, "dog" );
myAL->Insert(6,"cat");
.....

...
myAL->Insert(5000,"abc");

IEnumerator^ myEnum = myAL->GetEnumerator();



while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
Console::Write( " {0}", obj );
}




Is there any other way to Access Only a Single Element Without IEnumerator.
Because If I need only one element then What is the need to get the IEnumerator.

I am looking for searching a element based on particular index.


Any Help In this Regard will be Highly Appreciated.


Aijaz Soomro.

darwen
January 10th, 2009, 09:28 PM
You're having a really uphill struggle with this, so can I suggest you switch to using C# instead ? Its intellisense works much better and it (just about) produces the same code as C++/CLI.

In answer to your question, you can use an indexer e.g.


int index = 2;
String ^xx = safe_cast<String ^>(myAL[index]);


See the square brackets notation ?

Also see my response to your other post about using a generic List class instead of ArrayList.

Darwen.