Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Add variable to object reference

Add variable to object reference

Scheduled Pinned Locked Moved C#
question
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rick van Woudenberg
    wrote on last edited by
    #1

    Dear all, If I create a new reference to an object, I would like to add a variable to that newly create reference. Let's say : Foo foo = new Foo(); Now, everythime the code runs, I would like a variable added to foo, let's say : First time it is called : Foo foo1 = new Foo(); Second : Foo foo2 = new Foo(); Is there an easy way of doing this ? Cheers,

    L V L C R 5 Replies Last reply
    0
    • R Rick van Woudenberg

      Dear all, If I create a new reference to an object, I would like to add a variable to that newly create reference. Let's say : Foo foo = new Foo(); Now, everythime the code runs, I would like a variable added to foo, let's say : First time it is called : Foo foo1 = new Foo(); Second : Foo foo2 = new Foo(); Is there an easy way of doing this ? Cheers,

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      No. What is the actual problem, why would you want this ? :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/AllLanguages/General - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


      1 Reply Last reply
      0
      • R Rick van Woudenberg

        Dear all, If I create a new reference to an object, I would like to add a variable to that newly create reference. Let's say : Foo foo = new Foo(); Now, everythime the code runs, I would like a variable added to foo, let's say : First time it is called : Foo foo1 = new Foo(); Second : Foo foo2 = new Foo(); Is there an easy way of doing this ? Cheers,

        V Offline
        V Offline
        Vasudevan Deepak Kumar
        wrote on last edited by
        #3

        foo1 and foo2 are separate instances.

        Vasudevan Deepak Kumar Personal Homepage Tech Gossips

        1 Reply Last reply
        0
        • R Rick van Woudenberg

          Dear all, If I create a new reference to an object, I would like to add a variable to that newly create reference. Let's say : Foo foo = new Foo(); Now, everythime the code runs, I would like a variable added to foo, let's say : First time it is called : Foo foo1 = new Foo(); Second : Foo foo2 = new Foo(); Is there an easy way of doing this ? Cheers,

          L Offline
          L Offline
          laddie 0
          wrote on last edited by
          #4

          Hi , I too wonder of the exact situation you would require to do that.If you can tell that we should be able to help you ourt more. However calling the new on any object will be creating a new instance itself eg: Foo f; for(int i=0;i<10;i++) { f= new Foo(); } will create 10 new instances rather than 1 even though you are using the same variable name. laddie

          1 Reply Last reply
          0
          • R Rick van Woudenberg

            Dear all, If I create a new reference to an object, I would like to add a variable to that newly create reference. Let's say : Foo foo = new Foo(); Now, everythime the code runs, I would like a variable added to foo, let's say : First time it is called : Foo foo1 = new Foo(); Second : Foo foo2 = new Foo(); Is there an easy way of doing this ? Cheers,

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Rick van Woudenberg wrote:

            Is there an easy way of doing this ?

            Sounds like you need an array or list

            List fooList = new List();
            fooList.Add(new Foo());


            Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

            1 Reply Last reply
            0
            • R Rick van Woudenberg

              Dear all, If I create a new reference to an object, I would like to add a variable to that newly create reference. Let's say : Foo foo = new Foo(); Now, everythime the code runs, I would like a variable added to foo, let's say : First time it is called : Foo foo1 = new Foo(); Second : Foo foo2 = new Foo(); Is there an easy way of doing this ? Cheers,

              R Offline
              R Offline
              Rick van Woudenberg
              wrote on last edited by
              #6

              Thank you all for you swift reply. Very true , new would create a new instance ( hence the name ). I can understand why this could be confusing. I will try to explain as good as I can why I would want this. I create a new array of points, which I add to a ObjectList. private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { foreach (DataGridViewRow SelectedRow in dataGridView.SelectedRows) { Points pt = new Points(); pt.AddPoint(X,Y); RenderableObjects.Objects.Add(pt); } } When I rerender the form, the points (X,Y) show on the screen. So everytime I click on a row in the datagridview, the method is called. However, everytime a new instance of Points() is added to the RenderableObjects list. But before adding the new X and Y point , I want the list to be cleared. This can be achieved by : RenderableObjects.Objects.Remove(pt); However you can't use this line in the same method, since a new reference is initiated and therefore the previous pt is not removed. My though was ( and it was just a though ) : If I could add a variable to the new reference, I could specifically filter, something like :

              = new Points(); pt[x].AddPoint(X,Y); RenderableObjects.Objects.Add(pt[x]); int y = x -1 ; RenderableObjects.Objects.Remove(pt[y]); x++; } }
              For obvious reasons , this code would not run 'cause you cannot use an array (x) size on a variable declaration. Like I said, it was just a thought. I hope I was able to clearify it a bit more. Cheers

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups