How to add two points together

  • Thread starter Thread starter Colin McGuire
  • Start date Start date
C

Colin McGuire

Hi again NG. Is there an easier way than I have shown to add two
points together. All I can find in the docs is a shortcut to add a
point and a size together (ie Point.op_Addition).
Thank you
Colin

Dim pt1 As New Point(10, 10)
Dim pt2 As New Point(3, 5)

//lots of other code

//Adding point2 to point1, is there a hidden operator that
//would enable me to go for example pt1.add(pt2) ?
pt1.X+=pt2.X
pt1.Y+=pt2.Y
 
No, either add "pt1.X+=pt2.X" as you have shown or
"pt1=Point.op_Addition(pt1,New Size(pt2))" as you imply.
Hexathioorthooxalate
 
Colin McGuire said:
Hi again NG. Is there an easier way than I have shown to add two
points together. All I can find in the docs is a shortcut to add a
point and a size together (ie Point.op_Addition).
Thank you
Colin

Dim pt1 As New Point(10, 10)
Dim pt2 As New Point(3, 5)

//lots of other code

//Adding point2 to point1, is there a hidden operator that
//would enable me to go for example pt1.add(pt2) ?
pt1.X+=pt2.X
pt1.Y+=pt2.Y


Dim pt1 As New Point(10, 10)

pt1.Offset(3, 5)

or, if it has to be a point:

Dim pt1 As New Point(10, 10)

pt1.Offset(pt2.x, pt2.y)
 
Colin,
As Hexathioorthooxalate pointed out, you need to use Point.op_Addition as he
demonstrated or use the code you demonstrated.
//Adding point2 to point1, is there a hidden operator that
//would enable me to go for example pt1.add(pt2) ?
Addition for Point is an overloaded operator, the method that implements
this overloaded operator is Point.op_Addition. Current versions VB.NET
(VS.NET 2002 & VS.NET 2003) do not support overloaded operators, hence the
statement Hexathioorthooxalate gave. When we get to Whidbey (VS.NET 2004)
VB.NET will support overloaded operators! Woo Hoo!!

Which means you will then the more 'natural':

' VB.NET 2004 syntax (Whidbey)
pt1 += pt2

Which converts pt2 to a Size, then adds this size to pt1, all under the
covers!

Note: Point.op_Addition is normally hidden in VS.NET, you need to use
"Tools - Options - Text Editor - Basic - General - Hide advanced members" to
show or hide advance members of classes, such as overloaded operators.

Hope this helps
Jay
 
Back
Top