This is a very basic or rather simple question.
In my code, I’m using foreach value to put values in a string variable. For each iteration I want to store(add one after another) that variable value in an arraylist. Then retrieve arraylist values by its index.
Code I’m trying:
foreach(string abc in namestring)
{
string values = abc;
ArrayList list = new ArrayList();
list.Add(values);
}
For Example:
If ‘namestring’ contains values as tom, dik, harry. Then ‘list’ should
contain those values as list(0) = tom, list(1) = dik, list(2) =
harry.
Problem is with storing values in arraylist
You have to declare your
ArrayListoutside the loop:Also if you are on .NET 2.0 and above you should use the strongly typed
List<string>instead: