Casting an arraylist of integers to a string array

E

Earl

I'm trying to do a cast and I'm not sure where I'm going wrong here. I load
up the ColorExtIntID column (int) into a list array (no problem there). For
binding to a 3rd party component, it has to then be cast to a string array.

private void PopulateColors()
{

DataView dvColors = new DataView(dtColors);

ArrayList ColorIDList = new ArrayList(dvColors.Count);

Int32 i;

for (i = 0; i <= dvColors.Count - 1; i++)

{

ColorIDList.Add(dvColors["ColorExtIntID"]);

}

Int32 x = fpSpread1.ActiveSheet.ActiveRow.Index;

// this is the invalid cast where I get an exception

string[] sColorIDList = (string[])ColorIDList.ToArray(typeof(Int32));

.....

}
 
A

allfyre

try changing the last line to:

string[] sColorIDList = (string[])ColorIDList.ToArray(typeof(string));
 
J

Jon Skeet [C# MVP]

Earl said:
I'm trying to do a cast and I'm not sure where I'm going wrong here. I load
up the ColorExtIntID column (int) into a list array (no problem there). For
binding to a 3rd party component, it has to then be cast to a string array.

It can't. A cast doesn't change the contents, it just provides the
compiler with more information about what is present.

You'll need to create a new string array of the same size, then convert
each int to a string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top