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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Switch

Switch

Scheduled Pinned Locked Moved C#
question
6 Posts 4 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.
  • S Offline
    S Offline
    Shevchenko7
    wrote on last edited by
    #1

    How is this possible? protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { switch (e.CommandName) { case "1": HiddenField n = (HiddenField)e.Item.FindControl("IDProizvoda"); break; case "2": n = (HiddenField)e.Item.FindControl("IDProizvoda"); break; } } In Case"2" how can compiler know that n is HiddenField?

    L V 2 Replies Last reply
    0
    • S Shevchenko7

      How is this possible? protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { switch (e.CommandName) { case "1": HiddenField n = (HiddenField)e.Item.FindControl("IDProizvoda"); break; case "2": n = (HiddenField)e.Item.FindControl("IDProizvoda"); break; } } In Case"2" how can compiler know that n is HiddenField?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      HiddenField n has been declared before in the same {} block, the one that contains all the switch cases. This may be counter-intuitive, but it is how C# looks at it. :)

      Luc Pattyn

      S G 2 Replies Last reply
      0
      • L Luc Pattyn

        HiddenField n has been declared before in the same {} block, the one that contains all the switch cases. This may be counter-intuitive, but it is how C# looks at it. :)

        Luc Pattyn

        S Offline
        S Offline
        Shevchenko7
        wrote on last edited by
        #3

        Thanks! :)

        1 Reply Last reply
        0
        • L Luc Pattyn

          HiddenField n has been declared before in the same {} block, the one that contains all the switch cases. This may be counter-intuitive, but it is how C# looks at it. :)

          Luc Pattyn

          G Offline
          G Offline
          gnadeem
          wrote on last edited by
          #4

          hi, adding to what Luc Pattyn explained, here is a remark from specification "The statement-lists of a switch-block may contain declaration statements (§15.5). The scope of a local variable or constant declared in a switch block is the switch block." please note that a statement-list has obviosly to be in a sequence and reachable.

          1 Reply Last reply
          0
          • S Shevchenko7

            How is this possible? protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { switch (e.CommandName) { case "1": HiddenField n = (HiddenField)e.Item.FindControl("IDProizvoda"); break; case "2": n = (HiddenField)e.Item.FindControl("IDProizvoda"); break; } } In Case"2" how can compiler know that n is HiddenField?

            V Offline
            V Offline
            Vega02
            wrote on last edited by
            #5

            This question has already been answered, but I just wanted to add that you can use more curly braces to further delimit the scope of variables. For example: protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { switch (e.CommandName) { case "1": { HiddenField n = (HiddenField)e.Item.FindControl("IDProizvoda"); ... break; } case "2": { Hiddenfield n = (HiddenField)e.Item.FindControl("IDProizvoda"); ... break; } } } In your example it probably doesn't matter, but this certainly comes in handy when case blocks become larger and you want to keep your code partitioned. :)

            S 1 Reply Last reply
            0
            • V Vega02

              This question has already been answered, but I just wanted to add that you can use more curly braces to further delimit the scope of variables. For example: protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { switch (e.CommandName) { case "1": { HiddenField n = (HiddenField)e.Item.FindControl("IDProizvoda"); ... break; } case "2": { Hiddenfield n = (HiddenField)e.Item.FindControl("IDProizvoda"); ... break; } } } In your example it probably doesn't matter, but this certainly comes in handy when case blocks become larger and you want to keep your code partitioned. :)

              S Offline
              S Offline
              Shevchenko7
              wrote on last edited by
              #6

              Useful tip :) Thanks!

              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