(.NET 2.0)
code:
array<TextBox^> ^dialog_txb = {gcnew TextBox(),gcnew TextBox(),gcnew
TextBox(),gcnew TextBox(),gcnew TextBox()};
question:
Is there a shorter way to declare and initialize an array of 5 objects
without repetition of "gcnew TextBox()" ?
(What if I need to create an array with much bigger number of elements?)
Thanks in advance,
gmb
David Anton - 03 Jun 2007 14:56 GMT
You only should use that syntax if you have the specific instances ready to
insert.
Instead use:
array<TextBox^> ^dialog_txb = gcnew array<TextBox^>(5);
and then when you have the instance of TextBox that you want in the array,
add them using standard array syntax (e.g., dialog_txb[0] =
some_specific_textbox, or dialog_txb[0] = gcnew TextBox() - obviously in a
loop if you have many that you're adding this way).

Signature
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
C++ to C# Converter: converts C++ to C#
Instant C++: converts C# or VB to C++/CLI
> (.NET 2.0)
>
[quoted text clipped - 9 lines]
> Thanks in advance,
> gmb
Ben Voigt [C++ MVP] - 04 Jun 2007 14:38 GMT
> (.NET 2.0)
>
[quoted text clipped - 5 lines]
> Is there a shorter way to declare and initialize an array of 5 objects
> without repetition of "gcnew TextBox()" ?
Please realize that .NET has no concept of initialized array. If you look
at the IL, you will see that it first creates the array, and then _assigns_
(not initializes) each element in sequence. So you will get the exact same
behavior using a loop.
> (What if I need to create an array with much bigger number of elements?)
>
> Thanks in advance,
> gmb