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. what type of value does switch statement allow to use?such as switch(?){....}

what type of value does switch statement allow to use?such as switch(?){....}

Scheduled Pinned Locked Moved C#
helpquestion
4 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.
  • P Offline
    P Offline
    panyl
    wrote on last edited by
    #1

    private static string ToDbfFieldTypeString(Type pType) { string sResult = ""; try { if (pType == typeof(int)) { sResult = "int"; return sResult; } if (pType == typeof(long)) { sResult = "long"; return sResult; } if (pType == typeof(float)) { sResult = "float"; return sResult; } if (pType == typeof(double)) { sResult = "double"; return sResult; } if (pType == typeof(decimal)) { sResult = "number"; return sResult; } if (pType == typeof(bool)) { sResult = "char(2)"; return sResult; } if (pType == typeof(DateTime)) { sResult = "date"; return sResult; } if (pType == typeof(string)) { sResult = "string"; return sResult; } } catch //(Exception excp) { sResult = ""; } return sResult; } ------------------------------------------------------------------- i change the if-else structure to switch structure as follow: but it tell me to input an integer value at switch(); switch (pType) { case typeof(int): return "int"; case typeof(long): return "long"; case typeof(float): return "float"; case typeof(double): return "double"; case typeof(decimal): return "number"; case typeof(bool): return "char(2)"; case typeof(DateTime): return "date"; case typeof(string): return "string"; } So,buddy,could you help me modify the c

    C D P 3 Replies Last reply
    0
    • P panyl

      private static string ToDbfFieldTypeString(Type pType) { string sResult = ""; try { if (pType == typeof(int)) { sResult = "int"; return sResult; } if (pType == typeof(long)) { sResult = "long"; return sResult; } if (pType == typeof(float)) { sResult = "float"; return sResult; } if (pType == typeof(double)) { sResult = "double"; return sResult; } if (pType == typeof(decimal)) { sResult = "number"; return sResult; } if (pType == typeof(bool)) { sResult = "char(2)"; return sResult; } if (pType == typeof(DateTime)) { sResult = "date"; return sResult; } if (pType == typeof(string)) { sResult = "string"; return sResult; } } catch //(Exception excp) { sResult = ""; } return sResult; } ------------------------------------------------------------------- i change the if-else structure to switch structure as follow: but it tell me to input an integer value at switch(); switch (pType) { case typeof(int): return "int"; case typeof(long): return "long"; case typeof(float): return "float"; case typeof(double): return "double"; case typeof(decimal): return "number"; case typeof(bool): return "char(2)"; case typeof(DateTime): return "date"; case typeof(string): return "string"; } So,buddy,could you help me modify the c

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

      panyl wrote:

      but it tell me to input an integer value at switch();

      No, it tells you you need an integral type. I googled your error message and found this: The C# compiler informs us that a switch statement can only take certain integral types. To be more specific, it can take an sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or an enum-type. C# realizes that the switch has received a yyy object and since it cannot convert a yyy into the above integral types, it reports an error.

      Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.

      1 Reply Last reply
      0
      • P panyl

        private static string ToDbfFieldTypeString(Type pType) { string sResult = ""; try { if (pType == typeof(int)) { sResult = "int"; return sResult; } if (pType == typeof(long)) { sResult = "long"; return sResult; } if (pType == typeof(float)) { sResult = "float"; return sResult; } if (pType == typeof(double)) { sResult = "double"; return sResult; } if (pType == typeof(decimal)) { sResult = "number"; return sResult; } if (pType == typeof(bool)) { sResult = "char(2)"; return sResult; } if (pType == typeof(DateTime)) { sResult = "date"; return sResult; } if (pType == typeof(string)) { sResult = "string"; return sResult; } } catch //(Exception excp) { sResult = ""; } return sResult; } ------------------------------------------------------------------- i change the if-else structure to switch structure as follow: but it tell me to input an integer value at switch(); switch (pType) { case typeof(int): return "int"; case typeof(long): return "long"; case typeof(float): return "float"; case typeof(double): return "double"; case typeof(decimal): return "number"; case typeof(bool): return "char(2)"; case typeof(DateTime): return "date"; case typeof(string): return "string"; } So,buddy,could you help me modify the c

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        A switch statement can take an enum because: "Every enumeration type has an underlying type, which can be any integral type except char." source MSDN[^] In your situation this helps because you can use the GetTypeCode() method which returns an enum member.

        deciaml d = 0;
        switch (d.GetTypeCode())
        {
        case TypeCode.Decimal:
        Console.WriteLine("It's a deciaml");
        break;
        case TypeCode.Int32:
        Console.WriteLine("It's an int");
        break;
        default:
        Console.WriteLine("WTF?");
        break;
        }

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        1 Reply Last reply
        0
        • P panyl

          private static string ToDbfFieldTypeString(Type pType) { string sResult = ""; try { if (pType == typeof(int)) { sResult = "int"; return sResult; } if (pType == typeof(long)) { sResult = "long"; return sResult; } if (pType == typeof(float)) { sResult = "float"; return sResult; } if (pType == typeof(double)) { sResult = "double"; return sResult; } if (pType == typeof(decimal)) { sResult = "number"; return sResult; } if (pType == typeof(bool)) { sResult = "char(2)"; return sResult; } if (pType == typeof(DateTime)) { sResult = "date"; return sResult; } if (pType == typeof(string)) { sResult = "string"; return sResult; } } catch //(Exception excp) { sResult = ""; } return sResult; } ------------------------------------------------------------------- i change the if-else structure to switch structure as follow: but it tell me to input an integer value at switch(); switch (pType) { case typeof(int): return "int"; case typeof(long): return "long"; case typeof(float): return "float"; case typeof(double): return "double"; case typeof(decimal): return "number"; case typeof(bool): return "char(2)"; case typeof(DateTime): return "date"; case typeof(string): return "string"; } So,buddy,could you help me modify the c

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Take a look at my http://www.codeproject.com/KB/cs/TypeTransmogrifier.aspx[^]

          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