Results 1 to 4 of 4

Thread: Problem with array list

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598

    Problem with array list

    Hi All,

    I have this arraylist which I add 2 shape objects to it I also add the shape to a temp array list, why is it that when I change the values in a shape the values in the temp array list also get changed?

    Highlighted red is where I add the shapes to the first temp array list
    Highlighted blue is where I add the shapes to the second temp array list

    VB Code:
    1. Drawable shape;
    2.         int ClickedShape = -1;
    3.         ArrayList primitives = new ArrayList();
    4.         List<ArrayList> Undo = new List<ArrayList>();
    5.         int startx = 0;
    6.         int starty = 0;
    7.         bool CanMove = false;
    8.         int UndoPosition = 0;
    9.  
    10.         private void button1_Click(object sender, EventArgs e)
    11.         {
    12.  
    13.             GraphicsPath path = new GraphicsPath();
    14.             Rectangle r = new Rectangle(10, 10, 45, 45);
    15.             path.AddRectangle(r);
    16.             primitives.Add(new Shape(path, Color.Red));
    17.  
    18.  
    19.             GraphicsPath path2 = new GraphicsPath();
    20.             Rectangle r2 = new Rectangle(50, 10, 45, 45);
    21.             path2.AddRectangle(r2);
    22.             primitives.Add(new Shape(path2, Color.Green));
    23.  
    24.            
    25.             Draw();
    26.  
    27.             [COLOR=Red]ArrayList Temp = new ArrayList();
    28.             foreach (Shape s in primitives)
    29.             {
    30.                 Shape te = new Shape(s.GraphicsPath, s.Color);
    31.                 Temp.Add(te);
    32.             }
    33.             Undo.Add(Temp);[/COLOR]        }
    34.  
    35.         private void button2_Click(object sender, EventArgs e)
    36.         {
    37.             primitives.Reverse(ClickedShape, 2);
    38.             Draw();
    39.         }
    40.  
    41.         private void pane1_MouseMove(object sender, MouseEventArgs e)
    42.         {
    43.             if (CanMove == true)
    44.             {
    45.                 shape = ((Drawable)(primitives[ClickedShape]));
    46.                 shape.Move(new Point(e.X, e.Y), new Point(startx, starty));
    47.                 Draw();
    48.             }
    49.         }
    50.  
    51.         private void pane1_MouseDown(object sender, MouseEventArgs e)
    52.         {
    53.            
    54.            
    55.             foreach (Shape s in primitives)
    56.             {
    57.                 if(s.GraphicsPath.IsVisible(e.X,e.Y))
    58.                 {
    59.                     CanMove = true;
    60.                     ClickedShape = primitives.IndexOf(s);
    61.                     shape = ((Drawable)(primitives[ClickedShape]));
    62.                    
    63.                 }
    64.             }
    65.             startx = e.X - (int)shape.GraphicsPath.GetBounds().X;
    66.             starty = e.Y - (int)shape.GraphicsPath.GetBounds().Y;
    67.         }
    68.  
    69.         private void pane1_MouseUp(object sender, MouseEventArgs e)
    70.         {
    71.             CanMove = false;
    72.             [COLOR=RoyalBlue]ArrayList Temp = new ArrayList();
    73.             foreach (Shape s in primitives)
    74.             {
    75.                 Shape te = new Shape(s.GraphicsPath, s.Color);
    76.                 Temp.Add(te);
    77.             }
    78.            
    79.             Undo.Add(Temp);[/COLOR]
    80.         }
    81.  
    82.         public void Draw()
    83.         {
    84.             Bitmap bat = new Bitmap(pane1.TempImage, pane1.Width, pane1.Height);
    85.             Image d = bat;
    86.             Graphics graphics = Graphics.FromImage(d);
    87.             Drawable shapepy;
    88.             for (int i = 0; i <= primitives.Count - 1; i++)
    89.             {
    90.                 shapepy = ((Drawable)(primitives[i]));
    91.                 graphics.SmoothingMode = SmoothingMode.AntiAlias;
    92.                 graphics.FillPath(new SolidBrush(Color.FromArgb(255, shapepy.Color.R, shapepy.Color.G, shapepy.Color.B)), shapepy.GraphicsPath);
    93.                 graphics.DrawPath(new Pen(shapepy.Color, 2), shapepy.GraphicsPath);
    94.             }
    95.  
    96.             pane1.Image = d;
    97.         }
    98.  
    99.  
    100.         private Image _Image()
    101.         {
    102.             Bitmap b = new Bitmap(pane1.Width, pane1.Height);
    103.             Rectangle rect2 = new Rectangle(0, 0, pane1.Width, pane1.Height);
    104.             pane1.DrawToBitmap(b, rect2);
    105.  
    106.             return b;
    107.         }

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: Problem with array list

    The reference to the object is stored in the array list.
    What you want to do is clone (use ICloneable or define your own interface - choosing either a shallow or deep clone - http://www.codeproject.com/dotnet/Clone.asp for more information), and add the cloned object to your temporary array list, for example:

    Code:
    Temp.Add(te.Clone());

  3. #3
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: Problem with array list

    yeah axion_sa is right. You should either clone your shape. Or you should clone the instances of the values of your shape's properties. I'll give an example of what I mean;
    Code:
    //let's say you have made a shape like this:
    Graphicspath g = new GraphicsPath();
    Color c = Color.Red;
    Shape s = new Shape(g, c);
    
    //now let's make a totally independant copy of this.
    Shape s2 = s.Clone(); //axion_sa's suggestion.
    // of course this will only work if the Clone() function exists. The Clone function is specified by the ICloneable interface.
    
    //Another way to do it...
    GraphicsPath g2 = s1.GraphicsPath.Clone(); //we make a copy first.
    //here again the Clone() function will only be available if the ICloneable interface was implemented.
    Shape s3 = new Shape(g2,s1.Color);
    
    // Do you wonder why you should not clone the color just like 
    // we cloned the GraphicsPath??? That's because Color is not a
    // class, Color is a structure! Structures are value-types, not
    // reference-types. (Same goes for integers.)
    
    //Now, what if the clone function is not available and you don't want to
    // implement it. Then you can also do it like this:
    GraphicsPath g3 = new GraphicsPath();
    foreach(object o in GraphicsPath){
        g3.Add(o);
        //why not g3.Add(o.Clone()) ? because in your codelisting
        //you showed you used Rectangles. Rectangles are structures,
        // not classes ;-). No need to clone them in other words.
    }
    Shape s4 = new Shape(g3,s1.Color);
    
    //What if you don't clone anything at all. It won't work because ...
    Shape s5 = new Shape(s1.GraphicsPath, s1.Color);
    if (s5.GraphicsPath == s1.GraphicsPath){
        Console.WriteLine("This will be true, both GraphicsPath's point to the same instance.");
    }
    s1.GraphicsPath.Add(Whatever);
    //s5.GraphicsPath is the same GraphicsPath, so it will also be added there.
    Last edited by BramVandenbon; Nov 24th, 2006 at 05:37 PM.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598

    Resolved Re: Problem with array list

    Hi,

    Thanks for your help using IClonable has worked

    Thanks

    Loftty

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width