First of all: Don't use
Magic numbers - Wikipedia[
^]
A for loop won't help. What you have there is a mapping: You map "8" to 0, "9" to 1 etc. And a map in C# is a dictionary. I'm not sure what you're trying to do there couldn't be done in a completely different and better way but with a dictionary it would look like this:
Dictionary<string, int> SelectedIndexMapping = new Dictionary<string, int>()
{
{ "8", 0 },
{ "9", 1 },
};
comboBox3.SelectedIndex = SelectedIndexMapping[a];
int index;
if (SelectedIndexMapping.TryGetValue(a, out index))
comboBox3.SelectedIndex = index;
else
Also, just occurred to me, if the index is always smaller by 8 than the number in
a
then instead of the dictionary you could parse the value in
a
into an integer, subtract 8 and assign the result to comboBox3.SelectedIndex:
int index;
if (Int32.TryParse(a, out index))
comboBox3.SelectedIndex = index - 8;
else
Edit: fixed a brain fart with TryParse(..) in the second code example