Click here to Skip to main content
15,888,334 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have example;

C#
 if (a == "8")
    comboBox3.SelectedIndex = 0;

if (a == "9")
    comboBox3.SelectedIndex = 1;
if (a == "10")
    comboBox3.SelectedIndex = 2;

if (a == "11")
    comboBox3.SelectedIndex = 3;
if (a == "12")


What I have tried:

I want to use loop for, make this code short, everyone can help me, thanks
Posted
Updated 17-Mar-16 20:15pm
v2
Comments
Peter Leow 17-Mar-16 10:49am    
comboBox3.SelectedIndex = Int32.Parse(a) - 8;
CHill60 17-Mar-16 10:54am    
Good solution!
Hoang Thang Vo 17-Mar-16 12:36pm    
I mean not that,i think how to not rewrite many sentences if
ZurdoDev 17-Mar-16 10:54am    
If it is as simple as you have shown, then Peter has given you a one line solution. However, do you need a loop and where are you stuck?
Hoang Thang Vo 17-Mar-16 12:25pm    
I think I need one loop eliminates the need to rewrite much if statement

1 solution

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:

C#
// create the dictionary at some central place so that it
// only needs to be created once:
Dictionary<string, int> SelectedIndexMapping = new Dictionary<string, int>()
{
   { "8", 0 },
   { "9", 1 },
   // etc
};


// and then somewhere else instead of your shown code:
comboBox3.SelectedIndex = SelectedIndexMapping[a];

// or, if the string a could contain an invalid value:
int index;
if (SelectedIndexMapping.TryGetValue(a, out index))
   comboBox3.SelectedIndex = index;
else
   // show an error-message or set comboBox3.SelectedIndex to some default value 


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:
C#
int index;
if (Int32.TryParse(a, out index))
    comboBox3.SelectedIndex = index - 8;
else
    // show an error-message or set comboBox3.SelectedIndex to some default 


Edit: fixed a brain fart with TryParse(..) in the second code example
 
Share this answer
 
v3
Comments
Sascha Lefèvre 17-Mar-16 12:18pm    
Typing didn't keep up with neural input ;) Thanks, Richard.
Richard MacCutchan 17-Mar-16 12:36pm    
You should see some of the rubbish I have in front of me before I proof-read it; and sometimes after I have posted it.
Richard MacCutchan 17-Mar-16 12:37pm    
Er, should be if (Int32.TryParse(a, out index)) - it's a static method.
Sascha Lefèvre 17-Mar-16 12:41pm    
Time to call it a day I guess - thanks again :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900