Problems with ArrayList::ToArray

  • Thread starter Thread starter Dirk
  • Start date Start date
D

Dirk

Hello

The following code does not work

ArrayList __gc* allControls = new ArrayList();
.....
// put some Controls in allControls
....
Control __gc* arr[] = dynamic_cast<Control
__gc*[]>(allControls->ToArray(__typeof(Control)));

The cast does not succeed and arr is NULL. This is a translated version of
some C# code I saw. So I think it is supposed to work. If I use allControls
directly and iterate through the array casting each item to Control __gc* it
also works. So the items in allControl are really of type Control __gc*.

Thanks
 
Dirk,
The following code does not work

ArrayList __gc* allControls = new ArrayList();
....
// put some Controls in allControls
...
Control __gc* arr[] = dynamic_cast<Control
__gc*[]>(allControls->ToArray(__typeof(Control)));

The cast does not succeed and arr is NULL. This is a translated version of
some C# code I saw. So I think it is supposed to work. If I use allControls
directly and iterate through the array casting each item to Control __gc* it
also works. So the items in allControl are really of type Control __gc*.

This seems to work just fine for me....:
Control __gc* allControls2[] =
dynamic_cast<Control __gc*
__gc[]>(allControls->ToArray(__typeof(Control)));

I'm not quite sure what the problem could be.... have you tried checking
that indeed allControls->ToArray() is not returning NULL itself?
 
Hello
I'm not quite sure what the problem could be.... have you tried checking
that indeed allControls->ToArray() is not returning NULL itself?

allControls->ToArray() returns a valid System::Array, only the cast fails.
I also inserted a __try_cast to Control * for each item I put into the
ArrayList, so there are indeed only valid controls in the ArrayList.


Tomas Restrepo (MVP) said:
Dirk,
The following code does not work

ArrayList __gc* allControls = new ArrayList();
....
// put some Controls in allControls
...
Control __gc* arr[] = dynamic_cast<Control
__gc*[]>(allControls->ToArray(__typeof(Control)));

The cast does not succeed and arr is NULL. This is a translated version of
some C# code I saw. So I think it is supposed to work. If I use allControls
directly and iterate through the array casting each item to Control
__gc*
it
also works. So the items in allControl are really of type Control __gc*.

This seems to work just fine for me....:
Control __gc* allControls2[] =
dynamic_cast<Control __gc*
__gc[]>(allControls->ToArray(__typeof(Control)));

I'm not quite sure what the problem could be.... have you tried checking
that indeed allControls->ToArray() is not returning NULL itself?
 
Back
Top