This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Multiple Cases in Switch:
Is it possible to do a multiple constant-expression switch statement like
switch (i) {
case "run","notrun", "runfaster": //Something like this.
DoRun();
break;
case "save":
DoSave();
break;
default:
InvalidCommand(command);
break;
}
Yes, it is. You can use multiple case labels for the same section:
switch (i)
{
case "run":
case "notrun":
case "runfaster":
DoRun();
break;
case "save":
DoSave();
break;
default:
InvalidCommand(command);
break;
}
Related
This question already has answers here:
Value is in enum list
(7 answers)
Closed 5 years ago.
Here is the sample, CommandType is an enum. It's a none flag enum.
CommandType cmdType = CommandType.back;
if (cmdType == CommandType.back || cmdType == CommandType.forward || cmdType == CommandType.previous || cmdType == CommandType.home)
{
//do something
}
Is there any way to simplify the "if" statement?
How about a switch ?
switch(cmdType)
{
case CommandType.back:
case CommandType.forward:
case CommandType.previous:
case CommandType.home:
// do something
break;
}
You can always use a switch statement. It performs better and can be more readable:
switch (cmdType)
{
case CommandType.back:
case CommandType.forward:
case CommandType.previous:
case CommandType.home:
//do somehting
break;
default:
//do something else
break;
}
You can use a switch statement
CommandType cmdType = CommandType.home;
switch(cmdType)
{
case CommandType.back:
case CommandType.forward:
case CommandType.previous:
case CommandType.home:
// do somethinig
break;
}
It says I cannot use an OR on two strings in a switch case (Dental || Vision). Would placing each option in its own separate case work or am I messing up the syntax? Much thanks
switch (GR.planTypeFocus){
case "Medical":
CreatePlanForMedical();
break;
case "Dental" || "Vision":
//createPlanForDental_Vision
break;
case "LTD" || "Life":
//createPlanForLTD_Life
break;
}
}
Technically you achieve 'OR' by using fall through
switch (GR.planTypeFocus){
case "Medical":
CreatePlanForMedical();
break;
case "Dental":
case "Vision":
//createPlanForDental_Vision
break;
case "LTD":
case "Life":
//createPlanForLTD_Life
break;
}
Of course you can use an "or", based on the context of your example:
switch (GR.planTypeFocus){
case "Medical":
CreatePlanForMedical();
break;
case "Dental":
case "Vision": //<-- here's how you do it
//createPlanForDental_Vision
break;
case "LTD":
case "Life": //<-- this is how you do it
//createPlanForLTD_Life
break;
}
This question already has answers here:
Multi-variable switch statement in C#
(13 answers)
Closed 8 years ago.
Is it possible to return result from multiple switch statement?
For example i would like to use employee.DepartmentID and employee.StatusID for my case. But how do i include employee.StatusID in this statement? Using and/or operators?
switch (employee.DepartmentID)
{
case 1:
EMAIL = "abc#gmail.com";
break;
case 2:
EMAIL = "abcd#gmail.com";
break;
}
What you really need is this.
Use the Switch to determine which department is involved
Switch (DepartmentID)
{
case 1:
Email = classHR.GetEmailAddress(Status);
break;
case 2:
Email = classMarketing.GetEmailAddress(Status);
break;
}
Use Static Classes for the different departments (using an interface preferably).
This will give you a better run down than what you are thinking of here.
This question already has answers here:
Is there a better alternative than this to 'switch on type'?
(31 answers)
C# switch on type [duplicate]
(5 answers)
Closed 9 years ago.
I understand that C# compiler as it stands does not let switching on Type like
switch (typeof(MyObj))
case Type1:
case Type2:
case Type3:
There are solutions where a Dictionary of Type and Action could be used or a static class (link), but I am just curious, why is this a bad practice or not implemented in the compiler yet ?
Thank you in advance.
If you're talking about primitive types you can do switch over TypeCode.
var typeCode = Type.GetTypeCode(type);
switch (typeCode)
{
case TypeCode.Empty:
break;
case TypeCode.Object:
break;
case TypeCode.DBNull:
break;
case TypeCode.Boolean:
break;
case TypeCode.Char:
break;
case TypeCode.SByte:
break;
case TypeCode.Byte:
break;
case TypeCode.Int16:
break;
case TypeCode.UInt16:
break;
case TypeCode.Int32:
break;
case TypeCode.UInt32:
break;
case TypeCode.Int64:
break;
case TypeCode.UInt64:
break;
case TypeCode.Single:
break;
case TypeCode.Double:
break;
case TypeCode.Decimal:
break;
case TypeCode.DateTime:
break;
case TypeCode.String:
break;
}
BTW answer for your question you may need to read How many Microsoft employees does it take to change a lightbulb?
I would refer you to the accepted answer for : switch statement in C# and "a constant value is expected" . The compiler needs to know at compile time that there won't be any duplicates, so it accepts only constants. By the way, you can achieve the same effect with
switch (typeof(MyObj).FullName
and use the names of each type as case conditions, like:
case "MyNamespace.Type1":
/*stuff*/
break;
case "MyNamespace.Type2":
/*other stuff*/
break;
default:
/*default stuff*/
When would you use switching on type? Most of the cases I can think of are better solved with inheritance, i.e. rather than doing:
switch (typeof(MyObj)) {
case Type1: doSomethingForType1; break;
case Type2: doSomethingForType2; break;
case Type3: doSomethingForType3; break;
You would set it up in a much more object-oriented manner:
Interface ISpecialType {
void doSomething();
}
Type1 : ISpecialType {
doSomething() {}
}
Type2 : ISpecialType {
doSomething() {}
}
Type3 : ISpecialType {
doSomething() {}
}
then, no matter what, you just call MyObj.doSomething(); It's a little bit more typing at the start, but a lot more robust.
Also, if it's really important to you to switch on, you can always use typeof(MyObj).toString() and switch on that. It's not recommended practice, since you're then hard-coding strings that are allowed to change into your switch, but you can do it.
This question already has answers here:
How to make C# Switch Statement use IgnoreCase
(12 answers)
Closed 9 years ago.
C#'s switch() statement is case-sensitive. Is there a way to toggle it so it becomes case-insensitive?
==============================
Thanks,
But , I don't like these solutions;
Because case conditions will be a variable , and I don't know if they ALL are UPPER or lower.
Yes - use ToLower() or ToLowerInvariant() on its operands. For example:
switch(month.ToLower()) {
case "jan":
case "january": // These all have to be in lowercase
// Do something
break;
}
You can do something like this
switch(yourStringVariable.ToUpper()){
case "YOUR_CASE_COND_1":
// Do your Case1
break;
case "YOUR_CASE_COND_2":
// Do your Case 2
break;
default:
}
Convert your switch string to lower or upper case beforehand
switch("KEK".ToLower())
{
case "kek":
CW("hit!");
break;
}