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. Doubt in Switch-Case statement.

Doubt in Switch-Case statement.

Scheduled Pinned Locked Moved C#
help
8 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.
  • H Offline
    H Offline
    He is Cool
    wrote on last edited by
    #1

    Hi, I have a basic doubt in using a switch case. For the case stmt am unable to have a static or readonly value. The compiler gives a error "A constant value is expected". i.e, if i have a variable like - private static myval = "R1"; -- switch { case classname.myval: // gives compiler error } Would be much happy to know the logic behind this. Thanks.

    A C C 3 Replies Last reply
    0
    • H He is Cool

      Hi, I have a basic doubt in using a switch case. For the case stmt am unable to have a static or readonly value. The compiler gives a error "A constant value is expected". i.e, if i have a variable like - private static myval = "R1"; -- switch { case classname.myval: // gives compiler error } Would be much happy to know the logic behind this. Thanks.

      A Offline
      A Offline
      albCode
      wrote on last edited by
      #2

      He is Cool wrote:

      switch { case classname.myval: // gives compiler error }

      in switch steatement you should set any value .

      H C 2 Replies Last reply
      0
      • A albCode

        He is Cool wrote:

        switch { case classname.myval: // gives compiler error }

        in switch steatement you should set any value .

        H Offline
        H Offline
        He is Cool
        wrote on last edited by
        #3

        yup. i am giving a static value.

        1 Reply Last reply
        0
        • H He is Cool

          Hi, I have a basic doubt in using a switch case. For the case stmt am unable to have a static or readonly value. The compiler gives a error "A constant value is expected". i.e, if i have a variable like - private static myval = "R1"; -- switch { case classname.myval: // gives compiler error } Would be much happy to know the logic behind this. Thanks.

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          He is Cool wrote:

          Would be much happy to know the logic behind this

          Only constants are known at compile time. The value must be known at compile time for it to generate a valid switch statement. With a static the value can be changed at any time during the run of the program. With a readonly the value is set at runtime, although it cannot be changed once set. So, you can only provide a constant value. Either a literal value or something set as a const, just as the compiler says.


          Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

          1 Reply Last reply
          0
          • H He is Cool

            Hi, I have a basic doubt in using a switch case. For the case stmt am unable to have a static or readonly value. The compiler gives a error "A constant value is expected". i.e, if i have a variable like - private static myval = "R1"; -- switch { case classname.myval: // gives compiler error } Would be much happy to know the logic behind this. Thanks.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            switch is pretty much limited to constant values. The value needs to be known at compile time. myval can be changed in the code, so it will not always be the same.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            C 1 Reply Last reply
            0
            • A albCode

              He is Cool wrote:

              switch { case classname.myval: // gives compiler error }

              in switch steatement you should set any value .

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              :confused:


              Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

              C 1 Reply Last reply
              0
              • C Colin Angus Mackay

                :confused:


                Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                Yeah, that was definately a thinker...

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                1 Reply Last reply
                0
                • C Christian Graus

                  switch is pretty much limited to constant values. The value needs to be known at compile time. myval can be changed in the code, so it will not always be the same.

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  Damn... Beat me to it!


                  Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                  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