Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2006

Tip: Looking for answers? Try searching our database.

int[,] array in C++?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
olexij.tkatchenko@googlemail.com - 28 Nov 2006 15:09 GMT
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++?
William DePalo [MVP VC++] - 28 Nov 2006 15:33 GMT
> I am failing to create appropriate array in C++ to pass to a C# method.
> The method looks like
[quoted text clipped - 6 lines]
> 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
Marcus Heege - 28 Nov 2006 15:54 GMT
> 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
David Anton - 28 Nov 2006 15:52 GMT
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}};
Signature

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

> Hello,
> I am failing to create appropriate array in C++ to pass to a C# method.
[quoted text clipped - 7 lines]
> it fails. So what is the proper way to declare an equivalent of int[,]
> in C++?
olexij.tkatchenko@googlemail.com - 28 Nov 2006 16:02 GMT
Thank Will and David,
array<int, 2> ^foo = {{1,2}, {3,4}};  is what I needed!
William DePalo [MVP VC++] - 28 Nov 2006 16:12 GMT
> 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
olexij.tkatchenko@googlemail.com - 28 Nov 2006 16:27 GMT
William DePalo [MVP VC++] schrieb:

> > Thank Will and David,
> > array<int, 2> ^foo = {{1,2}, {3,4}};  is what I needed!
[quoted text clipped - 11 lines]
> Regards,
> Will

Typing takes some time, I just overlooked Markus's answer :) Thanks to
you too, Markus!
David Anton - 28 Nov 2006 16:19 GMT
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}};
Signature

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

> Thank Will and David,
> array<int, 2> ^foo = {{1,2}, {3,4}};  is what I needed!
olexij.tkatchenko@googlemail.com - 28 Nov 2006 16:02 GMT
Thanks Will and David,
array<int, 2> ^foo = {{1,2}, {3,4}};  is what I needed!

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.