Exception when doing CopyTo

  • Thread starter Thread starter emma_middlebrook
  • Start date Start date
E

emma_middlebrook

Hi

This code snippet fails with "Object cannot be stored in an array of
this type." whether I type the symbol 'items' as Size[] or just plain
Array or anything sensible.

Any ideas please?

Hashtable h = new Hashtable();

for(int i = 0; i < 10; i++)
{
h = i;
}

Size[] items = new Size[h.Count];
h.Keys.CopyTo( items, 0 );

Tx
 
This code snippet fails with "Object cannot be stored in an array of
this type." whether I type the symbol 'items' as Size[] or just plain
Array or anything sensible.

Any ideas please?

Hashtable h = new Hashtable();

for(int i = 0; i < 10; i++)
{
h = i;
}

Size[] items = new Size[h.Count];
h.Keys.CopyTo( items, 0 );


Well, the keys are all Int32s, not Sizes. If you declare items to be an
int[], it'll work fine. It would also work if you declared items to be
an object[].
 
This code snippet fails with "Object cannot be stored in an array of
this type." whether I type the symbol 'items' as Size[] or just plain
Array or anything sensible.

Any ideas please?

Hashtable h = new Hashtable();

for(int i = 0; i < 10; i++)
{
h = i;
}

Size[] items = new Size[h.Count];
h.Keys.CopyTo( items, 0 );


Well, the keys are all Int32s, not Sizes. If you declare items to be an
int[], it'll work fine. It would also work if you declared items to be
an object[].


Jon - you're right. I 'adapted' this snippet from some original code
but it has the same problem. What was I thinking of?!

Cheers

Emma
 
Back
Top