array creation (elementary question)

  • Thread starter Thread starter gmb
  • Start date Start date
G

gmb

(.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
 
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).
--
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
 
gmb said:
(.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()" ?

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.
 
Back
Top