Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# if statement that check test condition values IN (like in t-sql does) [duplicate]

I was wondering if it is possible to use a keyword like "IN" to test a condition in an if statement in C# ?

original code :

if (fAuth.ShowDialog(this.DialogParent) == DialogResult.Cancel 
|| fAuth.ShowDialog(this.DialogParent) == DialogResult.Abort 
|| fAuth.ShowDialog(this.DialogParent) == DialogResult.None
|| fAuth.ShowDialog(this.DialogParent) == DialogResult.No)
return;

I'm asking how to do something like this:

if (fAuth.ShowDialog(this.DialogParent) IN (DialogResult.Cancel,DialogResult.Abort,DialogResult.None,DialogResult.No)
return; 

What would be the correct syntax ?

like image 576
Martin Lebel Avatar asked Dec 29 '25 13:12

Martin Lebel


1 Answers

I think the pinpoint exact answer to your question is like CodeMaster already indicated at if statements matching multiple values

That being said, since you are checking for a DialogResult, in this specific case, I doubt performance considerations come into play and we look more into code readability (as you likely won't popup 10,000 dialogs to the user).

The simplest solution is a switch statement to increase readability:

switch(fAuth.ShowDialog(this.DialogParent))
{
    case DialogResult.Cancel:
    case DialogResult.Abort:
    case DialogResult.None:
    case DialogResult.No:
        return;
    default:
        break;
}

The problem with that is that you possibly have to duplicate code if you want to check the same thing in multiple places. In that case I would suggest to wrap this into a function like that (change the name to your liking):

public class DialogEvaluator
{
    public static bool IsResultNegative(DialogResult result)
    {
        switch(fAuth.ShowDialog(this.DialogParent))
        {
            case DialogResult.Cancel:
            case DialogResult.Abort:
            case DialogResult.None:
            case DialogResult.No:
                return true;
            default:
                return false;
        }
    }
}

//And use it like that:
if(DialogEvaluator.IsResultNegative(fAuth.ShowDialog(this.DialogParent))
{
    return;
}

If you have however a bunch of different cases with different requirements, then I would go back again to the Generic In Method and wrap the possible outcomes in well named Lists:

public static class DialogEvaluator
{
    public static bool In<T>(this T obj, IEnumerable<T> args)
    {
        return args.Contains(obj);
    }

    static DialogEvaluator
    {
        NegativeResult = new List<DialogResult>() { DialogResult.Cancel, DialogResult.Abort, DialogResult.None, DialogResult.No };
        SpecificNegativeResult = new List<DialogResult>() { DialogResult.Cancel, DialogResult.Abort, DialogResult.No };
    }

    public static List<DialogResult> NegativeResult {get; private set;}
    public static List<DialogResult> SpecificNegativeResult {get; private set;}
}

//And use it like that:
if (fAuth.ShowDialog(this.DialogParent)
     .In(DialogEvaluator.NegativeResult)
{
    return;
}
//Or
if (fAuth.ShowDialog(this.DialogParent)
     .In(DialogEvaluator.SpecificNegativeResult)
{
    return;
}

It depends on your circumstances and you have to balance readability, maintainability and conciseness.

like image 130
Frank J Avatar answered Jan 01 '26 03:01

Frank J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!