Proton
Proton

Reputation: 93

Build an dynamic if statement?

How can i build an dynamic if statement that also includes <, >, ==, <=, >= I want to build an dynamic if statement that is not looking like this:

if (seconds < choosedSeconds)
{

}
else if (seconds > choosedSeconds)
{

}
else if(seconds >= choosedSeconds)
{

}
else if(seconds == choosedSeconds)
{

}

This is what i want it to look like

if(seconds myOperator choosedSeconds) // or minutes, hours and so on
{

}

I just want to have that in one statement. Do i have to build a struct for that?

An example would be nice.

Upvotes: 0

Views: 247

Answers (5)

Ulf Kristiansen
Ulf Kristiansen

Reputation: 1631

You could go with predicates, i.e:

public bool IsExactlyOneSecond(TimeSpan timeSpan)
{
    return timeSpan.TotalSeconds == 1.0;
}

public bool IsMoreThanOneSecond(TimeSpan timeSpan)
{
    return timeSpan.TotalSeconds > 1.0;
}

Then you probably have some method taking the predicate as input:

private void Process(TimeSpan timeSpan, Predicate<TimeSpan> test)
{
    if (test(timeSpan))
    {
        // Do something
    }
}

And you use it like this:

Process(timeSpan, IsExactlyOneSecond);

Or

Process(timeSpan, IsMoreThanOneSecond);

Upvotes: 2

Dmitry
Dmitry

Reputation: 14059

Maybe you need something like this? This generic method compares two values using the specified comparison type.

public enum ComparisonType
{
    Equal,
    Less,
    Greater,
    LessOrEqual,
    GreaterOrEqual
}

public static bool Compare<T>(T a, ComparisonType compType, T b)
    where T : IComparable<T>
{
    switch (compType)
    {
        case ComparisonType.Equal:
            return a.CompareTo(b) == 0;
        case ComparisonType.Less:
            return a.CompareTo(b) < 0;
        case ComparisonType.Greater:
            return a.CompareTo(b) > 0;
        case ComparisonType.LessOrEqual:
            return a.CompareTo(b) <= 0;
        case ComparisonType.GreaterOrEqual:
            return a.CompareTo(b) >= 0;
    }
    throw new ApplicationException();
}

Usage example:

if (Compare(seconds, ComparisonType.LessOrEqual, choosenSeconds))
{
    // seconds <= choosenSeconds here
}

Upvotes: 1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112437

You can work with delegates and lambda expressions

void MyMethod (Func<int, int, bool> comparison)
{
    int seconds = ...;
    int chosenSeconds = ...;

    if (comparison(seconds, chosenSeconds)) {
        ...
    }
}

You can call it like this

MyMethod((a, b) => a <= b);

or

MyMethod((a, b) => a == b);

Any comparison will work as long as the expression is a Boolean expression:

MyMethod((a, b) => a % b == 0);
MyMethod((a, b) => array[a] == 100 * b + 7);

Upvotes: 2

li-raz
li-raz

Reputation: 1696

Take a look at Expression Trees

Upvotes: -4

Ondrej Tucny
Ondrej Tucny

Reputation: 27974

interface IMyConditionEvaluator 
{
    bool EvaluateCondition(int x, int y); 
}

…

IMyConditionEvaluator e = new SomeSpecificConditionEvaluator();

…

if (e.EvaluateCondition(seconds, choosedSeconds))
{
    …
}

Now go ahead and create as many classes implementing IMyConditionEvaluator as you wish.

Upvotes: 2

Related Questions