Click to See Complete Forum and Search --> : ArrayList: Type casting error with ArrayList Objects


aijazasoomro
January 9th, 2009, 11:56 AM
Dear All,


I created one ArrayList

ArrayList^ myAL = gcnew ArrayList;


myAL->Add('#');

myAL->Add("4");
myAL->Add(3);
myAL->Add(1.5);



Finally I want to assign each type of element to String type. But I am getting Type Casting Problem, I have applied many tricks , but most of them are failed

I need this



String ^one=myAL[0];
String ^two=myAL[1];
String ^three=myAL[2];

String ^four=myAL[3];


Obviously I must need explicit casting, I have applied explicit casting in many ways Like


String ^ one=myAL[0]->ToString()

But unfortunately , instead of “#”, “35” are copied into the string object.

For others I have also got surprising results.


I only need to convert all the contents converted into string object.


Please Suggest me how can I do this , better if you write a line of casting for me.



Thanks


Regards

Aijaz Soomro.

darwen
January 10th, 2009, 09:25 PM
You should be using the generic list class instead of ArrayList for just about everything now : ArrayList has been included in .NET 2.0 for backwards compatibility with .NET 1.1.

e.g.


using namespace System::Collections::Generic;
List<System::String ^> ^list = gcnew List<System::String ^>;

list->Add(wchar_t('#').ToString());
list->Add("4");
list->Add(int(3).ToString());
list->Add(int(1.5).ToString());


Darwen.