vendredi 15 mai 2015

Concatenate variable in variable name

I have a list of variable in ASP.NET (C#) that I'm trying to loop through without writing dozens of If statements. What I was hoping to do is just reference each of them with a variable and append it to the variable name. So something like the following:

int x = 1;

while (x < 20)
{
    if ("_id&" + x == "True")
    {
        _id + x = x.ToString();
    }
    else
    {
        _id + x = 0;
    }
    x++;
}

The logic behind what I'm trying to do is I have several checkboxes on a form, Id1CheckBox, Id2CheckBox, Id3CheckBox, etc. I need get these values as strings so I can pass them as parameters in a SQL query. So basically If Id1CheckBox = true then _id1 = 1. If Id2CheckBox = true then _id2 = 2. If any are false then I would want something like If Id3CheckBox = false then _id3 = 0. Is this possible or will I just need to write several If statements instead of using a loop?

Aucun commentaire:

Enregistrer un commentaire