tags:

views:

42

answers:

3
+1  Q: 

Multiple Variables

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.

+3  A: 

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
Matthew Jones
i have about sixty different variables. that's just an example.
Ah. You should say that then. But why do you have sixty different variables?
Matthew Jones
sixty different, separate values. yep, pain in the ass. think i may just go line by line on this one.
I think you may have to. Sorry, man. That's no fun. :(
Matthew Jones
+1  A: 

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.

klabranche
+1  A: 

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.

Mr. Smith