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. Web Development
  3. ASP.NET
  4. problem with expressions in switch statement

problem with expressions in switch statement

Scheduled Pinned Locked Moved ASP.NET
helpcsharp
5 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
    sishya
    wrote on last edited by
    #1

    Hi, I want to write the exact replica of this in c# Case 100 To 199 Return "One Hundred " & NumberToText(n Mod 100) Case 200 To 999 Return "NumberToText" after trying this in c#: switch (n) { case (n==0): return ""; case (n>0 && n<20) : return "ada" } i get an error please suggest a way out

    P P 2 Replies Last reply
    0
    • S sishya

      Hi, I want to write the exact replica of this in c# Case 100 To 199 Return "One Hundred " & NumberToText(n Mod 100) Case 200 To 999 Return "NumberToText" after trying this in c#: switch (n) { case (n==0): return ""; case (n>0 && n<20) : return "ada" } i get an error please suggest a way out

      P Offline
      P Offline
      Paddy Boyd
      wrote on last edited by
      #2

      switch (n) { case (n==0): return ""; case (n>0 && n<20) : return "ada"**;** } It's not that you're just missing a semi-colon there is it...

      S 1 Reply Last reply
      0
      • P Paddy Boyd

        switch (n) { case (n==0): return ""; case (n>0 && n<20) : return "ada"**;** } It's not that you're just missing a semi-colon there is it...

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

        Hi, switch (n) { case (n==0): return ""; case (n>0 && n<20) : return "ada"; } gives the folowing error cannot convert bool to int

        G 1 Reply Last reply
        0
        • S sishya

          Hi, switch (n) { case (n==0): return ""; case (n>0 && n<20) : return "ada"; } gives the folowing error cannot convert bool to int

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

          You get that error message because you have a bool expression in the case statement. You can't have expressions in the case statement, only constants. You can only have distinct values, not ranges.

          switch (n) {
          case 0: return string.Empty;
          case 1: return "one";
          case 2: return "two"
          default: return "other";
          }

          If you want to use ranges, you have to use if statements instead. --- b { font-weight: normal; }

          1 Reply Last reply
          0
          • S sishya

            Hi, I want to write the exact replica of this in c# Case 100 To 199 Return "One Hundred " & NumberToText(n Mod 100) Case 200 To 999 Return "NumberToText" after trying this in c#: switch (n) { case (n==0): return ""; case (n>0 && n<20) : return "ada" } i get an error please suggest a way out

            P Offline
            P Offline
            patt
            wrote on last edited by
            #5

            The frustrating thing is that you will have to use if statements in C# to do this....

            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