S
Sue & Bill
I attach the following code and output. I Add an object to a
Collection. I modified a property of the object. The item in the
Collection gets modifed also. Can you help me determine where I have
gone wrong. Thanks.
public class Shape
{
protected Size _size;
protected Point _location;
public Size Size
{
get{return _size;}
set{_size=value;}
}
public Point Location
{
get{return _location;}
set{_location=value;}
}
public Shape()
{ }
}
public class Rect : Shape
{
public Rect(Point loc, Size size) : base()
{
_size = size;
_location = loc;
}
}
// For second example
public class ShapeCollection : CollectionBase
{
public ShapeCollection() : base()
{ }
public void Add(Shape s)
{ this.List.Add(s); }
public Shape this[int index]
{
get
{ return (Shape)List[index]; }
set
{ List[index]=value; }
}
}
public class RectanglesCollection : CollectionBase
{
public RectanglesCollection() : base()
{ }
public void Add(Rectangle r)
{ this.List.Add(r); }
public Rectangle this[int index]
{
get
{ return (Rectangle)List[index]; }
set
{ List[index]=value; }
}
}
public void test()
{
ShapeCollection shapes = new ShapeCollection();
Rect rect = new Rect (new Point(0,0), new Size(50,50));
shapes.Add(rect);
Debug.WriteLine("1st item: " + shapes[0].Location.ToString());
rect.Location=new Point(2,2); //Change .Location property
Debug.WriteLine("1st item again: " +
shapes[0].Location.ToString() + " Why?!");
shapes.Add(rect);
foreach (Shape sh in shapes) Debug.WriteLine
(sh.Location.ToString());
Debug.WriteLine("==============");
RectanglesCollection rectanglesCollection = new
RectanglesCollection();
Rectangle r = new Rectangle(new Point(0,0), new Size(100,50));
rectanglesCollection.Add(r);
r.Location = new Point(5,5); // change the .Location property
Debug.WriteLine("List item after r modified: " +
rectanglesCollection[0].ToString());
rectanglesCollection.Add(r);
foreach (Rectangle rx in rectanglesCollection)
Debug.WriteLine(rx.ToString());
}
Output:
-------
1st item: {X=0,Y=0}
1st item again: {X=2,Y=2} Why?!
{X=2,Y=2}
{X=2,Y=2}
==============
List item after r modified: {X=0,Y=0,Width=100,Height=50}
{X=0,Y=0,Width=100,Height=50}
{X=5,Y=5,Width=100,Height=50}
Collection. I modified a property of the object. The item in the
Collection gets modifed also. Can you help me determine where I have
gone wrong. Thanks.
public class Shape
{
protected Size _size;
protected Point _location;
public Size Size
{
get{return _size;}
set{_size=value;}
}
public Point Location
{
get{return _location;}
set{_location=value;}
}
public Shape()
{ }
}
public class Rect : Shape
{
public Rect(Point loc, Size size) : base()
{
_size = size;
_location = loc;
}
}
// For second example
public class ShapeCollection : CollectionBase
{
public ShapeCollection() : base()
{ }
public void Add(Shape s)
{ this.List.Add(s); }
public Shape this[int index]
{
get
{ return (Shape)List[index]; }
set
{ List[index]=value; }
}
}
public class RectanglesCollection : CollectionBase
{
public RectanglesCollection() : base()
{ }
public void Add(Rectangle r)
{ this.List.Add(r); }
public Rectangle this[int index]
{
get
{ return (Rectangle)List[index]; }
set
{ List[index]=value; }
}
}
public void test()
{
ShapeCollection shapes = new ShapeCollection();
Rect rect = new Rect (new Point(0,0), new Size(50,50));
shapes.Add(rect);
Debug.WriteLine("1st item: " + shapes[0].Location.ToString());
rect.Location=new Point(2,2); //Change .Location property
Debug.WriteLine("1st item again: " +
shapes[0].Location.ToString() + " Why?!");
shapes.Add(rect);
foreach (Shape sh in shapes) Debug.WriteLine
(sh.Location.ToString());
Debug.WriteLine("==============");
RectanglesCollection rectanglesCollection = new
RectanglesCollection();
Rectangle r = new Rectangle(new Point(0,0), new Size(100,50));
rectanglesCollection.Add(r);
r.Location = new Point(5,5); // change the .Location property
Debug.WriteLine("List item after r modified: " +
rectanglesCollection[0].ToString());
rectanglesCollection.Add(r);
foreach (Rectangle rx in rectanglesCollection)
Debug.WriteLine(rx.ToString());
}
Output:
-------
1st item: {X=0,Y=0}
1st item again: {X=2,Y=2} Why?!
{X=2,Y=2}
{X=2,Y=2}
==============
List item after r modified: {X=0,Y=0,Width=100,Height=50}
{X=0,Y=0,Width=100,Height=50}
{X=5,Y=5,Width=100,Height=50}