简体   繁体   English

使用多变量选择变量

[英]Select an variable using multi variables

I have a lot of Int32 variables that I wanted to select which int I want to check. 我有很多Int32变量,我想选择要检查的int Is it possible to make this line and select an variable using multi variables? 是否可以使这一行并使用多个变量选择一个变量?

Int32 redleft0 = 0; 
Int32 redleft1 = 0; 
Int32 redleft2 = 0; 
Int32 redleft3 = 0; 
Int32 redleft4 = 0; 
Int32 redleft5 = 0;
Int32 blueleft0 = 0; 
Int32 blueleft1 = 0; 
Int32 blueleft2 = 0; 
Int32 blueleft3 = 0;     
Int32 blueleft4 = 0; 
Int32 blueleft5 = 0;

redorblue = "red";    
for (int i = 0; i < count; i++)
{ 
    String checkleftint = (redorblue + "left" + i);                   
    if (checkleftint < 0)
    {
    }
}

You should use an array - or rather two - here: 您应该在此处使用一个数组-或两个数组:

var red = new int[]{0,0,0,0,0,0};
var blue = new int[]{0,0,0,0,0,0};

var arrayToUse = redorblue == "red" ? red : blue;
for (int i = 0; i < count; i++)
{
    var value = arrayToUse[i];
    // ....
}

将数组或字典与键和值一起使用,以便可以使用键提取值

It won't work that way. 这样就行不通了。 Either don't use separate variables but use an array: 要么不使用单独的变量,而是使用数组:

int[,] vars = new int[2,6] { { 0, 0, 0, 0, 0, 0 },  { 0, 0, 0, 0, 0, 0 } };
// int[0,*] would be redleft and int[1,*] would be blueleft

or use a Dictionary<string, int> 或使用Dictionary<string, int>

It's possible if you use enum , for example: 如果使用enum ,则是可能的,例如:

enum values { redleft0 = 0; redleft1 = 120; redleft2 = 13; }

By using enum 通过使用enum

So what you wolud do is just: 所以,你该做的只是:

for (int i = 0; i < count; i++)
{  
   if(i == values.redLeft0){
     ...
   }
} 

By using enum , you associate a name you want to a number you want. 通过使用enum ,可以将所需的名称与所需的数字相关联。

If this is not what you're asking for, please clarify. 如果这不是您要的内容,请进行说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM