Runtime error on casting Array to string Enumerator

272 Views Asked by At

This is a follow up question to Design issues and implementing Enumerable.AsEnumerable<FarPoint.Win.Spread.Row>

I have casted my farpoint row to a 2 d object array like this:

object[,] nthRow = fpSpread2.ActiveSheet.GetArray(e.Row, e.Column, 1, FarPointSpread1.ActiveSheet.ColumnCount);

I try to cast this to string so that i can apply Linq but i get the following error at runtime:

IEnumerator<string> narry = (IEnumerator<string>)nthRow.GetEnumerator();

Unable to cast object of type 'ArrayEnumerator' to type 'System.Collections.Generic.IEnumerator`1[System.String]'.

How can i resolve this problem?

1

There are 1 best solutions below

2
On BEST ANSWER

Even for multidimensional arrays you can call Cast<T> on it.

using System.Linq;
...
nthRow.Cast<string>().GetEnumerator()    // returns IEnumerator<string>

But each element in the array should actually be type string. If you want to do formatting like ToString(), you can first cast them into object and then call Select<T>.