How to use an event in a program to print multiples of 2 and 3? VB.NET

时间:2015-08-07 02:16:15

标签: vb.net events

I need to use three different methods of calling methods to print multiples of 2 and 3. I figured out the programs using delegates and threads but I don't understand how to use an event for this task. Here's what I tried, how do I fix it? I don't really grasp the concept of events as it was explained in my course.

Module Module1
  Public Event Multiples(Byval val as Integer)
  Sub Main()
    AddHandler Multiple, Addressof Multsof2
        For X as Integer = 0 to 100
            If X mod 2 = 0 then
                Console.writeline(X)
            End if
            Next
    AddHandler Multiple, Addressof Multsof3
        For Y as Integer = 0 to 100
            If Y mod 3 = 0 then
                Console.writeline(Y)
            End if
            Next
    End Sub
End Module

Here is how I did it using a delegate,

Module Module1

Public Delegate Sub Multiples()

Sub Main()
    Dim Mults as Multiples
    Mults = new Multiples(AddressOf multsof2)
    Mults()
    Mults = new Multiples(Addressof multsof3)
    Mults()
End Sub

Sub multsof2()
    For X as Integer = 0 to 100
        If X mod 2 = 0 Then
            console.writeline(X)
        End if
    Next
End Sub

Sub multsof3()
    For Y as Integer = 0 to 100
        If Y mod 3 = 0 Then
            console.Writeline(Y)
        End if
    Next

    Console.readkey()
End Sub
End Module

1 个答案:

答案 0 :(得分:1)

An event allows a class instance to notify listeners that they should do something.

Public Class EventProducer

    Public Event SomethingInterestingHappened(value As Int32)

    Public Sub StartTheAction()
        Dim index As Int32 = 0
        Do
            If (0 = (index Mod 2)) Then
                RaiseEvent SomethingInterestingHappened(index)
            End If
            index += 1
        Loop Until 32 <= index
    End Sub

End Class



Public Class EventConsumer
    ' This class is just an example container.  Any code container will do.
    Private producer As EventProducer = new EventProducer

    Public Sub New()
        AddHandler producer.SomethingInterestingHappened, 
                       AddressOf SomethingInterestingHappenedHandler
    End Sub

    Private Sub SomethingInterestingHappenedHandler(ByVal value As Int32)
        Console.WriteLine(value)
    End Sub

    Public Sub AllDoneListeningToEvents()
        RemoveHandler producer.SomethingInterestingHappened, 
                       AddressOf SomethingInterestingHappenedHandler
    End Sub
End Class

See how the EventProducer.StartAction issues a RaiseEvent statement. That will inform anyone who is listening that the event has fired.

Then in EventConsumer.New() we call AddHandler to first tell EventProducer that we would like to subscribe to the SomethingInterestingHappened event, and second to tell the event mechanism what code should be run when the event fires (in this case the SomethingInterestingHappenedHandler function).

This is a very simple and contrived example, but should set you on the right path.

As pointed out in the comments, calling RemoveHandler is important. If you forget you could end up with memory leaks.