Multiple Variables

后端 未结 9 2051
不知归路
不知归路 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: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.

提交回复
热议问题