user3213352
user3213352

Reputation: 45

symbol argument for function input parameter

I am writing a c# code and I want to pass an argument in a function, which will be one of theses symbols <, >, ==, !=

private Image ThresholdImageFilter(image, symbol, threshold)
{

for (int i = 1; i < 512; i++)
{
   for (int j = 1; j < 512; j++)
    {
       for (int j = 1; j < 512; j++)
         {
          if pixel symbol threshold;
               {
                     pixel = 1;
               }
          else
               {
                     pixel = 0;
               }
         }
    }
  }

}

I want to call many times a function,but with different symbols, like this:

AImage = ThresholdImageFilter(image > threshold);
BImage = ThresholdImageFilter(image < threshold);
CImage = ThresholdImageFilter(image <= threshold);
DImage = ThresholdImageFilter(image >= threshold);

I am searching for something like this:

private Image Filter(Bitmap image, symbolArgument symbol, int threshold)
{
     ...
}

and finally call the function like this:

BinaryImage = Filter(imageB, >, threshold);

Is there a way to do it? If so, please suggest how the >= or <= will be passed (which consist of 2 symbols).

Upvotes: 1

Views: 399

Answers (1)

Servy
Servy

Reputation: 203821

Have filter accept a delegate that accepts two parameters and returns a boolean. You can then pass a lambda to that argument using the given operator:

public SomeResult Filter(Func<ArgumentType, ArgumentType, bool> predicate)
{
    if(predicate(operand1, operand2))
        DoSomething();
}

Then you would call it like:

var result = Filter((a,b)=> a > b);

Upvotes: 4

Related Questions