Monday, August 4, 2008

Creating a StringCollection Array in C#.

This explanation was tried in C# 2.0. Basically we all know that StringCollection class allows you to store multiple strings. But unfortunately it doesn't have the possibility of using it as an Array type. But interestingly it allows you to define something like:
StringCollection[] coll = new StringCollection[2];
You get no compile errors,but you cannot access it.It will give a Runtime error, which still I don't know why. I was in need of having a set of related StringCollections stored in an array. So after some discussion and analysis with a friend, we both came up with a solution to achieve what I wanted. I was able to create, and access my StringCollection Array like this:

Initially import the the following mnaespaces:
using System.Collections.Generic;
using System.Collections.Specialized;

Then some where in a button click event (or just anywhere you prefer):

//Create two string arrays

String[] nameArr = new String[] {"Mafaz","David" };
String[] countryArr = new String[] {"Srilanka","India" };

//The primary StringCollections i'm storing
//the array values to

StringCollection nameColl = new StringCollection();
StringCollection countryColl = new StringCollection();

//Adding the arrays to the relevant StringCollections


//declare an ArrayList with the number of StringCollections
//which we want to atore as Arrays
//Basically this will serve as a StringCollection Array.
//In this case we have two string collections

ArrayList al = new ArrayList(2);

//Add the StringCollections as Elements


//Two view the elements again

StringCollection coll2 = new StringCollection();

//You have to cast it to StringCollection type from the

coll2 =(StringCollection) al[0];
Console.WriteLine("Names :"+ coll2[0] + coll2[1]);

coll2 =(StringCollection) al[1];
Console.WriteLine("Countries :"+ coll2[0] + coll2[1]);

You can use a loop also to iterate through many StringCollection array elements. So that's it, That's how you create a StringCollection Array... :-)


Vinesh K Velaga said...


StringCollection has a method CopyTo.

string[] sArray = new string[scSort.Count];

scSort.CopyTo(sArray, 0);

is this what you are trying to get?


smcube said...

Hi Vinesh,
First of all thanks for the comments. Well actually no. The method you have told is to copy elements of a StringCollection to a String array.
But my need was to create a set of StringCollections in an array and get them back as output. to further illustrate my example:
I have two StringCollections, one which contains a set of names, and other one contains a set of countries. I wanted to organize both collections into one object and return that object in the function (rather than trying to read the values of each StringCollection Seperately).
Here in the code I can add the StringCollections to a single ArrayList and return this ArrayList in the function.
So given the fact that I know the order of StringCollections organized in the ArrayList, I would first display the Names, and then the Countries... :-)

Hans said...

Hi! Your first example probably doesn't work because you initialize only the array, not the two elements in the array.

You do
StringCollection[] coll = new StringCollection[2];

But in order to use this, you have to instantiate both the array elements explicitly:
coll[0] = new StringCollection();
coll[1] = new StringCollection();


smcube said...

Yep Got the point..Thanks...! :)

Rahul said...


First of all. Thanks very much for your useful post.

I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.

Please let me introduce you some info related to this post and I hope that it is useful for .Net community.

There is a good C# resource site, Have alook


smcube said...

Hello Rahul,

I am glad that you were impressed,and thank you for sharing some links from your personal website. I am sure it will be helpful to many (including me).