Why manipulating 1 object affects the other?

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

    Why manipulating 1 object affects the other?

    I created 2 objects from a custom collection class.

    IntCollection a = new IntCollection() ;
    IntCollection b = new IntCollection() ;
    a.FromInt32Arra y(someIntArray) ;
    b = a;

    However when ever I attempt to remove items from the object a, somehow b
    will be affected. Is there a reason why this is happening?


  • Jim Hughes

    #2
    Re: Why manipulating 1 object affects the other?

    Reassigning a variable will cause the loss of whatever data the variable was
    originally assigned unless there is another reference to that data.

    // Assignment
    IntCollection b = new IntCollection() ;
    // Reassignment causes loss of original intCollection that was assigned to b
    b = a;
    // Now both b and a are variables that reference the intCollection first
    assigned to a

    "Ryu" <blizzardstorm8 899@yahoo.com> wrote in message
    news:emoz45r9EH A.2540@TK2MSFTN GP09.phx.gbl...[color=blue]
    >I created 2 objects from a custom collection class.
    >
    > IntCollection a = new IntCollection() ;
    > IntCollection b = new IntCollection() ;
    > a.FromInt32Arra y(someIntArray) ;
    > b = a;
    >
    > However when ever I attempt to remove items from the object a, somehow b
    > will be affected. Is there a reason why this is happening?
    >[/color]


    Comment

    • Ryu

      #3
      Re: Why manipulating 1 object affects the other?

      So what should I do to prevent both variables from having the same
      reference.
      "Jim Hughes" <NOSPAMJ3033@Ho tmail.com> wrote in message
      news:eTXDXFs9EH A.2180@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Reassigning a variable will cause the loss of whatever data the variable
      > was originally assigned unless there is another reference to that data.
      >
      > // Assignment
      > IntCollection b = new IntCollection() ;
      > // Reassignment causes loss of original intCollection that was assigned to
      > b
      > b = a;
      > // Now both b and a are variables that reference the intCollection first
      > assigned to a
      >
      > "Ryu" <blizzardstorm8 899@yahoo.com> wrote in message
      > news:emoz45r9EH A.2540@TK2MSFTN GP09.phx.gbl...[color=green]
      >>I created 2 objects from a custom collection class.
      >>
      >> IntCollection a = new IntCollection() ;
      >> IntCollection b = new IntCollection() ;
      >> a.FromInt32Arra y(someIntArray) ;
      >> b = a;
      >>
      >> However when ever I attempt to remove items from the object a, somehow b
      >> will be affected. Is there a reason why this is happening?
      >>[/color]
      >
      >[/color]


      Comment

      • Richard Blewett [DevelopMentor]

        #4
        Re: Why manipulating 1 object affects the other?

        Don't perform the assignment

        a=b;

        Is IntCollection a type you have defined?

        And what are you trying to do, get a copy of a into b?

        Regards

        Richard Blewett - DevelopMentor



        So what should I do to prevent both variables from having the same
        reference.

        Comment

        • Ryu

          #5
          Re: Why manipulating 1 object affects the other?

          Yes it is a custom type that I have defined. And Yes I am trying to get a
          copy of a into b.
          "Richard Blewett [DevelopMentor]" <richardb@NOSPA Mdevelop.com> wrote in
          message news:uDseUnt9EH A.2180@TK2MSFTN GP12.phx.gbl...[color=blue]
          > Don't perform the assignment
          >
          > a=b;
          >
          > Is IntCollection a type you have defined?
          >
          > And what are you trying to do, get a copy of a into b?
          >
          > Regards
          >
          > Richard Blewett - DevelopMentor
          > http://www.dotnetconsult.co.uk/weblog
          > http://www.dotnetconsult.co.uk
          >
          > So what should I do to prevent both variables from having the same
          > reference.
          >[/color]


          Comment

          • MattC

            #6
            Re: Why manipulating 1 object affects the other?

            Would b = a.Clone(); work? Or is the clone method not deep enough?

            MattC
            "Ryu" <blizzardstorm8 899@yahoo.com> wrote in message
            news:%23IPp1nu9 EHA.3908@TK2MSF TNGP12.phx.gbl. ..[color=blue]
            > Yes it is a custom type that I have defined. And Yes I am trying to get a
            > copy of a into b.
            > "Richard Blewett [DevelopMentor]" <richardb@NOSPA Mdevelop.com> wrote in
            > message news:uDseUnt9EH A.2180@TK2MSFTN GP12.phx.gbl...[color=green]
            >> Don't perform the assignment
            >>
            >> a=b;
            >>
            >> Is IntCollection a type you have defined?
            >>
            >> And what are you trying to do, get a copy of a into b?
            >>
            >> Regards
            >>
            >> Richard Blewett - DevelopMentor
            >> http://www.dotnetconsult.co.uk/weblog
            >> http://www.dotnetconsult.co.uk
            >>
            >> So what should I do to prevent both variables from having the same
            >> reference.
            >>[/color]
            >
            >[/color]


            Comment

            • Richard Blewett [DevelopMentor]

              #7
              Re: Why manipulating 1 object affects the other?

              Reference types don't copy by default. You will have to add copy functionality to your IntCollection and call it. There is an interface called ICloneable which has a single method Clone on it - its defined like this

              public interface ICloneable
              {
              object Clone();
              }

              You implement this interface to give your type copy semantics and would change your code to this

              IntCollection a = new IntCollection() ;
              IntCollection b = (IntCollection) a.Clone();

              In your Clone method you would have something like this (I'm making some assumptions of how your IntCollection works from its name)

              public object Clone()
              {
              IntCollection ret = new IntCollection() ;
              foreach( int i in this.myIntStore )
              {
              ret.Add(i);
              }
              return ret;
              }

              One last thing is I remember reading some time ago that there were plans to obsolete IColneable in the next version but I haven't downloaded the latest build so I don't know if that still is the case. If it is you might simply want to add a public method, something like

              public IntCollection Copy()
              {
              // details similar to above
              }

              and have your calling code as

              IntCollection a = new IntCollection() ;
              IntCollection b = a.Copy();

              Regards

              Richard Blewett - DevelopMentor



              Yes it is a custom type that I have defined. And Yes I am trying to get a
              copy of a into b.
              "Richard Blewett [DevelopMentor]" <richardb@NOSPA Mdevelop.com> wrote in
              message news:uDseUnt9EH A.2180@TK2MSFTN GP12.phx.gbl...[color=blue]
              > Don't perform the assignment
              >
              > a=b;
              >
              > Is IntCollection a type you have defined?
              >
              > And what are you trying to do, get a copy of a into b?
              >
              > Regards
              >
              > Richard Blewett - DevelopMentor
              > http://www.dotnetconsult.co.uk/weblog
              > http://www.dotnetconsult.co.uk
              >
              > So what should I do to prevent both variables from having the same
              > reference.
              >[/color]

              Comment

              Working...