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. object references

object references

Scheduled Pinned Locked Moved C#
help
3 Posts 3 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.
  • A Offline
    A Offline
    amit_83
    wrote on last edited by
    #1

    Hi, i have two object of same class, suppose object name are class1 and class2. and i have made class1 object and assign it to class2 object.if i do make chages in class2 then it also affact to class2 object. but i dont want this.means if i make any changes in class2 then it should not affact to class1 object. so for thos what should i do. please help.

    amit

    V C 2 Replies Last reply
    0
    • A amit_83

      Hi, i have two object of same class, suppose object name are class1 and class2. and i have made class1 object and assign it to class2 object.if i do make chages in class2 then it also affact to class2 object. but i dont want this.means if i make any changes in class2 then it should not affact to class1 object. so for thos what should i do. please help.

      amit

      V Offline
      V Offline
      Vikram A Punathambekar
      wrote on last edited by
      #2

      First, calling an object class1 or class2 is a bad idea. Second, when you say obj1 = obj2; with reference types, you are not assigning one object to another, you are only assigning a reference to another.

      amit_83 wrote:

      if i do make chages in class2 then it also affact to class2 object.

      Yes.

      amit_83 wrote:

      but i dont want this.means if i make any changes in class2 then it should not affact to class1 object.

      1. Don't assign the references. 2. If you absolutely must do something of the sort, consider implementing your Clone() method which creates a new object.

      Cheers, Vikram.


      "I will put my new found knolage to good use" - Captain See Sharp. "Every time Lotus Notes starts up, somewhere a puppy, a kitten, a lamb, and a baby seal are killed." - Gary Wheeler.

      1 Reply Last reply
      0
      • A amit_83

        Hi, i have two object of same class, suppose object name are class1 and class2. and i have made class1 object and assign it to class2 object.if i do make chages in class2 then it also affact to class2 object. but i dont want this.means if i make any changes in class2 then it should not affact to class1 object. so for thos what should i do. please help.

        amit

        C Offline
        C Offline
        CKnig
        wrote on last edited by
        #3

        Well it's confusing at start but try to think of all "class"-objects as POINTERS or references (in C++). You've got several possible solutions: 1.) Implement something like Clone (like proposed) - normaly you just add an copy-constructor so you can say MyClass Obj2 = new MyClass(Obj1) and use this in the Clone()-implementation - this way you can save one cast 2.) use struct instead of class - this will locate your objects in the local stack and copy the data instead of the reference when you say (obj2 = obj1) 3.) use the same data to construct the object 2 times with new etc.

        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