Add a multicast delegate that contains all four math routines?

I had to write a small vb.net console program shown below for my programming class. Now I have to do this:

Add a multicast delegate that contains all four procedures. Call procedures using the DynamicInvoke () method.

How should I do it? I tried everything I could but can't figure it out. Here is my code. Feel free to do whatever you want. Any help and explanation is greatly appreciated. Thank!

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

      

+3


source to share


1 answer


Something like this? ... add this at the end 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

      

+2


source







All Articles