if statements matching multiple values
How about:
if (new[] {1, 2}.Contains(value))
It's a hack though :)
Or if you don't mind creating your own extension method, you can create the following:
public static bool In<T>(this T obj, params T[] args)
{
return args.Contains(obj);
}
And you can use it like this:
if (1.In(1, 2))
:)
Typescript If statement with multiple value matches for the same variable
Syntactically, you can use switch
instead of if
to test for multiple values using repeating cases:
switch (x) {
case 5:
case "":
case undefined:
// do something
break;
}
But this is rarely done.
To programatically test for multiple values, you can put the values in an array and check that it includes your argument value using indexOf
:
if ([5, "", undefined].indexOf(x) >= 0) {
...
}
Note that this will create the values array each time, so if this check repeats you might want to create the array once, elsewhere, and reuse it.
const values = [5, "", undefined];
// elsewhere
if (values.indexOf(x) >= 0) {
...
}
In fact, if the number of values to test is large, you might want to put them in a Set
and check that it has
them, as this is faster than testing against an array:
const values = new Set(5, "", undefined);
// elsewhere
if (values.has(x)) {
...
}
JAVASCRIPT How to equal an if value for two or multiple values?
function confirm() {
var x = document.formname.name.value;
if (x == "") {
alert("You need to fill here!");
return false;
} else if (x == "Lisa" || x == "value2" || x == "value3") {
alert("You can't enter here");
return false;
} else {
alert("Welcome");
return false;
}
}
<form name="formname" action="send.php" method="post" onsubmit="return confirm();">
Type your name : <input type="text" name="name"><br>
<input type="submit" value="Send">
</form>
Difference between if statement multiple condition and matching multiple values
if (Result.Key == Result.Value == false)
{
//Do Other Work
}
this is the same as
if ((Result.Key == Result.Value) == false)
{
//Do Other Work
}
which is the same as
if (Result.Key != Result.Value)
{
//Do Other Work
}
Unless i got confused and (Result.Key == Result.Value == false)
is actually the same as (Result.Key == (Result.Value == false))
, which in this case still is Result.Key != Result.Value
. It's confusing, so please never ever chain equality operators. It won't work the way you expect it to.
You can look up the rules in great detail here: http://msdn.microsoft.com/en-us/library/126fe14k.aspx
Test for multiple values in an if statement in C#
Try this:
if ((new[]{1, 2, 5, 13, 14}).Contains(x)) ...
Multiple value for if-statement
This has worked for me:
if (valueList.Any(x => searchx.Contains(x)))
{
}
or even shorter (thanks to rajeeshmenoth)
if(valueList.Any(searchx.Contains))
PowerShell If Statements: IF equals multiple specific Texts
Looks like you're trying to create the directories if your user chooses one of 3 text phrases and the directory doesn't already exist, and complain to your user if they choose something other than the 3 text phrases. I would treat each of those cases separately:
$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
$AcceptableText = @("Specific Text1","Specific Text2","Specific Text3")
If ($text -in $AcceptableText)
{
If (!(Test-Path $Path\$text))
{
new-item -ItemType directory -path $Path\$text
}
}
Else
{
write-host "invalid input"
}
Or you could test for the existence of the directory first like this:
$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
$AcceptableText = @("Specific Text1","Specific Text2","Specific Text3")
If (!(Test-Path $Path\$text))
{
If (($text -in $AcceptableText))
{
new-item -ItemType directory -path $Path\$text
}
Else
{
write-host "invalid input"
}
}
EDIT: Or, if you want to be tricky and avoid the Test-Path (as suggested by @tommymaynard), you can use the following code (and even eliminate the Try|Catch wrappers if you don't want error checking)
$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
$AcceptableText = @("Specific Text1","Specific Text2","Specific Text3")
If (($text -in $AcceptableText))
{
try { mkdir -path $Path\$text -ErrorAction Stop } #Change to -ErrorAction Ignore if you remove Try|Catch
catch [System.IO.IOException] { } #Do nothing if directory exists
catch { Write-Error $_ }
}
Else
{
write-host "invalid input"
}
EDIT: Also worth noting that there are many ways to Use PowerShell to Create Folders
Some nice, clean examples. Hope this helps.
Related Topics
How to Add a Blend Behavior in a Style Setter
How to Force Garbage Collector to Run
ASP.NET MVC 5 Group of Radio Buttons
How to Run a Winform from Console Application
Best Way to Remove Duplicate Entries from a Data Table
How to Get Every Nth Item from a List<T>
Requested Registry Access Is Not Allowed
Get Last 10 Lines of Very Large Text File > 10Gb
If Statements Matching Multiple Values
How to Catch Exception in Task
Read SQL Table into C# Datatable
ASP.NET Calling Webmethod with Jquery Ajax "401 (Unauthorized)"
How to Get a Property Value Based on the Name
How to Automatically Filter Out Soft Deleted Entities with Entity Framework
Is It Bad to Not Unregister Event Handlers
What Is the Use of "Ref" for Reference-Type Variables in C#
Writing Large Number of Records (Bulk Insert) to Access in .Net/C#