添加一个包含所有四个数学过程的多播委托?

时间:2013-01-27 00:14:57

标签: vb.net delegates invoke multicast

我必须为我的编程课编写下面显示的小vb.net控制台程序。现在我必须这样做:

  

添加包含所有四个过程的多播委托。使用DynamicInvoke()方法调用过程。

我该怎么做?我尝试了我能想到的一切,但似乎无法弄明白。这是我的代码。随意做任何你想做的事。任何帮助和解释都表示赞赏。谢谢!

Imports System
Module Module1
Dim a, b, result as Integer
Private Delegate Sub myDelegate()
Sub Main()
    Dim ask As myDelegate
    ask = New myDelegate(AddressOf Ask1)
    ask()
    Dim Multiply1 As myDelegate
    Multiply1 = New myDelegate(AddressOf Multiply)
    Multiply1()
    Dim Subtraction1 As myDelegate
    Subtraction1 = New myDelegate(AddressOf Subtraction)
    Subtraction1()
    Dim Addition1 As myDelegate
    Addition1 = New myDelegate(AddressOf Addition)
    Addition1()
    Dim Division1 As myDelegate
    Division1 = New myDelegate(AddressOf Division)
    Division1()
    Console.ReadLine()
End Sub
Sub Ask1()
    Console.WriteLine("Enter First Number")
    a = Console.ReadLine()
    Console.WriteLine("Enter Second Number")
    b = Console.ReadLine()
End Sub
Sub Addition()
    Result = a + b
    Console.WriteLine("Added: " & Result)
End Sub
Sub Subtraction()
    Result = a - b
    Console.WriteLine("Subtracted: " & Result)
End Sub
Sub Multiply()
    result = a * b
    Console.WriteLine("Multiplied: " & Result)
End Sub
Sub Division()
    Console.WriteLine("Divided: " & a / b)
End Sub
End Module

1 个答案:

答案 0 :(得分:2)

这样的东西? ...在Main

的末尾添加此内容
    Dim dMultiCast As myDelegate
    dMultiCast = [Delegate].Combine(New myDelegate(AddressOf Addition),
                                    New myDelegate(AddressOf Subtraction),
                                    New myDelegate(AddressOf Division),
                                    New myDelegate(AddressOf Multiply))
    dMultiCast.DynamicInvoke()

    Console.ReadLine()
End Sub