Results 1 to 9 of 9

Thread: Overloading = operator with additional parameters

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    16

    Overloading = operator with additional parameters

    Hello,

    I want to have an = operator for one of my class. The equality can be defined in multiple ways. For this, I need to pass multiple optional parameters to the operator procedure.

    But it seems that the = operator procedure can have exactly two parameters, no more no less.

    Is there any way to overcome this restriction?

    I want avoid writing a custom procedure so that I can use the = operator.

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Overloading = operator with additional parameters

    I don't see how you could do that. What would the consumption of it look like? is x =(someParm) y ?

    You can overload equality based on the types being compared but beyond that it just doesn't make sense as a concept.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    16

    Re: Overloading = operator with additional parameters

    Suppose I have a class C1 that has 4 properties p1, p2, p3, p4.

    For equality of 2 objects it is essential that values of p1 & p2 should match for both objects.
    This is, however, may not be sufficient in some cases. Equality needs that p3 also should match.
    And in some other case p4 should also match. So, this was what I had in my mind:

    Public Shared Operator =(Co1 As C1, Co2 As C1, checkP3 as boolean, checkp4 as boolean) As Boolean

    if (Co1.p1 = Co2.p1) and (Co1.p2 = Co2.p2) then

    'TRUE

    ENDIF

    IF (TRUE) AND (checkP3 = TRUE) THEN

    'CHECK P3

    END IF

    IF (TRUE) AND (checkP4 = TRUE) THEN

    'CHECK P3

    END IF

    End Operator

  4. #4
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Overloading = operator with additional parameters

    Yes, but how would you consume it?

    When you write:-
    VB.Net Code:
    1. If A = B Then
    ...what will that mean?

    A slightly pedantic point but pertinent, there can only ever be one form of equality between two things. There can be multiple forms of equivalence. I suspect you're actually talking about the latter in which case the = operator isn't really appropriate (we often use it to mean equivalent in programming but we really shouldn't). My suggestion would be to implement an IsEquivalentTo method on your class or a shared AreEquivalent method, depending on how you'd like to consume it. Then you will have either:-
    VB.Net Code:
    1. If A.IsEquivalentTo(B) Then
    or
    VB.Net Code:
    1. If AreEquivalent(A, B) Then
    Personally I think the first form is better but it's subjective.

    As far as how to tell it which comparison to use, you would either implement different methods or you would pass in Boolean parameters. Again, I prefer the first approach because I think it will offer more readable code.
    VB.Net Code:
    1. If A IsEquivalentCheckingP3To(B) Then
    ..is more readable to me than
    VB.Net Code:
    1. If A.IsEquivalentTo(B, True, False) Then
    Last edited by FunkyDexter; Apr 18th, 2018 at 07:25 AM.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Overloading = operator with additional parameters

    The = operator is binary. You can't pass anything but two arguments to it, because the compiler expects it to be binary. You can't legally call it, because the compiler uses it when you use syntax that performs an equality check. When you write "a = b", first the semantic analysis decides if you're aiming for equality or assignment. If it decides you want equality, it calls Operator=(a, b). This is cooked into the compiler, you can't change it.

    If you want more complex equality, it's best to write your own methods. For example, the String class has a Compare() method that lets you define culture information, case-sensitivity, etc. It's possible maybe you could name your method Operator= (I don't think it is), but you can't make the compiler use it because it's expecting a binary operator.

    Being clever with the '=' operator is the fastest way to confuse yourself.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Overloading = operator with additional parameters

    And to confuse other people. We've been taught since our earliest days what the = sign means. For custom classes, the concept of equality can certainly change, but when somebody writes = then we expect it to mean that the item on the left equals the item on the right, or "set the item on the left to the value on the right." If you start playing around with the meaning of equality, it will confuse anybody else who looks at the code.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    16

    Re: Overloading = operator with additional parameters

    Ok. All this means I have to write my own function. I already did. I just wanted to check if there is a way to overcome the 2-parameter restriction.
    Thanks.

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Overloading = operator with additional parameters

    If you are talking about determining if one instance of a class is equal to another instance of a class then it is up to you to define that relationship. Based on what you have said, and what you haven't, this illustrates the concept.

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim aFoo As New Foo
            Dim bFoo As New Foo
    
            If aFoo = bFoo Then
                Stop
            End If
    
            aFoo.p3MatchReq = True
            aFoo.p3 = "Nope"
    
    
            If aFoo = bFoo Then
                Stop
            End If
    
        End Sub
    
        Public Class Foo
    
            Public p1 As String
            Public p2 As String
            Public p3 As String
            Public p4 As String
            Public p3MatchReq As Boolean = False
            Public p4MatchReq As Boolean = False
    
            Public Shared Operator =(firstFoo As Foo, secondFoo As Foo) As Boolean
                Dim rv As Boolean = False
                If firstFoo.p1 = secondFoo.p1 Then
                    rv = True 'p1 match
                    If firstFoo.p3MatchReq OrElse secondFoo.p3MatchReq Then
                        If firstFoo.p3 <> secondFoo.p3 Then
                            rv = False
                        End If
                    End If
                    If firstFoo.p4MatchReq OrElse secondFoo.p4MatchReq Then
                        If firstFoo.p4 <> secondFoo.p4 Then
                            rv = False
                        End If
                    End If
                End If
                Return rv
            End Operator
    
            Public Shared Operator <>(firstFoo As Foo, secondFoo As Foo) As Boolean
    
            End Operator
    
        End Class
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Overloading = operator with additional parameters

    I just wanted to check if there is a way to overcome the 2-parameter restriction.
    Afraid not. You can override the = operator as DB has shown but you can only provide one definition for equality.

    If you start playing around with the meaning of equality...
    Yeah, that way lies madness. Like giving women the vote. Next thing you know they'll start having "opinions". Where would it end?
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width