c# - `System.Windows.Point` struct replacement with class -


i need type similar system.windows.point, need class, not struct.

background

i tried create few points , put them on list, , change coordinates.

my code equivalent this:

list<point> listofpoints = new list<point>();  listofpoints.add(new point(1, 1)); listofpoints.add(new point(5, 5));  line line = new line();  line.brush = new solidcolorbrush(colors.black);  line.x1 = listofpoints[0].x; line.y1 = listofpoints[0].x; line.x2 = listofpoints[1].x; line.y2 = listofpoints[1].x;  canvas1.children.add(line);   // later had change these points coordinates // tried move shapes these points changing these point properties listofpoints[0].x = 50; listofpoints[0].y = 50;  // cant, (probably) because struct not reference type 

what have

i wrote simple class gives me ability change points on list without replacing them new points.

public class cpoint {      public double x;     public double y; } 

what want?

i want class act system.windows.point struct. "act like" mean, want able create rectangle cpoint this:

cpoint cp1 = new cpoint();  cp1.x = 0; cp1.y = 0;  cpoint cp2 = new cpoint();  cp2.x = 10; cp2.y = 20;  rect r = new rect(cp1, cp2); // example, in fact - use other shapes  // want able move rectangle changing cp1 , cp2 properties. 

is possible?

do need accessor? know how make accessor returns property, don't know how write accessor returns whole class.

implement implicit conversion operators:

public class cpoint {      public double x;     public double y;      public static implicit operator point(cpoint cpoint)     {         return new point(cpoint.x, cpoint.y);     }      public static implicit operator cpoint(point point)     {         return new cpoint(point.x, point.y);     } } 

this means wherever try use cpoint point required (and vice-versa), compiler insert call conversion operators. usage want it:

rect r = new rect(cp1, cp2); 

however, based on code comment, afaik, it's not possible make once create rect, if subsequently change cp1.x = 9000 have automatically propagate rect rect struct. think have consider different design/usage entirely if that's want achieve.

edit: based on requirements, possible design consider might this. first, define cpoint notify when properties changed:

public class cpoint {      public event action<cpoint, double> xchanged;     public event action<cpoint, double> ychanged;      private double _x;     public double x     {         { return _x; }         set         {             _x = value;              var xchanged = xchanged;             if (xchanged != null)                 xchanged(this, value);         }     }      private double _y;     public double y     {         { return _y; }         set         {             _y = value;              var ychanged = ychanged;             if (ychanged != null)                 ychanged(this, value);         }     }      public cpoint()     {      }      public cpoint(double x, double y)     {         this._x = x;         this._y = y;     } } 

then, because i think, intent share same point multiple lines (drawing n-sided polygon example), link lines point:

list<cpoint> listofpoints = new list<cpoint>();  listofpoints.add(new cpoint(1, 1)); listofpoints.add(new cpoint(5, 5));  line line = new line();  line.brush = new solidcolorbrush(colors.black);  listofpoints[0].xchanged += (p, x) => line.x1 = x; listofpoints[0].ychanged += (p, y) => line.y1 = y; listofpoints[1].xchanged += (p, x) => line.x2 = x; listofpoints[1].ychanged += (p, y) => line.y2 = y;  line.x1 = listofpoints[0].x; line.y1 = listofpoints[0].x; line.x2 = listofpoints[1].x; line.y2 = listofpoints[1].x;  canvas1.children.add(line); 

notice listeners xchanged , ychanged events , how associated line's x1, x2,y1,y2` properties.

presumably you'll use these points other shapes , way won't tied 1 shape type.

listofpoints[0].x = 50; //propagates line.x1 via event listofpoints[0].y = 50; //propagates line.y1 via event 

there's bit of duplication there assigning events and assigning initial values line, i'm sure bit of tweaking eliminate if necessary.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -