Click here to Skip to main content
15,889,877 members
Articles / Programming Languages / Visual Basic
Alternative
Tip/Trick

Call Functions Until One Meets Condition

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
2 Apr 2011CPOL1 min read 7.8K   2   2
VB.NET Version (C# Version)Thanks to cechode for inspiring this tip/trick. Suppose you have the following functions:Function Step1() As Boolean Return TrueEnd FunctionFunction Step2(ByVal val1 As Integer, ByVal val2 As Integer) As Boolean Return val1 = val2End...

VB.NET Version (C# Version)


Thanks to cechode for inspiring this tip/trick. Suppose you have the following functions:
VB.NET
Function Step1() As Boolean
    Return True
End Function
Function Step2(ByVal val1 As Integer, ByVal val2 As Integer) As Boolean
    Return val1 = val2
End Function
Function Step3() As Boolean
    MessageBox.Show("I will be reached.")
    Return False
End Function
Function Step4() As Boolean
    Throw New Exception("This should be impossible!")
End Function


If you want to execute each of those in sequence until one returns false, this is done fairly easily in VB.NET:
VB.NET
Select Case False
    Case Step1()
    Case Step2(1, 1)
    Case Step3()
    Case Step4()
    ' Note that you need not call a function.
    Case 0 = 1
End Select


While that works fine for comparing the results against a value, it does not allow you to process each result using an arbitrary condition, such as checking if the value is greater than or equal to 5. However, doing so is possible using the Any() function (also note that the return type of each function was changed to an integer rather than a boolean):
VB.NET
Dim steps() As Func(Of Integer) =
{
    AddressOf Step1,
    ' You can use a lambda to wrap a function with a different signature.
    Function() Step2(1, 1),
    AddressOf Step3,
    AddressOf Step4,
    ' You can use a lambda to avoid the use of a function.
    Function() 0 + 1
}

' We test against a condition rather than a value.
steps.Any(Function([step]) [step]() >= 5)


The condition >= 5 is a very short one, so this won't save you any typing over the short-circuiting technique shown below. However, it would save you some typing for longer conditions. Specifying the condition only once rather than for each value also reduces the probability that you will make a mistake when typing. And if you change the condition later on, you only have to change the condition in one place rather than many. Here is the short-circuiting approach, which leads to duplicated code:
VB.NET
' Don't do this.
If Step1() >= 5 OrElse
    Step2(1, 1) >= 5 OrElse
    Step3() >= 5 OrElse
    Step4() >= 5 OrElse
    0 + 1 >= 5 Then
End If


While shorter for this example (due to the short condition), this code also has the maintenance problems and higher probability of human error explained above.

License

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


Written By
Web Developer
United States United States

  • Managing Your JavaScript Library in ASP.NET (if you work with ASP.net and you don't read that, you are dead to me).
  • Graduated summa cum laude with a BS in Computer Science.
  • Wrote some articles and some tips.
  • DDR ("New high score? What does that mean? Did I break it?"), ping pong, and volleyball enthusiast.
  • Software I have donated to (you should too):

Comments and Discussions

 
GeneralNice tip! Pin
Sander Rossel11-Jul-12 11:09
professionalSander Rossel11-Jul-12 11:09 
GeneralReason for my vote of 5 I Like VB.NET Pin
tratak29-Apr-11 11:21
professionaltratak29-Apr-11 11:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.