Object name as variable

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

    Object name as variable

    How can I declare a new object with a variable name ?
    I'm trying to add many objects with s for each loop
    for ....
    Object varname = new Object() ......


  • Peter Koen

    #2
    Re: Object name as variable

    "Sorin Sandu" <ssandu@bizland .ro> wrote in news:O3l8ipgmDH A.360
    @TK2MSFTNGP12.p hx.gbl:
    [color=blue]
    > How can I declare a new object with a variable name ?
    > I'm trying to add many objects with s for each loop
    > for ....
    > Object varname = new Object() ......
    >
    >
    >[/color]

    if i understand you correctly you want pass the name of the object type in
    a string...

    using System.Reflecti on;

    Assembly.GetExe cutingAssembly( ).CreateInstanc e(.....)

    or

    Activator.Creat eInstance(....)

    --
    best regards

    Peter Koen
    -----------------------------------
    MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Object name as variable

      Sorin Sandu <ssandu@bizland .ro> wrote:[color=blue]
      > How can I declare a new object with a variable name ?[/color]

      You can't.
      [color=blue]
      > I'm trying to add many objects with s for each loop
      > for ....
      > Object varname = new Object() ......[/color]

      That sounds like a perfect place to use an array, or an ArrayList.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Morten Wennevik

        #4
        Re: Object name as variable

        On Fri, 24 Oct 2003 11:37:13 +0300, Sorin Sandu <ssandu@bizland .ro> wrote:
        [color=blue]
        > How can I declare a new object with a variable name ?
        > I'm trying to add many objects with s for each loop
        > for ....
        > Object varname = new Object() ......
        >
        >[/color]

        You don't need a new name for each object. If you store the objects in an
        array they will each get a "name" as ArrayList[x].

        If you need to add your own name, one way could be to create a new struct
        having a name and an object.

        --
        Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

        Comment

        • news.microsoft.com

          #5
          Re: Object name as variable

          That would be interesting code if u could :D
          Imagine debugging that :D

          "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
          news:oprxjm59kp hntkfz@localhos t...
          On Fri, 24 Oct 2003 11:37:13 +0300, Sorin Sandu <ssandu@bizland .ro> wrote:
          [color=blue]
          > How can I declare a new object with a variable name ?
          > I'm trying to add many objects with s for each loop
          > for ....
          > Object varname = new Object() ......
          >
          >[/color]

          You don't need a new name for each object. If you store the objects in an
          array they will each get a "name" as ArrayList[x].

          If you need to add your own name, one way could be to create a new struct
          having a name and an object.

          --
          Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


          Comment

          • Morten Wennevik

            #6
            Re: Object name as variable

            My point is you only need a reference to the object, not the original name
            of it.

            --
            Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

            Comment

            Working...