Sizing Array Property at Runtime

  • Thread starter Thread starter Randy Hersom
  • Start date Start date
R

Randy Hersom

In C++ using Windows Forms, how do you declare, and then size, a
multidimensional array property whose size is determined at runtime?

I've been through several permutations and haven't been able to get it to
compile without error. What combination of declaration and runtime sizing
will allow me to accomplish this?

The line just below the ellipsis (...) is what's breaking. Here's my latest
failure:

private: System::Array * originalSizes;
....

this->originalSizes =
Array.CreateInstance(typeof(this->Controls->Item(z)->Top) ,
{this->Controls->Count,4});

for (int z=0;z<this->Controls->Count;z++) {

this->originalSizes[z,0]=this->Controls->Item(z)->Top;

this->originalSizes[z,1]=this->Controls->Item(z)->Left;

this->originalSizes[z,2]=this->Controls->Item(z)->Height;

this->originalSizes[z,3]=this->Controls->Item(z)->Width;

}
 
Hi Randy,
If the type of your data member in your array won't change, you may try
this simple way

System::Int32 arr[,] = new Int32[this->Controls->Count,4];
//Note, you must handle box/unbox of value type on your own
arr->Item[2,3] = __box(this->Controls->Item[0]->Top);

And, a little more advice, why uses multi-demensional array to save these
value?
You may get a Rectangle object by Controls->Item->ClientRectangle; you
code will even simple after using that
System::Drawing::Rectangle rcArr[] = NULL;
//Note: Rectangle is also value type
rcArr->Item[0] = __box(this->Controls->Item[0]->ClientRectangle;

If you really want to create the array dynamically ,here is an simple
example.

System::Array* Arr = NULL;
Arr =
System::Array::CreateInstance(__box(System::Int32())->GetType(),this->
Controls->Count,4);
Arr->Item[0,0] = __box(this->Controls->Item[0]->Top);
If you still have problem on this issue, please let me know.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.

--------------------
| From: "Randy Hersom" <[email protected]>
| Subject: Sizing Array Property at Runtime
| Date: Fri, 12 Sep 2003 11:32:03 -0400
| Lines: 30
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <O#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: adsl-156-203-113.clt.bellsouth.net 66.156.203.113
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:52242
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| In C++ using Windows Forms, how do you declare, and then size, a
| multidimensional array property whose size is determined at runtime?
|
| I've been through several permutations and haven't been able to get it to
| compile without error. What combination of declaration and runtime sizing
| will allow me to accomplish this?
|
| The line just below the ellipsis (...) is what's breaking. Here's my
latest
| failure:
|
| private: System::Array * originalSizes;
| ...
|
| this->originalSizes =
| Array.CreateInstance(typeof(this->Controls->Item(z)->Top) ,
| {this->Controls->Count,4});
|
| for (int z=0;z<this->Controls->Count;z++) {
|
| this->originalSizes[z,0]=this->Controls->Item(z)->Top;
|
| this->originalSizes[z,1]=this->Controls->Item(z)->Left;
|
| this->originalSizes[z,2]=this->Controls->Item(z)->Height;
|
| this->originalSizes[z,3]=this->Controls->Item(z)->Width;
|
| }
|
|
|
 
Back
Top