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 Question

Switch Question

Scheduled Pinned Locked Moved C#
csharphelpquestion
10 Posts 5 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.
  • M Offline
    M Offline
    mcd2424
    wrote on last edited by
    #1

    Hi All, I am converting some of my vb.net code to c# and have run into a problem. Select Case True Case p.ParameterType Is GetType(String) Response.Write("paramater is a string") End Select the code above works but when I try to convert to c# I get the error: A constant value is expected. switch (true) { case pInfo.ParameterType == typeof(String): Response.Write("is type of string"); break; } Is there any way to do this in c#? thanks tom

    L C R 3 Replies Last reply
    0
    • M mcd2424

      Hi All, I am converting some of my vb.net code to c# and have run into a problem. Select Case True Case p.ParameterType Is GetType(String) Response.Write("paramater is a string") End Select the code above works but when I try to convert to c# I get the error: A constant value is expected. switch (true) { case pInfo.ParameterType == typeof(String): Response.Write("is type of string"); break; } Is there any way to do this in c#? thanks tom

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I'm not sure this has to do anything with a switch statement, but try this one: if(p.ParameterType is string) Response.Write("is type of string"); regards

      1 Reply Last reply
      0
      • M mcd2424

        Hi All, I am converting some of my vb.net code to c# and have run into a problem. Select Case True Case p.ParameterType Is GetType(String) Response.Write("paramater is a string") End Select the code above works but when I try to convert to c# I get the error: A constant value is expected. switch (true) { case pInfo.ParameterType == typeof(String): Response.Write("is type of string"); break; } Is there any way to do this in c#? thanks tom

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

        The variable goes in the switch part, the constants go in the case part.

        switch(pInfo.ParameterType)
        {
        case typeof(string):
        // Do stuff
        break;
        }

        Although I'm not sure how it handles Types. I've only ever used a switch statement for numbers, strings and enumerators.


        Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

        M 1 Reply Last reply
        0
        • C Colin Angus Mackay

          The variable goes in the switch part, the constants go in the case part.

          switch(pInfo.ParameterType)
          {
          case typeof(string):
          // Do stuff
          break;
          }

          Although I'm not sure how it handles Types. I've only ever used a switch statement for numbers, strings and enumerators.


          Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

          M Offline
          M Offline
          mcd2424
          wrote on last edited by
          #4

          Thanks for the reply. I tried that but then I get the error: A value of an integral type expected this is the only work around that I can think of, but it isnt really an ideal solution: switch (pInfo.ParameterType.Name) { case "String": Response.Write("is type of string"); break; } I cant believe you cant do this in c#! thanks

          C 1 Reply Last reply
          0
          • M mcd2424

            Thanks for the reply. I tried that but then I get the error: A value of an integral type expected this is the only work around that I can think of, but it isnt really an ideal solution: switch (pInfo.ParameterType.Name) { case "String": Response.Write("is type of string"); break; } I cant believe you cant do this in c#! thanks

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

            mcd2424 wrote:

            I cant believe you cant do this in c#!

            I would imagine that C# is enforcing some additional restrictions in order to produce optimised code - something VB is trading for ease of coding.


            Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

            1 Reply Last reply
            0
            • M mcd2424

              Hi All, I am converting some of my vb.net code to c# and have run into a problem. Select Case True Case p.ParameterType Is GetType(String) Response.Write("paramater is a string") End Select the code above works but when I try to convert to c# I get the error: A constant value is expected. switch (true) { case pInfo.ParameterType == typeof(String): Response.Write("is type of string"); break; } Is there any way to do this in c#? thanks tom

              R Offline
              R Offline
              Robert Rohde
              wrote on last edited by
              #6

              Hi, the switch statement isn't designed for such a thing. Use if-then-else-if instead:

              if (pInfo.ParameterType == typeof(String)) {
              Response.Write("is type of string");
              } else if (...) {
              } else if (...) {
              }

              Robert

              N 1 Reply Last reply
              0
              • R Robert Rohde

                Hi, the switch statement isn't designed for such a thing. Use if-then-else-if instead:

                if (pInfo.ParameterType == typeof(String)) {
                Response.Write("is type of string");
                } else if (...) {
                } else if (...) {
                }

                Robert

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #7

                Robert Rohde wrote:

                the switch statement isn't designed for such a thing.

                Please explain


                only two letters away from being an asset

                R 1 Reply Last reply
                0
                • N Not Active

                  Robert Rohde wrote:

                  the switch statement isn't designed for such a thing.

                  Please explain


                  only two letters away from being an asset

                  R Offline
                  R Offline
                  Robert Rohde
                  wrote on last edited by
                  #8

                  Hi, this simply means it can only work with values which are internally represented by integers. This applies to integer types, boolean and enumeration values. Don't ask me why MS didn't extend its capabilities - its just a fact. Robert

                  N 1 Reply Last reply
                  0
                  • R Robert Rohde

                    Hi, this simply means it can only work with values which are internally represented by integers. This applies to integer types, boolean and enumeration values. Don't ask me why MS didn't extend its capabilities - its just a fact. Robert

                    N Offline
                    N Offline
                    Not Active
                    wrote on last edited by
                    #9

                    Robert Rohde wrote:

                    can only work with values which are internally represented by integers

                    Then I must be doing something wrong for this to work :rolleyes: string MyString = "Foo"; switch(MyString) { case "Foo": break; case "Bar": break; default: break; }


                    only two letters away from being an asset

                    R 1 Reply Last reply
                    0
                    • N Not Active

                      Robert Rohde wrote:

                      can only work with values which are internally represented by integers

                      Then I must be doing something wrong for this to work :rolleyes: string MyString = "Foo"; switch(MyString) { case "Foo": break; case "Bar": break; default: break; }


                      only two letters away from being an asset

                      R Offline
                      R Offline
                      Robert Rohde
                      wrote on last edited by
                      #10

                      Hi, as usual string seems to get a special treatment :). Probably there are some more case where switch works, but the fact remains that its rather limited (compared to some other languages). Robert

                      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