How to make the + operator work while adding two Points to each other?
Asked Answered
G

3

6

Is there any way to get the + operator to work for the Point object?

Take, for example, this tiny snippet:

this.cm1.Show((MouseEventArgs)e.Location+this.i_rendered.Location);

You see, I try to add two points to eachother. It just does not work (which was expected). I'd love to get this working.

Any ideas?

Guyer answered 19/5, 2012 at 8:8 Comment(4)
What Point object are you talking about? Exactly? In what namespace? Is it your own type? What do you expect the result of addition to be?Vacation
Have you tried this.cm1.Show(((MouseEventArgs)e).Location+this.i_rendered.Location);Directed
@Vacation -> System.Drawing.PointGuyer
@Directed Does not work.Guyer
M
7

It's not going to happen the way you expect. The only overload that the Point structure provides for the + (addition) operator is one that translates the coordinates of the Point by a Size.

There's no way to add two Point structures together, and I'm not even sure what that would mean.

Don't waste too much time figuring it out, either, considering that you cannot write extension methods that overload operators.

Fortunately, in a compiled language, there's no penalty for splitting up code into multiple lines. So you can re-write your code as follows:

Point newLocation = new Point(e.Location.X + this.i_rendered.Location.X,
                              e.Location.Y + this.i_rendered.Location.Y);
this.cm1.Show(newLocation);

Alternatively, you could use the Offset method, but I'm not convinced that enhances readability.

Mahaffey answered 19/5, 2012 at 8:11 Comment(2)
Awww sad that it's not really possible. I guess I'll go that way then, it's at least best readable. - Thanks :).Guyer
Check my answer. Maybe you just need to cast the second addend to Size?Horn
H
10

I read the documentation for System.Drawing.Point (linked in Cody Gray's answer), and it has an instance method Offset. That method mutates the current Point (the designers chose to make Point a mutable struct!).

So here's an example:

var p1 = new Point(10, 20);
var p2 = new Point(6, 7);
p1.Offset(p2); // will change p1 into the sum!

In the same doc I also see an explicit conversion from Point to Size. Therefore, try this:

var p1 = new Point(10, 20);
var p2 = new Point(6, 7);
Point pTotal = p1 + (Size)p2; // your solution?
Horn answered 19/5, 2012 at 10:57 Comment(1)
The second snippet gets somewhat close, yes, however, it looks terribly confusing. I think Cody Gray's answer is easiest and the clearest solution to apply in this case.Guyer
M
7

It's not going to happen the way you expect. The only overload that the Point structure provides for the + (addition) operator is one that translates the coordinates of the Point by a Size.

There's no way to add two Point structures together, and I'm not even sure what that would mean.

Don't waste too much time figuring it out, either, considering that you cannot write extension methods that overload operators.

Fortunately, in a compiled language, there's no penalty for splitting up code into multiple lines. So you can re-write your code as follows:

Point newLocation = new Point(e.Location.X + this.i_rendered.Location.X,
                              e.Location.Y + this.i_rendered.Location.Y);
this.cm1.Show(newLocation);

Alternatively, you could use the Offset method, but I'm not convinced that enhances readability.

Mahaffey answered 19/5, 2012 at 8:11 Comment(2)
Awww sad that it's not really possible. I guess I'll go that way then, it's at least best readable. - Thanks :).Guyer
Check my answer. Maybe you just need to cast the second addend to Size?Horn
A
0

The simplest (and cleanest) solution is to just cast i_rendered.Location to a Size

Point newLocation = (e.Location + (Size)this.i_rendered.Location);

I don't see how or why anything needs to be split into convoluted X and Y components, when a simple cast does the job in the way the OP was asking for.

Afterimage answered 13/5, 2020 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.