array creation (elementary question)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gmb

    #1

    array creation (elementary question)

    (.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

  • =?Utf-8?B?RGF2aWQgQW50b24=?=

    #2
    RE: array creation (elementary question)

    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_t extbox, or dialog_txb[0] = gcnew TextBox() - obviously in a
    loop if you have many that you're adding this way).
    --
    David Anton
    Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

    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" wrote:
    (.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
    >
    >

    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: array creation (elementary question)


      "gmb" <diehard_gmb@wp .plwrote in message
      news:u$zH3AcpHH A.3880@TK2MSFTN GP04.phx.gbl...
      (.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.
      (What if I need to create an array with much bigger number of elements?)
      >
      Thanks in advance,
      gmb

      Comment

      Working...