Are Operators (=) defined in Classes?
-
Property Pt1() As POINT Get Return Me._Pt1 End Get Set(ByVal value As POINT) If **value = Me._Pt2** Then ' TODO: Invalid Point (same as PT2) Else Me._Pt1 = value End If End Set End Property
In my property code (above) for the 1st point in my line class, the if statement produces squiggly (wavy underline) warning saying: Operator '=' is not defined for types 'POINT'. How do you define an 'operator' in a class definition? I have yet to find this concept in the book. Thanks, Karen Nooobie to OOP and VB.Net 2005 -
Property Pt1() As POINT Get Return Me._Pt1 End Get Set(ByVal value As POINT) If **value = Me._Pt2** Then ' TODO: Invalid Point (same as PT2) Else Me._Pt1 = value End If End Set End Property
In my property code (above) for the 1st point in my line class, the if statement produces squiggly (wavy underline) warning saying: Operator '=' is not defined for types 'POINT'. How do you define an 'operator' in a class definition? I have yet to find this concept in the book. Thanks, Karen Nooobie to OOP and VB.Net 2005It doesn't matter that you are in a class, the error says you can't use = to compare Points. Taking a quick look at the library for the Point type I see you can use the equals method:
Dim a As New Point(0, 10) Dim b As New Point(1, 10) Dim c As New Point(0, 10) If a.Equals(b) Then Debug.WriteLine(" a = b") Else Debug.WriteLine(" a not = b") If a.Equals(c) Then Debug.WriteLine(" a = b") Else Debug.WriteLine(" a not = b")
-
Property Pt1() As POINT Get Return Me._Pt1 End Get Set(ByVal value As POINT) If **value = Me._Pt2** Then ' TODO: Invalid Point (same as PT2) Else Me._Pt1 = value End If End Set End Property
In my property code (above) for the 1st point in my line class, the if statement produces squiggly (wavy underline) warning saying: Operator '=' is not defined for types 'POINT'. How do you define an 'operator' in a class definition? I have yet to find this concept in the book. Thanks, Karen Nooobie to OOP and VB.Net 2005There is a critical distinction you need to make: 1) Do you want to know if the two objects are really the same? In other words, are you passing in a second reference to a single instance of the object? 2) Do you want to know if the state of two different object instances are identical. In other words, you have two separate object instances, but their property values, i.e their state, is identical. In the first case there is a comparison operator - "Is". Example boolTest = m_Object1 Is m_Object2 If this returns true, you have a single object instance that is being referenced by two pointers - m_Object1 and m_Object2. In the second case, the simplest approach is to write your own validator function in the class that performs a memberwise comparison of two objects, such as Private Function IsObjectStateIdentical(ByRef MyPoint1 As POINT, ByRef MyPoint2 As POINT) As Boolean Dim fIsIdentical As Boolean With MyPoint1 fIsIdentical = (.X = MyOvbject2.X) fIsIdentical = fIsIdentical And (.Y = MyObject2.Y) End With Return fIsIdentical End Function