switch case from array
-
how can i run a switch...case from all the elements inside an array? my example gives me an error saying 'a constant value is expected'. for example...(e.Node.Text shows the selected node from a treeview)
string[] cities = new string[3] {"atlanta","akron","cincinnati"}; for(int i = 1; i < cities.Length; i++) { switch(cities[i]) { case cities[i]: MessageBox.Show(e.Node.Text); break; } }
.gonad -
how can i run a switch...case from all the elements inside an array? my example gives me an error saying 'a constant value is expected'. for example...(e.Node.Text shows the selected node from a treeview)
string[] cities = new string[3] {"atlanta","akron","cincinnati"}; for(int i = 1; i < cities.Length; i++) { switch(cities[i]) { case cities[i]: MessageBox.Show(e.Node.Text); break; } }
.gonadThe case label must be a constant. For example,
case "Los Angeles":
switch[^] switch in the C# spec[^] Looks like you just want to display the node's text if it equals one of the cities in the array. Try something like:// snip if (cities[i].ToLower () == e.Node.Text.ToLower ()) { MessageBox.Show (e.Node.Text); } // snip
That, and you also need to change yourfor
loop: either start withi = 0
, or change the condition toi <= cities.Length
. Otherwise, the first array element will never be evaluated.Jon Sagara I bent my wookie.
My Articles -
The case label must be a constant. For example,
case "Los Angeles":
switch[^] switch in the C# spec[^] Looks like you just want to display the node's text if it equals one of the cities in the array. Try something like:// snip if (cities[i].ToLower () == e.Node.Text.ToLower ()) { MessageBox.Show (e.Node.Text); } // snip
That, and you also need to change yourfor
loop: either start withi = 0
, or change the condition toi <= cities.Length
. Otherwise, the first array element will never be evaluated.Jon Sagara I bent my wookie.
My Articles