Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Call Functions Until One Meets Condition

0.00/5 (No votes)
19 Jun 2012CPOL 10K  
This is an alternative for Call Functions Until One Meets Condition

Introduction

This is an alternative tip to the original tip[^].

It basically wraps the original tip into one function. I prefer wrapping such functionality into a function that tells about the intent of the construct (even some purists will argue that function calls cost execution time...).

Here comes the code:

C#
public static void RunToFirstMatch<T>(Func<T, bool> sentry, params Func<T>[] functions)
{
    functions.Any(f => sentry(f()));
}

The code is called as follows (see the original functions[^]):

C#
RunToFirstMatch(v => (v >= 5), Step1, ()=>Step2(1,1), Step3, Step4, ()=>0+1);

As said, the content is the same, I simply added some wrapping.

History

  • V1.0, 2012-06-19: Initial version
  • V1.1, 2012-06-19: Fixed code

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)