你可以传递一个委托函数作为一个可选参数吗? [英] Can you pass a delegate function as an optional parameter?

查看:181
本文介绍了你可以传递一个委托函数作为一个可选参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在Visual Basic中,委托函数不能包含可选参数。但是,一个方法可以将代理作为可选参数吗?

I know that in Visual Basic, delegate function cannot contain optional parameters. But can a method take a delegate as an optional parameter?

我想做的是这样的:

Delegate Sub MyDelegate(ByVal input As String)


Sub MyDelegateDefault(ByVal input As String)
    'by default do nothing'
End Sub


Sub MyDelegateCustom1(ByVal input As String)
    'do something here'
End Sub

在代码的不同部分:

Sub OtherFunction(ByVal str As String, Optional ByVal delegate As MyDelegate = AddressOf MyDelegateDefault)
    delegate(str)
End Sub

Sub ParentFunction()
    OtherFunction("", ) '< "" as string, nothing for optional delegate parameter'
End Sub

注意最终的函数 OtherFunction 将可选委托作为第二个参数。

Note how the final function OtherFunction takes a optional delegate as second parameter.

这是一件事吗?一个委托函数可以是可选参数?

Is this a thing? Can a delegate function be an optional parameter?

推荐答案

引用类型的参数只能默认为null。将默认值更改为null,检查null条件,不要调用委托(不做任何操作)。

A parameter that is of a reference type can only be defaulted to null. Change the default value to null, check for the null condition, and don't call the delegate (do nothing).

这篇关于你可以传递一个委托函数作为一个可选参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆