Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. switch case from array

switch case from array

Scheduled Pinned Locked Moved C#
questiondata-structureshelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gonad
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • G 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; } } .gonad

      J Offline
      J Offline
      Jon Sagara
      wrote on last edited by
      #2

      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 your for loop: either start with i = 0, or change the condition to i <= cities.Length. Otherwise, the first array element will never be evaluated.

      Jon Sagara I bent my wookie.
      My Articles

      G 1 Reply Last reply
      0
      • J Jon Sagara

        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 your for loop: either start with i = 0, or change the condition to i <= cities.Length. Otherwise, the first array element will never be evaluated.

        Jon Sagara I bent my wookie.
        My Articles

        G Offline
        G Offline
        gonad
        wrote on last edited by
        #3

        thanks for your help. this is a good fix for what i need to do. thanks for your input. :) .gonad

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups