int[,] array in C++?

O

olexij.tkatchenko

Hello,
I am failing to create appropriate array in C++ to pass to a C# method.
The method looks like
public Foo(int[,] data) {..}

in C#. When I try to call it with an array initialized in C++ as:

int inputArray[][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};

it fails. So what is the proper way to declare an equivalent of int[,]
in C++?
 
W

William DePalo [MVP VC++]

I am failing to create appropriate array in C++ to pass to a C# method.
The method looks like
public Foo(int[,] data) {..}

in C#. When I try to call it with an array initialized in C++ as:

int inputArray[][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};

it fails. So what is the proper way to declare an equivalent of int[,]
in C++?

Well, the purists will wince when they read this but ...

.... which C++ language are you talking about? :)

Arrays in C# are instances of the System::Array class.

In MC++ (VS 2003), to create a managed array of integers you'd make the
declaration

Int32 inputArray[,] = new Int32[4,2];

and the compiler will do the right thing.

Alternatively, you should be able to cook up something using

Array::CreateInstance()

I've not used C++/CLI (in VS 2005) much so I'm not all that familiar with
the syntax using it. My guess is that new is replaced by gcnew in the line
above but that is a WAG.

As for ISO C++ - if you really want your C# code to see a managed array - I
don't think it is an option though I could be wrong.

Regards,
Will
 
G

Guest

In C++/CLI, you'd declare the array as:
array<int, 2> ^foo;
or (with value initialization):
array<int, 2> ^foo = {{1,2}, {3,4}};
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
 
M

Marcus Heege

I've not used C++/CLI (in VS 2005) much so I'm not all that familiar with
the syntax using it. My guess is that new is replaced by gcnew in the line
above but that is a WAG.

array<int, 2>^ arr = gcnew array<int, 2>(3, 4);

Marcus
 
O

olexij.tkatchenko

Thank Will and David,
array<int, 2> ^foo = {{1,2}, {3,4}}; is what I needed!
 
O

olexij.tkatchenko

Thanks Will and David,
array<int, 2> ^foo = {{1,2}, {3,4}}; is what I needed!
 
W

William DePalo [MVP VC++]

Thank Will and David,
array<int, 2> ^foo = {{1,2}, {3,4}}; is what I needed!

It's a good thing that thanks aren't necessary because Marcus (who deserved
one) didn't get one and I (who didn't deserve one because I offered a VS2003
solution) got one. :)

Seriously, the thing that you should take away from this exchange is that
unlike the native case where arrays are simply "objects" layed out
sequentially in memory where the "next" storage location may or may not be
what you expect, arrays are types in their own right in .Net in which the
array's "size" is known.

Regards,
Will
 
G

Guest

By the way, I gave you the short-form value initialization, which is only
valid in a declaration.
Some people may prefer the long form, which is:
array<int, 2> ^foo = gcnew array<int, 2>(2,2) {{1,2}, {3,4}};
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
 
O

olexij.tkatchenko

William said:
It's a good thing that thanks aren't necessary because Marcus (who deserved
one) didn't get one and I (who didn't deserve one because I offered a VS2003
solution) got one. :)

Seriously, the thing that you should take away from this exchange is that
unlike the native case where arrays are simply "objects" layed out
sequentially in memory where the "next" storage location may or may not be
what you expect, arrays are types in their own right in .Net in which the
array's "size" is known.

Regards,
Will

Typing takes some time, I just overlooked Markus's answer :) Thanks to
you too, Markus!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top