gdi+ shape is not a reference object in Arraylist?
-
Rectangle a=new Rectangle(); Arraylist b=new Arraylist; b.add(a); a.X=1; When i debug these codes, i found b[0] is not a 's reference, they are not same, b[0].x is 0, not 1. I am fuzzy to gdi+, i need put all shapes in a Arraylist, when they are update(move or change shapes),i can re-draw all of it in a canvas. So how can i do it? :)
-
Rectangle a=new Rectangle(); Arraylist b=new Arraylist; b.add(a); a.X=1; When i debug these codes, i found b[0] is not a 's reference, they are not same, b[0].x is 0, not 1. I am fuzzy to gdi+, i need put all shapes in a Arraylist, when they are update(move or change shapes),i can re-draw all of it in a canvas. So how can i do it? :)
-
The problem is caused by the
Rectangle
being a structure (i.e. a Value type). So when you add it to theArrayList
it stores the values of the variable rather than a reference. -
Rectangle a=new Rectangle(); Arraylist b=new Arraylist; b.add(a); a.X=1; When i debug these codes, i found b[0] is not a 's reference, they are not same, b[0].x is 0, not 1. I am fuzzy to gdi+, i need put all shapes in a Arraylist, when they are update(move or change shapes),i can re-draw all of it in a canvas. So how can i do it? :)
First of all ArrayList is not recommended as the performance is not the best. See the Remarks section in ArrayList Class[^] That said, as you cannot change the values as the Rectangle is a struct and not a class, it might be better if you implement an interface and/or an abstract base class, and then a class for each shape you want to have, Rectangle, Circle, Triangle etc. (Or whatever it is you want) The specific shape classes all inherit the base class. The benefit of this approach is that you can share common properties, such as Position and Size but also implement shape specific a Paint method for each class.
public abstract class Base
{
public Base()
{
location = new Point();
}public abstract void Paint(Graphics g); public string ShapeName { get; protected set; } private Point location; public Point Location { get { return location; } set { location = value; } } private Size size; public Size Size { get { return size; } set { size = value; } } public int X { get { return location.X; } set { location.X = value; } }
}
public class Square : Base
{
public Square(Point location, Size size)
: base()
{
ShapeName = "Square";
Location = location;
Size = size;
}public override void Paint(Graphics g) { // Implement the way to draw a square }
}
Then
List shapes = new List();
shapes.Add(new Square(new Point(), new Size(30, 30)));
shapes[0].X = 10; -
Then how can i do if i want add all different shapes in a list? i need to update properties of them.
-
First of all ArrayList is not recommended as the performance is not the best. See the Remarks section in ArrayList Class[^] That said, as you cannot change the values as the Rectangle is a struct and not a class, it might be better if you implement an interface and/or an abstract base class, and then a class for each shape you want to have, Rectangle, Circle, Triangle etc. (Or whatever it is you want) The specific shape classes all inherit the base class. The benefit of this approach is that you can share common properties, such as Position and Size but also implement shape specific a Paint method for each class.
public abstract class Base
{
public Base()
{
location = new Point();
}public abstract void Paint(Graphics g); public string ShapeName { get; protected set; } private Point location; public Point Location { get { return location; } set { location = value; } } private Size size; public Size Size { get { return size; } set { size = value; } } public int X { get { return location.X; } set { location.X = value; } }
}
public class Square : Base
{
public Square(Point location, Size size)
: base()
{
ShapeName = "Square";
Location = location;
Size = size;
}public override void Paint(Graphics g) { // Implement the way to draw a square }
}
Then
List shapes = new List();
shapes.Add(new Square(new Point(), new Size(30, 30)));
shapes[0].X = 10; -
Thank you very much. I can't understand very well, you mean that the best method is i need new a rectangle class inherit from BaseShape ,use location and Size property, and no need to use rectangle struct ,is it right?
You can also use a Rectangle struct internally inside the base class. I just made a quick example to show the concept.