0

I want to declare an array of handel as the following code:

using namespace System::Drawing;
ref class B 
{
    Bitmap^ b[];

    B()
    {
        b = new Bitmap^[10];
    }
};

But it threw error when compiling

error C2728: 'System::Drawing::Bitmap ^' : a native array cannot contain this managed type
error C4368: cannot define 'b' as a member of managed 'B': mixed types are not supported
error C2728: 'System::Drawing::Bitmap ^' : a native array cannot contain this managed type
error C2440: '=' : cannot convert from 'System::Drawing::Bitmap ^*' to 'System::Drawing::Bitmap ^[]'

Someone can tell me the correctly way to declare an array of handel?

Many thanks!

T&TGroup

pstrjds
  • 16,840
  • 6
  • 52
  • 61
TTGroup
  • 3,575
  • 10
  • 47
  • 79
  • 8
    I'd rather have an array of Mozart than of Handel. Do you mean handle? – crashmstr Mar 30 '12 at 18:15
  • String handelsWorks[] = { "Nero", "Almira", "Messiah" } :) Just a joke. – Almo Mar 30 '12 at 18:15
  • 2
    Also I suggest just googling your first error to see the problem. You will be lead to the solution here: http://stackoverflow.com/questions/995434/arrays-of-strings-in-managed-c – Daniel Moses Mar 30 '12 at 18:22
  • 2
    You cannot store managed object references in an unmanaged array. The garbage collector won't be able to find them back. Use the *array* keyword. – Hans Passant Mar 30 '12 at 18:41

2 Answers2

5

You need to use gcnew since this a .Net array, not a C++ array since this is an array of a managed type, not an array of a native type. I don't have a compiler handy to test this code, but I believe this would be the way to do it.

using namespace System::Drawing;
ref class B 
{
private:
    array<Bitmap^>^ b;

public:
    B()
    {
        b = gcnew array<Bitmap^>(10);
    }
};
pstrjds
  • 16,840
  • 6
  • 52
  • 61
1

I would probably use a generic collection type instead of an array.

Not sure what a handel is, though.

djdanlib
  • 21,449
  • 1
  • 20
  • 29