what type of value does switch statement allow to use?such as switch(?){....}
-
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
-
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
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.
-
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
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) -
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
Take a look at my http://www.codeproject.com/KB/cs/TypeTransmogrifier.aspx[^]