2

Unlike a = b = 5 in VB.NET - impossible?, I am specifically asking for an extension method to overcome this.

In C#, you can do something like this, which is very helpful:

a = b = c = 16;

All variables end up being 16. One of the answers in Why do assignment statements return a value? gives reasons why this is handy.

But in VB.Net, if you do:

Dim a, b, c As Integer
a = b = c = 16

All you get are 0's for a, b, and c.

I want to defeat this limitation by an extension method. Can this be done?

EDIT:

Here is the closest answer I personally could come up with:

  <System.Runtime.CompilerServices.Extension()>
  Public Function Assign(ByRef Operand1 As Object, ByRef Operand2 As Object) As Object
    Operand1 = Operand2
    Return Operand1
  End Function

Even though it allows you do do this,

Dim a, b, c As Integer
a.Assign(b.Assign(c.Assign(16)))

it sure is clunky and imo harder to follow, but it's the closest thing to an direct answer to my actual question I could find. I welcome any improvements.

Community
  • 1
  • 1
toddmo
  • 20,682
  • 14
  • 97
  • 107
  • 1
    [a = b = 5 in VB.NET - impossible?](http://stackoverflow.com/questions/2027193/a-b-5-in-vb-net-impossible) – γηράσκω δ' αεί πολλά διδασκόμε Oct 10 '14 at 20:56
  • @γηράσκωδ'αείπολλάδιδασκόμε, I guess that's a duplicate, maybe, but it sure is titled and worded and asked badly. Hard to find. I think mine is way more clear. but Thanks, I'll read it. Plus, I'm specifically asking for an extension. – toddmo Oct 10 '14 at 20:58
  • If you actually read the answer in the link you get what you want. – γηράσκω δ' αεί πολλά διδασκόμε Oct 10 '14 at 21:02
  • @γηράσκωδ'αείπολλάδιδασκόμε, extension methods can't redefine operators? (I don't use extension methods much, but when I do, they sure are handy) – toddmo Oct 10 '14 at 21:03
  • @toddmo: extensions can't overload operators http://stackoverflow.com/questions/172658/operator-overloading-with-c-sharp-extension-methods – Tim Schmelter Oct 10 '14 at 21:18
  • @TimSchmelter, I think you are right. However, technically, just b/c you can't do it in C# doesn't mean you can't do it in Vb.Net. they really are independent in this regard, right? – toddmo Oct 10 '14 at 21:21
  • @toddmo: extensions are more restricted in VB.NET as opposed to C#, i've asked [a question three](http://stackoverflow.com/questions/26235134/cannot-use-enumerable-count-with-list-compiler-assumes-list-count) days ago myself according an issue in VB.NET. To be honest, i don't see why this extension would be useful. Imho it would just add confusion and possible pitfalls. – Tim Schmelter Oct 10 '14 at 21:27
  • @TimSchmelter, I agree. The extension is surely not an improvement over multiple lines, but I put it there as a final answer to show what the dead end looks like, and see if anyone can improve on it. To me, the extension shown is clunky and harder to follow. – toddmo Oct 10 '14 at 21:31

1 Answers1

0

As mentioned in a = b = 5 in VB.NET = impossible? by γηράσκωδ'αείπολλάδιδασκόμε, VB uses the same operator for assignment and equality, and you can't make extension operators per Tim's comment, so the only way to do this is through an extension method like you have found.

The syntax of your extension method can be cleaner if you overload it to accept multiple arguments; I'll guess that 3 assignments is enough. Here it's written for the Integer type so you don't have to bother casting, but use Object if you prefer.

    <System.Runtime.CompilerServices.Extension> _
    Public Sub Assign(ByRef aInt As Integer, ByRef newInt As Integer)
        newInt = aInt
    End Sub

    <System.Runtime.CompilerServices.Extension> _
    Public Sub Assign(ByRef aInt As Integer, ByRef newInt1 As Integer, ByRef newInt2 As Integer)
        newInt1 = aInt
        newInt2 = aInt
    End Sub

    <System.Runtime.CompilerServices.Extension> _
    Public Sub Assign(ByRef aInt As Integer, ByRef newInt1 As Integer, ByRef newInt2 As Integer, ByRef newInt3 As Integer)
        newInt1 = aInt
        newInt2 = aInt
        newInt3 = aInt
    End Sub

Usage:

        Dim a, b, c, d As Integer

        a = 16
        a.Assign(b)
        a.Assign(c, d)
        Console.Write("b = {0}; c = {1}; d = {2}", New Object() {b, c, d})

        a = 99
        a.Assign(b, c, d)
        Console.Write("b = {0}; c = {1}; d = {2}", New Object() {b, c, d})

You could keep going and add more overloads if you like, including one with an array or list if you want N assignments.

Community
  • 1
  • 1
Sean Skelly
  • 1,229
  • 7
  • 13