Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2007

Tip: Looking for answers? Try searching our database.

array creation (elementary question)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gmb - 03 Jun 2007 10:18 GMT
(.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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.