ArrayList ToArray Cast throwing exception

  • Thread starter Thread starter Mark Oueis
  • Start date Start date
M

Mark Oueis

ArrayList arrayList = new ArrayList();

foreach (Blabla bla in Blablas)
{
arrayList.Add(Convert.ToInt32(blabla.something));
}

so far this works great..
but now

int[] intArray = (int[])includedDevices.ToArray(typeof(int)));

throws System.InvalidCastException: At least one element in the source
array could not be cast down to the destination array type.

i've also tried
int[] intArray = (int[])includedDevices.ToArray(typeof(System.Int32)));

same thing

What's the issue here? I'm doing everything right

Mark
 
Mark Oueis said:
ArrayList arrayList = new ArrayList();

foreach (Blabla bla in Blablas)
{
arrayList.Add(Convert.ToInt32(blabla.something));
}

so far this works great..
but now

int[] intArray = (int[])includedDevices.ToArray(typeof(int)));

throws System.InvalidCastException: At least one element in the source
array could not be cast down to the destination array type.

i've also tried
int[] intArray = (int[])includedDevices.ToArray(typeof(System.Int32)));

same thing

What's the issue here? I'm doing everything right

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
I am sure it is just a typing error,
but you add the System.Int32 instances to 'arrayList'
and you are trying to cast them into System.Int32 [] from 'includedDevices'.
Are you mixing up two different instances?
 
Back
Top