ArrayList consisting of objects

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

    ArrayList consisting of objects

    Hi,
    Here is a piece of code where I have a class by name SomeClass
    array1 consists of objects of the type SomeClass
    array2 is intialized with contents of array1.
    Why does changes made to the object of array2 affect the same object in
    array1.

    The o/p for the following should be just '1' but why is it '11'?

    ArrayList array1 = new ArrayList();
    array1.Add(new SomeClass("1")) ;
    array1.Add(new SomeClass("2")) ;

    ArrayList array2 = new ArrayList();
    array2 = array1;
    array1.Add(new SomeClass("3")) ;


    SomeClass al = (SomeClass) array2[0];
    al.AlName = "11";

    SomeClass newObj = (SomeClass) array1[0];
    MessageBox.Show (newObj.AlName) ;

  • Steven Nagy

    #2
    Re: ArrayList consisting of objects

    Hi

    This is standard behaviour for "Reference" types.
    You should have a read about the difference between value types and
    reference types (just google it). Class instances are reference types.

    SN

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: ArrayList consisting of objects

      In addition to Steven,

      Have a look at the keyword "new"
      "new" tells that it is a new array (you are instancing that from a Class
      running its instancing method)

      Without it it is telling that it is referencing (setting the reference) to
      an existing one.

      Cor

      "ZS" <ZS@discussions .microsoft.coms chreef in bericht
      news:035E8687-4045-4287-A56C-6E43810D9F16@mi crosoft.com...
      Hi,
      Here is a piece of code where I have a class by name SomeClass
      array1 consists of objects of the type SomeClass
      array2 is intialized with contents of array1.
      Why does changes made to the object of array2 affect the same object in
      array1.
      >
      The o/p for the following should be just '1' but why is it '11'?
      >
      ArrayList array1 = new ArrayList();
      array1.Add(new SomeClass("1")) ;
      array1.Add(new SomeClass("2")) ;
      >
      ArrayList array2 = new ArrayList();
      array2 = array1;
      array1.Add(new SomeClass("3")) ;
      >
      >
      SomeClass al = (SomeClass) array2[0];
      al.AlName = "11";
      >
      SomeClass newObj = (SomeClass) array1[0];
      MessageBox.Show (newObj.AlName) ;
      >

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: ArrayList consisting of objects

        ZS <ZS@discussions .microsoft.comw rote:
        Here is a piece of code where I have a class by name SomeClass
        array1 consists of objects of the type SomeClass
        array2 is intialized with contents of array1.
        Why does changes made to the object of array2 affect the same object in
        array1.
        >
        The o/p for the following should be just '1' but why is it '11'?
        >
        ArrayList array1 = new ArrayList();
        array1.Add(new SomeClass("1")) ;
        array1.Add(new SomeClass("2")) ;
        >
        ArrayList array2 = new ArrayList();
        Calling new ArrayList() here is pointless when you're about to reassign
        the value:
        array2 = array1;
        This makes the variables array2 and array1 both have values which are
        *references* to the same ArrayList. Think of it as two people each with
        a piece of paper which has the address of the same house. If one person
        goes to the house and puts a chair in it (adds an item to the list, in
        this case), then the other person will see that chair when they go to
        the house.

        <snip>

        See http://www.pobox.com/~skeet/csharp/parameters.html and
        http://www.pobox.com/~skeet/csharp/memory.html for more information on
        reference types and value types.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • ZS

          #5
          RE: ArrayList consisting of objects

          So how can I change this piece of code so that it does not occur.

          -Zelma

          "ZS" wrote:
          Hi,
          Here is a piece of code where I have a class by name SomeClass
          array1 consists of objects of the type SomeClass
          array2 is intialized with contents of array1.
          Why does changes made to the object of array2 affect the same object in
          array1.
          >
          The o/p for the following should be just '1' but why is it '11'?
          >
          ArrayList array1 = new ArrayList();
          array1.Add(new SomeClass("1")) ;
          array1.Add(new SomeClass("2")) ;
          >
          ArrayList array2 = new ArrayList();
          array2 = array1;
          array1.Add(new SomeClass("3")) ;
          >
          >
          SomeClass al = (SomeClass) array2[0];
          al.AlName = "11";
          >
          SomeClass newObj = (SomeClass) array1[0];
          MessageBox.Show (newObj.AlName) ;
          >

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: ArrayList consisting of objects

            ZS,

            That is completely depending for what you want to do, there are not for
            nothing both solutions.

            Cor

            "ZS" <ZS@discussions .microsoft.coms chreef in bericht
            news:582D72A5-343E-425D-9392-35DBCA35F401@mi crosoft.com...
            So how can I change this piece of code so that it does not occur.
            >
            -Zelma
            >
            "ZS" wrote:
            >
            >Hi,
            >Here is a piece of code where I have a class by name SomeClass
            >array1 consists of objects of the type SomeClass
            >array2 is intialized with contents of array1.
            >Why does changes made to the object of array2 affect the same object in
            >array1.
            >>
            >The o/p for the following should be just '1' but why is it '11'?
            >>
            >ArrayList array1 = new ArrayList();
            >array1.Add(n ew SomeClass("1")) ;
            >array1.Add(n ew SomeClass("2")) ;
            >>
            >ArrayList array2 = new ArrayList();
            >array2 = array1;
            >array1.Add(n ew SomeClass("3")) ;
            >>
            >>
            >SomeClass al = (SomeClass) array2[0];
            >al.AlName = "11";
            >>
            >SomeClass newObj = (SomeClass) array1[0];
            >MessageBox.Sho w (newObj.AlName) ;
            >>

            Comment

            Working...