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

Kamil

I have complicated problem (maybe with simple answer).

I have class, that contains some lines, points and "Markers".

Marker is a class that contains Ellipse and its center coordinates (Point).

Marker class has drag-drop implementation, that moves ellipse and changes Marker.coordinates property. That works.

However I want to use drag-drop from my Marker class to move points in SomeShape object (Marker objects are parts of SomeShape).

I thought, that when I create Marker object and I pass 'SomeShape.lineEnds[0]' to Marker constructor - update on Marker class will update also my SomeShape.lineEnds[0], but Its not working.

How can I solve this? By using some references somehow?

I hope I described my problem clearly enough.

Code:

class SomeShape
{
    // this object is set of lines and "Markers" (class below)

    private List<Marker> markers;
    private List<Point> lineEnds;
    private List<Line> lines;

    // my object can redraw itself on canvas
    public RedrawMe()
    {
        // it removes own lines from canvas and it puts new lines 
        // I call this function after I add points to lineEnds collection etc.
        // or when I change coordinates on one of lineEnds (list of points)

    }

    public void AddPoint(Point p)
    {
        this.lineEnds.Add(p); // adding point to line ends
        this.markers.Add(new Marker(p, this, c)); // adding same point to new marker

        RedrawMe();
    }

}

Problematic part:

class Marker
{
    public Canvas canvas;

    private Ellipse e;
    private Point coordinates; // ellipse center coordinates
    private Object parent; // I store SomeShape object here to call RedrawMe method on it


    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 is SomeShape) ((SomeShape)parent).RedrawMe();
    }

    #region DragDrop  // just drag drop implementation, skip this


            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
}
Kamil

Point is a struct, not class and it will not work like this.

I assume, because structs are not "reference types".

I created own "MyPoint" class with X and Y properties and it worked.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Do all P2P connections need a server to connect them to each other?

분류에서Dev

How to make 2 interfaces (of 2 different linux server) reachable to each other?

분류에서Dev

how to make amazon EC2 instances authenticate each other automatically?

분류에서Dev

how to make widgets float over each other in flutter

분류에서Dev

make object follow each other

분류에서Dev

Create two classes which use each other?

분류에서Dev

python ndb classes that reference each other

분류에서Dev

JNA Java make data available to other classes

분류에서Dev

How to place 2 spans beside each other with full width with CSS

분류에서Dev

Multiplying two xts objects with each other

분류에서Dev

C++ - How do you make Explicitly Imported DLL functions available to other classes

분류에서Dev

how in angularjs we can achieve the object update which is based on some other objects?

분류에서Dev

How do I make two ubuntu computers communicate with each other by controlling their wireless cards?

분류에서Dev

Confused on how to write tests for classes that are refactored to compose other classes

분류에서Dev

How To Apply CSS rule to Classes with Specific Keywords Inside Them?

분류에서Dev

How To Apply CSS rule to Classes with Specific Keywords Inside Them?

분류에서Dev

how is Array object different from other objects

분류에서Dev

How to extend objects without having to resort to typing them as <any>?

분류에서Dev

How to combine each subarray of subarrays and store them as comma separated strings?

분류에서Dev

How to combine 2 Java classes?

분류에서Dev

WPA2 and EAP cannot connect like other devices

분류에서Dev

How to make resolvconf append nameservers to the list instead of prepending them?

분류에서Dev

subset data only if observations of 2 variables match with each other in R

분류에서Dev

Select top 2 rows different from each other

분류에서Dev

Webdriver in Ruby, how to make coke reusable by making classes

분류에서Dev

Update top N rows with N specific for each row from other table

분류에서Dev

How to place list items that are overlapping next to each other?

분류에서Dev

How to place 3 boxes nex to each other with some text inside it?

분류에서Dev

How to place buttons in the same line, center, and not too close to each other?

Related 관련 기사

  1. 1

    Do all P2P connections need a server to connect them to each other?

  2. 2

    How to make 2 interfaces (of 2 different linux server) reachable to each other?

  3. 3

    how to make amazon EC2 instances authenticate each other automatically?

  4. 4

    how to make widgets float over each other in flutter

  5. 5

    make object follow each other

  6. 6

    Create two classes which use each other?

  7. 7

    python ndb classes that reference each other

  8. 8

    JNA Java make data available to other classes

  9. 9

    How to place 2 spans beside each other with full width with CSS

  10. 10

    Multiplying two xts objects with each other

  11. 11

    C++ - How do you make Explicitly Imported DLL functions available to other classes

  12. 12

    how in angularjs we can achieve the object update which is based on some other objects?

  13. 13

    How do I make two ubuntu computers communicate with each other by controlling their wireless cards?

  14. 14

    Confused on how to write tests for classes that are refactored to compose other classes

  15. 15

    How To Apply CSS rule to Classes with Specific Keywords Inside Them?

  16. 16

    How To Apply CSS rule to Classes with Specific Keywords Inside Them?

  17. 17

    how is Array object different from other objects

  18. 18

    How to extend objects without having to resort to typing them as <any>?

  19. 19

    How to combine each subarray of subarrays and store them as comma separated strings?

  20. 20

    How to combine 2 Java classes?

  21. 21

    WPA2 and EAP cannot connect like other devices

  22. 22

    How to make resolvconf append nameservers to the list instead of prepending them?

  23. 23

    subset data only if observations of 2 variables match with each other in R

  24. 24

    Select top 2 rows different from each other

  25. 25

    Webdriver in Ruby, how to make coke reusable by making classes

  26. 26

    Update top N rows with N specific for each row from other table

  27. 27

    How to place list items that are overlapping next to each other?

  28. 28

    How to place 3 boxes nex to each other with some text inside it?

  29. 29

    How to place buttons in the same line, center, and not too close to each other?

뜨겁다태그

보관