• Welcome to Valhalla Legends Archive.
 

[C++.NET] Variable length arrays?

Started by Dyndrilliac, August 31, 2006, 01:59 PM

Previous topic - Next topic

Dyndrilliac

I've been trying to figure out how the correct syntax for creating a variable length array should go, and having very bad results. Here's what I've got:array<Byte>^ MyBuffer[(const UInt32)i_len+4] = {0};I also tried:
  • Array ^MyBuffer = System::Array::CreateInstance(System::Byte::typeid, i_len+4);
  • Array *MyBuffer = System::Array::CreateInstance(System::Byte::typeid, i_len+4);
  • const UInt32 Len = (i_len+4);
    array<Byte>^ MyBuffer[Len] = {0};
All to no avail. Also MSDN spits up bunches of results in their Search utility with old syntax making it pretty worthless -.- I'm using .NET Framework 2.0 btw.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

K

#1
A good tip is when searching for .NET 2.0 things, search http://msdn2.microsoft.com instead of msdn.microsoft.com.  Also try using "C++/CLI" on google instead of "Managed C++" or "C++.NET"

Oh, and

array<Byte>^ myarray = gcnew array<Byte>(Len);


Just like in C#, all objects must be allocated with new;  to differentiate the managed new from the standard c++ new, it is called "gcnew" garbage collected new).


Edit:  the carrot symbol means an item is a pointer to a managed object, as opposed to a pointer to a regular object (*).  So to create an array of managed strings, you would do this:


array<String^>^ myarray = gcnew array<String^>(Len);

HTH

Dyndrilliac

Oh! Thanks for the info, now things make a lot more sense. I'm really starting to enjoy C++.net. The interoperability is really nice, as is garbage collection.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.