7

Why aren't the assignment operators (+=, -=, *=, /=) overloadable in VB.NET?

user1351569
  • 73
  • 1
  • 3
  • @Mr.Disappointment - Yup, found that myself. Could you point out where in that article it says why the assignment operators aren't overloadable? – user1351569 Apr 23 '12 at 14:36
  • @Mr.Disappointment - Thought maybe you'd read something I'd missed. – user1351569 Apr 23 '12 at 14:44
  • Added some stuff about implicit/explicit converters that might help with whatever you may be trying to do. – Alain Apr 23 '12 at 15:00
  • 1
    @Oded LOL! Although in this case VB *is* **exactly** like C#, so nice comment but you've put it in the wrong question :) – MarkJ Apr 23 '12 at 21:46
  • 2
    I'm incredibly relieved to see this question has been closed as not constructive. For a moment there I almost learned something before the moderation mafia illuminated the folly in this inquisition. I fear to imagine the anarchy that would ensue had this not been closed. – ironsam Aug 01 '14 at 17:36

1 Answers1

11

Perhaps this is their reasoning:

Thanks for the suggestion! We don't allow you to overload the assignment operator for a type because there is currently no way to ensure that other languages or the .NET Framework itself will honor the assignment operator. The only alternative is to restrict what types that overload the assignment operator can do, but we felt that this would be too restrictive to be generally useful.

Thanks! Paul Vick Technical Lead, VB

There's something called 'Narrowing' and 'Widening' which allows you to define explicit and implicit converters from one type to another, i.e.

Dim y as MyClass1
Dim x as MyClass2 = y

But that doesn't let change the assignment operator for assigning an instance of the same class, only converting other classes.

See How to: Define a Conversion Operator

Class MyClass1
    Public Shared Widening Operator CType(ByVal p1 As MyClass1) As MyClass2

    End Operator
End Class

Same in C#

+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=

Assignment operators cannot be overloaded, but +=, for example, is evaluated using +, which can be overloaded.

=, ., ?:, ??, ->, =>, f(x), as, checked, unchecked, default, delegate, is, new, sizeof, typeof

These operators cannot be overloaded.

With the same conversion operators:

struct MyType1
{
    ...
    public static explicit operator MyType1(MyType2 src)  //explicit conversion operator
    {
        return new MyType1 { guts = src.guts };
    }
}
Alain
  • 26,663
  • 20
  • 114
  • 184
  • Ticket closed as "Won't Fix" in 2004 - rofl. Here's hoping they've since revisited the suggestion. – Alain Apr 23 '12 at 14:41
  • Does it? http://stackoverflow.com/questions/292676/is-there-a-workaround-for-overloading-the-assignment-operator-in-c – Alain Apr 23 '12 at 14:45
  • 1
    C# doesn't support operator overloads: http://msdn.microsoft.com/en-us/library/8edha89s.aspx. An ish workaround is to overload operator `+`, since += uses it, but you still can't overload `=`. – Alain Apr 23 '12 at 14:48