使用一个委托引用多个方法

时间:2016-07-11 06:53:59

标签: vb.net methods delegates

在C#中,我使用" + ="向委托引用多个方法。 但是我在VB.NET中尝试了它并且它没有用。

这是示例代码:

        $scope.reset = function() {
         $scope.movie = { name : '', star: '', year: ''};
         //setvalidity is removed
         $scope.movieForm.$setPristine();
         $scope.movieForm.$setUntouched();
     };

1 个答案:

答案 0 :(得分:4)

Delegate.Combine方法就是您想要的:

Dim s1 As New delegateSum(AddressOf sum1)
Dim s2 As New delegateSum(AddressOf sum2)

Dim s As delegateSum = DirectCast([Delegate].Combine(s1, s2), delegateSum)

在这种情况下,组合代表没有多大用处。这两种方法的要点是返回一个总和,但组合的委托只能返回一个值,所以你只能得到第二个方法的结果。您更有可能合并Sub而不是Function

顺便说一下,如果你想组合两个以上的委托,那么你可以用一个或多个委托来完成,因为Combine被重载并且需要两个参数或ParamArray

单一电话:

Dim s1 As New delegateSum(AddressOf sum1)
Dim s2 As New delegateSum(AddressOf sum2)
Dim s3 As New delegateSum(AddressOf sum3)

Dim s As delegateSum = DirectCast([Delegate].Combine(s1, s2, s3), delegateSum)

多次通话:

Dim s1 As New delegateSum(AddressOf sum1)
Dim s2 As New delegateSum(AddressOf sum2)

Dim s As delegateSum = DirectCast([Delegate].Combine(s1, s2), delegateSum)

Dim s3 As New delegateSum(AddressOf sum3)

s = DirectCast([Delegate].Combine(s, s3), delegateSum)