Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Are Operators (=) defined in Classes?

Are Operators (=) defined in Classes?

Scheduled Pinned Locked Moved Visual Basic
csharpquestionlearning
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    watagal
    wrote on last edited by
    #1

    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

    J R 2 Replies Last reply
    0
    • W watagal

      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

      J Offline
      J Offline
      jo0ls
      wrote on last edited by
      #2

      It 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")

      1 Reply Last reply
      0
      • W watagal

        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

        R Offline
        R Offline
        rwestgraham
        wrote on last edited by
        #3

        There 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups