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. Passing a class by value, but reversed?

Passing a class by value, but reversed?

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 2 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.
  • M Offline
    M Offline
    monrobot13
    wrote on last edited by
    #1

    I have a collection class that I've derived from List<MyClass>. I've added no extra functionality to this class except a constructor that clears the list. My problem is this: I'm using the collection in MyClass (the same class that the list is typed for) and I'm doing this

    // fill MyClass with some values
    col.Add (this);
    // where 'col' is my generic List<> dervied class

    No problem there it adds the class just fine. The problem arises when I run the code again (it's in a loop), what's happening is that the previous values in the list are changed to the new values that the class has taken on. In effect, if I run the loop, let's say, 5 times the List<> has 5 items in it, but they all have the exact same values. Does anyone have any ideas as to how I can defeat this? I'm thinking there's something better I could be passing into the Add function, but I'm not sure what it would be. I was also thinking I could override the Add function in my derived collection class and create a deep copy of the this parmeter that I passed in, but I'm not sure if that's the right idea. Any ideas and/or help is very appreciated. Thanks. - Aaron

    S 1 Reply Last reply
    0
    • M monrobot13

      I have a collection class that I've derived from List<MyClass>. I've added no extra functionality to this class except a constructor that clears the list. My problem is this: I'm using the collection in MyClass (the same class that the list is typed for) and I'm doing this

      // fill MyClass with some values
      col.Add (this);
      // where 'col' is my generic List<> dervied class

      No problem there it adds the class just fine. The problem arises when I run the code again (it's in a loop), what's happening is that the previous values in the list are changed to the new values that the class has taken on. In effect, if I run the loop, let's say, 5 times the List<> has 5 items in it, but they all have the exact same values. Does anyone have any ideas as to how I can defeat this? I'm thinking there's something better I could be passing into the Add function, but I'm not sure what it would be. I was also thinking I could override the Add function in my derived collection class and create a deep copy of the this parmeter that I passed in, but I'm not sure if that's the right idea. Any ideas and/or help is very appreciated. Thanks. - Aaron

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Can you make MyClass a struct? That way, it'll behave exactly as you want it to. The only other way is to clone the object, using MemberwiseClone or your custom deep cloning code. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      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