| re: multiple elements conditional on if
gordon wrote:[color=blue]
> I am trying to set some elements to values depending on the selection in a
> test box. The first possible value that can be selected should set
> fmttest1= "", fmttest2=" to " and fmttest3="". The second value that can be
> selected from the test box should set the fmttest1 ="From ", fmttest2=" to
> ", fmttest3=" less than".
>
> here is what i have so far but i cannot get an if statement to apply the
> condition to more than one item.
>
> private void lbSelect_SelectedIndexChanged(object sender, System.EventArgs
> e)
> {
> if (lbSelect.SelectedIndex=0)
> (fmttext1="" fmttext2=" to " fmttest3="");
> else if ((lbSelect.SelectedIndex=1)
> (fmttext1="From " fmttext2=" to " fmttext3=" less than ");
> }
> } }
>
> I know this syntax is wrong - but what is the correct way to use many
> elements in an if statement - should i use a select or case statement?[/color]
Well, you certainly *could* do it with a switch:
switch (lbSelect.SelectedIndex)
{
case 0:
fmttext1="";
fmttext2=" to ";
fmttext3="";
break;
case 1:
fmttext1="From ";
fmttext2=" to ";
fmttext3=" less than ";
break;
}
Personally I'd probably go with if/else until I had more than two
cases, at which point I'd go to the switch statement - or have:
static readonly string[,] TextStrings = { {"", " to ", ""}, {"From ", "
to ", " less than "} };
and do it in one hit so long as the value of SelectedIndex is in the
appropriate range. That would involve less code duplication.
Jon |