如何将方法作为 Action 参数传递
How to pass method as Action parameter
如何将参数作为语句传递给方法。我有这样的方法
我尝试在不使用 IIF 关键字
的情况下将 C# 中的三元运算符刺激为 vb.net
Protected Friend Sub TernaryOperater(ByVal condition As Boolean, _
ByVal truePart As action, Optional ByVal falsePart As action = Nothing)
If condition Then
truePart()
Else
falsePart()
End If
End Sub
我这样称呼这个方法:
TernaryOperater(DataGridView1.Rows.Count > 0, _
tp21txtBillNo.Clear, tp21txtBillNo.focus)
在语句tp21txtBillNo.clear
和tp21txtBillNo.focus
下显示为红色错误颜色。这样的语句不支持 Action
吗?
(寻找 C# 和 VB.Net 变体)
在 VB/Net 中传递 Action/Func 的语法:
TernaryOperater(true, Function() OneArg(42), AddressOf NoArgs)
更多信息:
首先要真实 "ternary operator"(正确命名为 "conditional operator"),您应该使用 Func<T>
作为参数,以便它可以 return 结果。
您作为 Func<T>
或 Action<T>
传递的方法签名应该匹配类型 - 没有参数的函数和 return 类型 T for Func<T>
, function (sub) with Action<T>
没有参数。如果它不匹配 - 你可以用 lambda 表达式将它包装成内联 - lambda expressions in VB.Net:
C#:
int Twice(int 值) {return 2 * 值;}
int Half(int value) {return 2 * Value;}
T Ternary<T>(bool condition, Func<T> onTrue, Func<T> onFalse)
{
return condition ? onTrue() : onFalse();
}
void StrangeIf(bool condition, Action onTrue, Action onFalse)
{
if (condition)
onTrue()
else
onFalse();
}
...
StrangeIf(true, ignore => Twice(42), ignore => Halhf(42));
var r = Ternary<int>(true, ignore => Twice(42), ignore => Halhf(42));
VB.Net:
Function TernaryOperater(Of T)(condition As Boolean, _
onTrue As Func(Of T), onFalse As Func(Of T)) As T
If condition Then
return onTrue()
Else
return onFalse()
End If
End Function
Sub StrangeIf(condition As Boolean, _
onTrue As Action, onFalse As Action)
If condition Then
onTrue()
Else
onFalse()
End If
End Sub
Function Twice(v as Integer)
return v * 2
End Function
StrangeIf(true, Function() Twice(42), Function() Twice(1))
如何将参数作为语句传递给方法。我有这样的方法 我尝试在不使用 IIF 关键字
的情况下将 C# 中的三元运算符刺激为 vb.netProtected Friend Sub TernaryOperater(ByVal condition As Boolean, _
ByVal truePart As action, Optional ByVal falsePart As action = Nothing)
If condition Then
truePart()
Else
falsePart()
End If
End Sub
我这样称呼这个方法:
TernaryOperater(DataGridView1.Rows.Count > 0, _
tp21txtBillNo.Clear, tp21txtBillNo.focus)
在语句tp21txtBillNo.clear
和tp21txtBillNo.focus
下显示为红色错误颜色。这样的语句不支持 Action
吗?
(寻找 C# 和 VB.Net 变体)
在 VB/Net 中传递 Action/Func 的语法:
TernaryOperater(true, Function() OneArg(42), AddressOf NoArgs)
更多信息:
首先要真实 "ternary operator"(正确命名为 "conditional operator"),您应该使用 Func<T>
作为参数,以便它可以 return 结果。
您作为 Func<T>
或 Action<T>
传递的方法签名应该匹配类型 - 没有参数的函数和 return 类型 T for Func<T>
, function (sub) with Action<T>
没有参数。如果它不匹配 - 你可以用 lambda 表达式将它包装成内联 - lambda expressions in VB.Net:
C#: int Twice(int 值) {return 2 * 值;} int Half(int value) {return 2 * Value;}
T Ternary<T>(bool condition, Func<T> onTrue, Func<T> onFalse)
{
return condition ? onTrue() : onFalse();
}
void StrangeIf(bool condition, Action onTrue, Action onFalse)
{
if (condition)
onTrue()
else
onFalse();
}
...
StrangeIf(true, ignore => Twice(42), ignore => Halhf(42));
var r = Ternary<int>(true, ignore => Twice(42), ignore => Halhf(42));
VB.Net:
Function TernaryOperater(Of T)(condition As Boolean, _
onTrue As Func(Of T), onFalse As Func(Of T)) As T
If condition Then
return onTrue()
Else
return onFalse()
End If
End Function
Sub StrangeIf(condition As Boolean, _
onTrue As Action, onFalse As Action)
If condition Then
onTrue()
Else
onFalse()
End If
End Sub
Function Twice(v as Integer)
return v * 2
End Function
StrangeIf(true, Function() Twice(42), Function() Twice(1))