c# ArrayList Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • writeallnight
    New Member
    • Feb 2008
    • 7

    #1

    c# ArrayList Question

    I can't seem to "disconnect " a "rebuilt" ArrayList of objects from it's source AL; I thought I understood the rules on copying AL Data objects vs. the "clone" Method (and it's simply being a reference to the original ArrayList). The code looks like:
    [code=cpp]
    ArrayList alTemp1 = new ArrayList();
    ArrayList alTemp2 = (ArrayList)Sess ion["AStandingSessi onAL"];

    for (int i = 0; i < alTemp2.Count; i++) {
    alTemp1.Add((cl sAnObject)alTem p2[i]);
    ((clsAnObject)a lTemp1[i]).Price = 46.00;
    }
    [/code]
    This code will change the "Price" to 46.00 for all objects in the ORIGINAL ArrayList (Session["AStandingSessi onAL"]). I thought that if you created a new AL and did "Add" from another AL, this would "disconnect " or "Deep Copy" the AL??

    Thanks...
    Last edited by Frinavale; Mar 12 '08, 04:11 PM. Reason: added [code] tags
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by writeallnight
    I can't seem to "disconnect " a "rebuilt" ArrayList of objects from it's source AL; I thought I understood the rules on copying AL Data objects vs. the "clone" Method (and it's simply being a reference to the original ArrayList). The code looks like:
    [code=cpp]
    ArrayList alTemp1 = new ArrayList();
    ArrayList alTemp2 = (ArrayList)Sess ion["AStandingSessi onAL"];

    for (int i = 0; i < alTemp2.Count; i++) {
    alTemp1.Add((cl sAnObject)alTem p2[i]);
    ((clsAnObject)a lTemp1[i]).Price = 46.00;
    }
    [/code]
    This code will change the "Price" to 46.00 for all objects in the ORIGINAL ArrayList (Session["AStandingSessi onAL"]). I thought that if you created a new AL and did "Add" from another AL, this would "disconnect " or "Deep Copy" the AL??

    Thanks...
    When you do:
    [code=cpp]
    alTemp1.Add((cl sAnObject)alTem p2[i]);
    [/code]

    You are are adding the object's memory address (at i) to alTemp1...which is the same memory address as alTemp2. Therefore when you change the Price of the object, both ArrayLists are updated (since they're pointing to the same object).

    You should clone the object while adding it to alTemp1.

    -Frinny

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Yea, frinny is right. It's not so much that the ArrayList is being funnny as it is normal behaviour for objects.

      Consider:
      Code:
      Form myform1 = new Form();
      Form myform2 = myform1;
      //this is roughly what you are doing in your array list copying

      Comment

      • writeallnight
        New Member
        • Feb 2008
        • 7

        #4
        Thanks, but how do I "clone the object" while add ing it?

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Have you tried?
          alTemp1.Add( ((clsAnObject)a lTemp2[i]).Clone() );

          Comment

          • writeallnight
            New Member
            • Feb 2008
            • 7

            #6
            Doesn't work because my "custom" class/object does not have a method "clone"...

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Originally posted by writeallnight
              Doesn't work because my "custom" class/object does not have a method "clone"...
              Well then you willd need to figure out a method for copy/cloning your custom object.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Originally posted by Plater
                Well then you willd need to figure out a method for copy/cloning your custom object.
                I know this is a slight bit off topic but, can you over load operators in C#?
                You cannot in VB.NET (found out yesterday and wasn't to impressed).

                -Frinny

                Comment

                • writeallnight
                  New Member
                  • Feb 2008
                  • 7

                  #9
                  Thanks, I built a "Copy" constructor

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Originally posted by Frinavale
                    I know this is a slight bit off topic but, can you over load operators in C#?
                    You cannot in VB.NET (found out yesterday and wasn't to impressed).

                    -Frinny
                    Yeah I believe you can define what is done on a + or - or various other things like that in C#

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      Originally posted by Plater
                      Yeah I believe you can define what is done on a + or - or various other things like that in C#
                      It's pretty handy to be able to tell the compiler what to do with your object when + or - (...==..??) operators are used on your object.

                      Using VB has completely removed the benefits that come from this....

                      >>Gur<<

                      I wish I worked in C#

                      Comment

                      Working...