Multiple Variables

后端 未结 9 2024
不知归路
不知归路 2020-12-19 04:49

Is there a way to give a value to multiple variables (integers in this case), instead of all at once?

For instance, I have Dim aceVal, twoVal, threeVal, fourVa

9条回答
  •  借酒劲吻你
    2020-12-19 05:25

    I thought the multiple assignment feature was so cool in C# that I wrote a VB extension method (Assign) to do the same thing. The semantics are pretty easy to follow; you just call Assign on any value to assign it to multiple other variables i.e.

    Call True.Assign(b1, b2, b3)
    Call 4.1.Assign(d1, d2, d3)
    

    etc...

    Here's the code:

    Imports System.Runtime.CompilerServices
    
    
    Namespace Utility
        Public Module MultiAssignExtensionMethod
            ' Multiply assign the same value to 1 (required) or more variables
             _
            Public Function Assign(Of T)(this As T, ByRef first As T, Optional ByRef second As T = Nothing, Optional ByRef third As T = Nothing,
                                        Optional ByRef forth As T = Nothing, Optional ByRef fifth As T = Nothing, Optional ByRef sixth As T = Nothing,
                                        Optional ByRef seventh As T = Nothing, Optional ByRef eighth As T = Nothing, Optional ByRef nineth As T = Nothing,
                                        Optional ByRef tenth As T = Nothing) As T
                                ' I would LIKE to do this as a ParamArray, but it doesn't allow references for basic types....grrr
                first = this
                second = this
                third = this
                forth = this
                fifth = this
                sixth = this
                seventh = this
                eighth = this
                nineth = this
                tenth = this
    
                Return this     ' For use as assignment and evaluation as Function parameter
            End Function
        End Module
    End Namespace
    

提交回复
热议问题