5
Answers

How to Convert string[] to eg int32[]

bonehead

bonehead

11y
989
1

Hi,

Getting invalid cast exception, when casting from string to int32[]. What's the correct approach to casting below.

Thanks.

int[] ii = ReadField<int[],string>("Col5", "Game", "Who");

static T ReadField<T,U>(string searchCol, U searchFor, string colName)
{

Type u = typeof(T);

string[] foundValues = "4,9".Split(',');
   
Array a = Array.CreateInstance(u, foundValues.Lenght);

for (int i =0; i < foundValues.Lenght, i++)
  a.SetValue(Convert.ChangeType(foundValues[i], u), i); <== Incorrect cast string to int32[]

return ?? (T)Convert.ChangeType(a, u);

}
Answers (5)