c# - How to "connect" objects in 2 classes to make them update each other? -


i have complicated problem (maybe simple answer).

i have class, contains lines, points , "markers".

marker class contains ellipse , center coordinates (point).

marker class has drag-drop implementation, moves ellipse , changes marker.coordinates property. works.

however want use drag-drop marker class move points in someshape object (marker objects parts of someshape).

i thought, when create marker object , pass 'someshape.lineends[0]' marker constructor - update on marker class update someshape.lineends[0], not working.

how can solve this? using references somehow?

i hope described problem enough.

code:

class someshape {     // object set of lines , "markers" (class below)      private list<marker> markers;     private list<point> lineends;     private list<line> lines;      // object can redraw on canvas     public redrawme()     {         // removes own lines canvas , puts new lines          // call function after add points lineends collection etc.         // or when change coordinates on 1 of lineends (list of points)      }      public void addpoint(point p)     {         this.lineends.add(p); // adding point line ends         this.markers.add(new marker(p, this, c)); // adding same point new marker          redrawme();     }  } 

problematic part:

class marker {     public canvas canvas;      private ellipse e;     private point coordinates; // ellipse center coordinates     private object parent; // store someshape object here call redrawme method on       public marker(point p, object par, canvas c)     {         this.coordinates = p;         this.canvas = c;         this.parent = par;          e = myclassfactory.ellipseformarker();          e.mousedown += new system.windows.input.mousebuttoneventhandler(e_mousedown);         e.mousemove += new system.windows.input.mouseeventhandler(e_mousemove);         e.mouseup += new system.windows.input.mousebuttoneventhandler(e_mouseup);          c.children.add(e);          e.margin = new thickness(p.x - (e.width/2), p.y -  (e.height/2), 0, 0);     }      public void moveit(point nc) // nc - new coordinates     {         this.e.margin = new thickness(nc.x - (e.width / 2), nc.y - (e.height / 2), 0, 0);         this.coordinates.x = nc.x;         this.coordinates.y = nc.y;          if (this.parent someshape) ((someshape)parent).redrawme();     }      #region dragdrop  // drag drop implementation, skip               private bool is_dragged = false;          void e_mouseup(object sender, system.windows.input.mousebuttoneventargs e){             e.handled = true;             is_dragged = false;             this.e.releasemousecapture();         }         void e_mousemove(object sender, system.windows.input.mouseeventargs e) {             if (is_dragged)             {                 this.moveit(e.getposition(canvas));             }         }         void e_mousedown(object sender, system.windows.input.mousebuttoneventargs e) {             is_dragged = true;             this.e.capturemouse();         }      #endregion // dragdrop } 

as problem, want "notify" parent, although there many ways solve classic c# manner creating event , firing when needed.

on other hand, have parent constructor. think should consider receiving interface idrawable contain methods want call parent, , through notify parent object can redrawn.

in drag n drop don't see code notifying parent, can't comment. why not activate same call ((someshape)parent).redrawme()?


Comments