managed jagged array - returning from method

I have an interface defined in C# with a method that returns int[][]. So far, I have not found away to implement this method in managed c++. I can declare the method:

int Class1::GetJaggedArray() __gc[] __gc[]

{

return NULL;

}

But I have yet to find away to declare, allocate and return a managed jagged array.

I get

Class1.cpp(102) : error C2691: 'int __gc[]' : invalid type for __gc array element

Likewise, my C# interface declares a method that takes an int[][]. However, I can't even declare a method such as

void Class1::SetJaggedArray(int foo __gc[] __gc[])

{

}

This gives the same error C2691

Is it possible to do any of this

I have control over the interface in this case, so I suppose I'll just work around it using Array __gc* objects, but I'd sure like it if this works. I'm using 1.1, any chance it works in 2.0

Please let me know if there's another / better / more appropriate place to post this question

Thanks.



Answer this question

managed jagged array - returning from method

  • TheMilkMan

    IIRC the old syntax does not support jagged arrays for some reason. Array* should work, even though it's obviously not very convenient.

    You can do it with the new syntax (VC 8.0 also supports the old syntax to a certain extent).

    The syntax for arrays uses a pseudo-template like

    template < typename T, unsigned Rank = 1> ref class array : public System::Array ...

    Hence, a jagged array looks like

    array<array<Element>^ >^

    You can now also use expression templates to implement your own array layout strategies (Jagged arrays obviously aren't very efficient for certain operations or densities).

    -hg


  • Mr Big

    BTW: Nish has a very useful article on C++/CLI arrays on codeproject (you might want to take a look at his other articles, too)

    http://www.codeproject.com/managedcpp/cppcliarrays.asp

    It might be slightly outdated, however (IIRC it's ::cli and no longer ::stdcli::language if you need a qualified name)

    -hg


  • managed jagged array - returning from method