您可以通过使用具有相同名称的字符串来更改变量吗?

时间:2019-10-16 11:44:53

标签: c#

基本上,我试图基于匹配的字符串名称来更改变量,我想这样做是因为我不必创建多个if else语句,因为它们都在做相同的事情,但是对另一个变量

string variableName;
int value1;
int value2;

public void button1_Click(){ 
value1 +=1;
variablename= "value1"; 
}

public void button2_Click(){ 
value2 += 1;
variablename= "value2" 
}

//This is the method I would like to exist
public void delete_btn_Click() {
//This is where I would like to change a variable that matches the "variablename" string
variablename.setVariableValue -= 1;
}

//This is what I currently would do
public void delete_btn_Click() {
if(variablename == "value1"){
    value1 -= 1;
}
else if(variableName = "value2"){
    value2 = -1; 
}
}

我应该只使用字典,而不要使用变量吗?

0 个答案:

没有答案