您可以将委托函数作为可选参数传递吗?
Can you pass a delegate function as an optional parameter?
我知道在 Visual Basic 中,委托函数不能包含 可选参数。但是方法可以将委托作为可选参数吗?
我想做的是:
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
如何将可选委托作为第二个参数。
这是一回事吗?委托函数可以是可选参数吗?
引用类型的参数只能默认为null。将默认值更改为 null,检查 null 条件,并且不调用委托(什么都不做)。
我知道在 Visual Basic 中,委托函数不能包含 可选参数。但是方法可以将委托作为可选参数吗?
我想做的是:
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
如何将可选委托作为第二个参数。
这是一回事吗?委托函数可以是可选参数吗?
引用类型的参数只能默认为null。将默认值更改为 null,检查 null 条件,并且不调用委托(什么都不做)。