在 vb.net 中将函数作为参数传递?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19498504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:14:46  来源:igfitidea点击:

Pass function as a parameter in vb.net?

vb.netfunctionfunctional-programmingparameter-passing

提问by Bernoulli Lizard

I have class that performs many similar yet different read/write operations to an excel file. All of these operations are defined in separate functions. They all have to be contained within the same long block of code (code that checks and opens a file, saves, closes, etc). What is reasonable way to not have to duplicate this code each time? The problem is that I can't just one method containing all of the shared code before I execute the code that is different for each method because that code must be contained within many layers of if, try, and for statements of the shared code.

我有一个类对 Excel 文件执行许多类似但不同的读/写操作。所有这些操作都在单独的函数中定义。它们都必须包含在同一长代码块中(检查和打开文件、保存、关闭等的代码)。不必每次都复制此代码的合理方法是什么?问题是,在我执行每个方法不同的代码之前,我不能只包含一个包含所有共享代码的方法,因为该代码必须包含在共享代码的 if、try 和 for 语句的多层中。

Is it possible to pass a function as a parameter, and then just run that function inside the shared code? Or is there a better way to handle this?

是否可以将函数作为参数传递,然后在共享代码中运行该函数?或者有没有更好的方法来处理这个问题?

采纳答案by Steve

You can also use a delegateso you can pass function/subs that have a certain signature.

您还可以使用委托,以便您可以传递具有特定签名的函数/子。

回答by Konrad Rudolph

Focusing on the actual question:

专注于实际问题:

Is it possible to pass a function as a parameter

是否可以将函数作为参数传递

Yes. Use AddressOf TheMethodto pass the method into another method. The argument type must be a delegate whose signature matches that of the target method. Inside the method, you treat the parameter as if it were the actual method. Example:

是的。使用该方法传递到另一个方法。参数类型必须是一个委托,其签名与目标方法的签名相匹配。在方法内部,您将参数视为实际方法。例子:AddressOf TheMethod

Sub Foo()
    Console.WriteLine("Foo!")
End Sub

Sub Bar()
    Console.WriteLine("Bar!")
End Sub

Sub CallSomething(f As Action)
    f()
End Sub

' … somewhere:
CallSomething(AddressOf Foo)
CallSomething(AddressOf Bar)

As I said, the actual parameter type depends on the signature of the method you'd like to pass. Use either Action(Of T)or Func(Of T)or a custom delegate type.

正如我所说,实际参数类型取决于您要传递的方法的签名。使用Action(Of T)Func(Of T)或 自定义委托类型。

However, in many cases it's actually appropriate to use an interface instead of a delegate, have different classes implement your interface, and pass objects of those different classes into your method.

但是,在许多情况下,使用接口而不是委托实际上更合适,让不同的类实现您的接口,并将这些不同类的对象传递到您的方法中。

回答by MarcinJuraszek

Declare your parameter as Func(Of T)or Action(Of T).

将您的参数声明为Func(Of T)or Action(Of T)

Which one you should use depend on your methods signature. I could give you more precise answer if you gave us one.

您应该使用哪一个取决于您的方法签名。如果你给我们一个,我可以给你更准确的答案。