Question

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, fourVal, fiveVal, sixVal, sevenVal, eightVal, nineVal, tenVal As Integer and, pending listbox selection, I'd like to assign threeVal fourVal and sixVal all values of -1.

How can I do this? Thanks.

No correct solution

OTHER TIPS

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

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.

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.

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..

You can declare and asign the value using the constructor:

 Dim str1, str2, str3, str4 As New String("asdf")
 Dim int1, int2, int3, int4 As New Integer

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.)

You can try like this:

Dim aceVal, twoVal,fourVal,sevenVal, eightVal, nineVal, tenVal As Integer
Dim threeVal As Integer =-1,fiveVal=-1,sixVal=-1

alternatively follow this post: http://www.informit.com/library/content.aspx?b=Net_2003_21days&seqNum=95

you can put it in List and use For Each method here is the code

Dim aceVal, twoVal, threeVal, fourVal, fiveVal, sixVal, sevenVal, eightVal, nineVal, tenVal As Integer
Dim var As New List(Of Integer) From {threeVal, fiveVal, sixVal} 'you can add more variable here
For Each n As Integer In var
    n = -1
Next

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top