Multiple Variables

后端 未结 9 2004
不知归路
不知归路 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 know this is an old thread, however I've just run into a similar issue myself - here's how I did it (I am only dealing with 4 values

    Private Sub SetFalse(ByRef first As Boolean, Optional ByRef second As Boolean = False, Optional ByRef third As Boolean = False, Optional ByRef fourth As Boolean = False)
        first = False
        second = False
        third = False
        fourth = False
    End Sub
    

    this can be easily adapted, by making the first variable the required value (this code also looks a bit daft because I have to provide a default which can only be true or false, but obviously with integers or something it looks more meaningful.)

    0 讨论(0)
  • 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
            <Extension()> _
            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
    
    0 讨论(0)
  • 2020-12-19 05:26

    If you want to keep it on one line then you can do it like this

    Dim m1 As String = "false", m2 As String = "false", m3 As String = "false" 
    'etc..
    
    0 讨论(0)
  • 2020-12-19 05:33

    You could use an array/dictionary like so:

    Dictionary myValues = new Dictionary();
    
    myValues.Add("FirstVal", 1);
    myValues.Add("SecondVal", -1);
    myValues.Add("ThirdVal", 1);
    

    You could then write a simple function:

    public updateMyValues(string[] myKeys, int myValue)
    {
         foreach (string s in myKeys)
            {
                myValues[s] = myValue;
            }
    }
    

    And finally when your list box changes you could just call the function to update the variables you want like so:

    upDateMyValues({"FirstVal", "ThirdVal"}, -1);
    

    Hope this helps.

    *Edit: I know it's in C#, but it's easily portable to VB.

    0 讨论(0)
  • 2020-12-19 05:34

    I really don't mean to be a jerk, but is there any reason why you can't just do:

    threeVal = -1
    fourVal = -1
    sixVal = -1
    
    0 讨论(0)
  • 2020-12-19 05:36

    There is no way in vb.net to assign them all on one line like threeVal=fourVal=SixVal.

    If you need all these variables then one line at a time is the way. I would suggest if -1 is going to be used a lot then create a constant for it and assign the var's to the constant.

    I would also consider using an array or collection to store all your values if possible and work with them in order for setting / retrieving their values. You then won't have to have 60 variables littered all in your code. Array and/or collection would be a little cleaner.

    0 讨论(0)
提交回复
热议问题