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.
Related
This question already has answers here:
'||' operators to be used between two enums in a switch statement
(1 answer)
How to use && operator in switch statement based on a combined return a value?
(5 answers)
c# - case statement for logical operators
(2 answers)
Closed 1 year ago.
I'm C# beginner. Why appear the error (Compiler Error CS0152, A label was repeated in a switch statement. The switch statement contains multiple cases with the value of tag '6') in "case 6:" line, when I write this code:
namespace ConsoleApp1
{
class Class1
{
public int abc { get; set; }
public Class1()
{
}
public void mTest(int aVal)
{
switch(aVal)
{
case (2 | 4):
{
break;
}
case 5:
{
break;
}
case 6:
{
break;
}
}
}
}
}
You're almost there, but the correct way to pattern match that condition is:
case 2 or 4:
break;
This question already has answers here:
Variable declaration in a C# switch statement [duplicate]
(7 answers)
Case Statement Block Level Declaration Space in C#
(6 answers)
Closed 3 years ago.
I was surprised to discover that locals in switch statements are scoped to the switch statement and not to the case. Why is that? And is it good practice to add curly braces around each case as a workaround.
This won't compile:
switch (n)
{
case 1:
var i = 1;
Console.WriteLine(i);
break;
default:
var i = 2;
Console.WriteLine(i);
break;
}
This does compile:
switch (n)
{
case 1:
{
var i = 1;
Console.WriteLine(i);
break;
}
default:
{
var i = 2;
Console.WriteLine(i);
break;
}
}
This is a design choice specifically made by C#.
A variable only exists inside the innermost braces in which the variable is first declared. This makes it easy to check the variables scope just by looking, and I might guess it also makes parsing easier
This question already has answers here:
Is there a C# case insensitive equals operator?
(14 answers)
Closed 5 years ago.
How to compare a 2 string in different case
like
String a="Pawan";
String b="PAWAN";
how to compare using inbuilt method
I am try to compare culture method but can not be compere so please provide a solution
return result equal or not equal
Transform both to lowercase strings and compare after that
a.ToLower() == b.ToLower()
Every answer so far was checking for equality, here is mine actually comparing:
switch(string.Compare(a, b, StringComparison.CurrentCultureIgnoreCase))
{
case 1: Console.WriteLine("a is greater"); break;
case 0: Console.WriteLine("a and b are equal"); break;
case -1: Console.WriteLine("b is greater"); break;
}
You can test it here https://dotnetfiddle.net/h75xZ8
If you arent sure about the comparison, you can look it up here:
https://msdn.microsoft.com/en-us/library/system.stringcomparison(v=vs.110).aspx
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;
}
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;
}