Answer to Question #25014 in C++ for ali akhtar

Question #25014
I am Making a snake game in c++/CLI Windows Form.. and I need your help to make Dynamic (System::Drawing::Rectangle) array that increase array->Length when i call some event ... Inshort is it possible to increase the length of array when i call some event...
Expert's answer
You can't change array size in C++/CLI
but there are two possible ways to solve your problem.
1. You can create a new array with a bigger size
and copy old array items to it, then add new Rectangle to the end of the new array.
(you will do it every time the event is handled so
it's not a memory and time-efficient method).
2. You can use collections. First of all declare a private field in your form class:
List<System::Drawing::Rectangle>^ rects = gcnew List<System::Drawing::Rectangle>();
and somewhere in your event handler method
rects->Add(System::Drawing::Rectangle(0, 9, 4, 5));
simply add new rectangles to your list.
And don't forget to import the System::Collections::Generic namespace
where the List type is defined:
using namespace System::Collections::Generic;

Read more and see how to work with lists in C++/CLI here:

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

New on Blog