c# If else statement with case insensative [duplicate] - c#

This question already has answers here:
Case insensitive 'Contains(string)'
(29 answers)
Closed 9 years ago.
I have this if else statement which is assigned to compare results from a text box to a list of context. I am wondering how do i make it such that it is case insensitive ?
value = textbox1.Text;
if (article.contains(value))
{
label = qwerty;
}
else
{
break;
{

try this
if(article.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
{
// ....
}
else
{
// .....
}

Related

Use of switch with logic operators on C# [duplicate]

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;

Scope of locals in switch statement [duplicate]

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

c# - StartsWith, EndsWith and between them [duplicate]

This question already has answers here:
How can I get a character in a string by index?
(2 answers)
Closed 5 years ago.
string qwe = "ABCD";
if(qwe.StartsWith("A") && qwe.EndsWith("D"))
{
MessageBox.Show("Message");
}
What I need is to make also a decision for B and C but not StartsWith and EndsWith, it's really hard for me to explain but just like this:
if(qwe.Second("B"))
{
//Do anything
}
and
if(qwe.Third("C"))
{
//Do anything
}
You know that you can access characters via index(zero based)?
if(qwe.Length >= 2 && qwe[1] == 'B')
{
//Do anything
}
if(qwe.Length >= 3 && qwe[2] == 'C')
{
//Do anything
}

C#: non-flag enum, is there any easier way for checking enum to match one of multiple values? [duplicate]

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;
}

Multiple variables in switch-case statement [duplicate]

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.

Categories

Resources