0

Is there any way to get the parameters of a function dynamic? Like:

Function(text As String, If text = "yes" text2 As String)

End Function

So the second one just gets asked, when the first is filled or a certain value? Optional is no way, cause for example the third needs to be filled when the second one is already.

heyjonny
  • 9
  • 6
  • The text value is "checked" on run time, hence there is no way to include condition as a parameter. however, you can use overloaded methods. – Jonathan Applebaum Sep 09 '20 at 14:55
  • It might be easier to answer your question if you gave examples of function calls you want to support and what you want the result to be. – Darryl Sep 12 '20 at 00:11

2 Answers2

1

No, you can't do that. But you might accomplish something similar via Currying.

Currying takes a function that needs multiple arguments, and converts into a set of functions that each need a single argument, where prior functions in the set return the next function in the set.

Where this will still be problematic for your situation is .Net likes strongly-typed delegates. If the initial function needs to return a method with one argument for any case, it must do that for all cases.

However, that doesn't mean it returns the same function. You can still vary which function is returned based on the initial input argument.

So instead of this:

Function test(text As String, a As String) As String
    If text = "yes" Then
        Return text & a
    Else
        Return a & text
    End If
End Function

test("yes", "foo") ' produces "yesfoo"
test("no", "foo")  ' produces "foono"

We have this:

Function test(text As String) As Func(Of String, String)
    If text = "Yes" Then
        Return Function(a) text & a
    Else
        Return Function(a) a & text
    End If
End Function

test("yes")("foo") ' produces "yesfoo"
test("no")("foo")  ' produces "foono"

This won't do exactly what you want; it still requires the additional argument or not in every situation. But it might give you enough new flexibility to accomplish your ultimate goal. For example, perhaps you could combine this with a closure, where you never use the additional argument, but in the case where you need it the additional value is still available. Exactly how this will look or whether it's really possible or helpful will depend on the details of what you're trying to accomplish beyond the simple test code in the question.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

You could handle what to pass in the button code.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim paramValue As String
    If MyTestText = "yes" Then
        paramValue = TextBox2.Text
    Else
        paramValue = TextBox1.Text
    End If
End Sub

Private Function SomeFunction(text As String) As String
    Dim ReturnValue As String = ""
    'your code here
    Return ReturnValue
End Function

Or you could pass both values and test in your Function code

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim ReturnedValue = SomeFunction(TextBox1.Text, TextBox2.Text)
End Sub

Private Function SomeFunction(text1 As String, text2 As String) As String
    Dim ReturnValue As String = ""
    If MyTestText = "yes" Then
        'process text2
    Else
        'process text1
    End If
    Return ReturnValue
End Function

I am sure there are other ways to handle this.

Mary
  • 14,926
  • 3
  • 18
  • 27