[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

Event and raisevent in VB.net

I would like to know the purpose of using Even and Raisevent instead of just building a function or procedure then call it by name?


Thanks
0
jskfan
Asked:
jskfan
3 Solutions
 
vb_jonasCommented:
Think of a ordinary Button for example. If it wouldnt raise a Click event, you could not add functionality to that.
0
 
Fahad MukhtarDistinguished EngineerCommented:
You have to call function manually by yourself but event handlers are automatically called whenever that event is raised and the handler is attached.

to read details about event handling in vb.net, see:
http://www.codeproject.com/vb/net/StepByStepEventsInVBNET.asp
http://www.devcity.net/PrintArticle.aspx?ArticleID=25
0
 
jskfanAuthor Commented:
Can you give me a simple example and go through its code and explain to me how Event/Raiseevent/Withevents are all related.

The score is upgraded to 500 pts.

Thanks
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
We use events because it "decouples" classes from each other.

If you used functions and wanted to call them directly then you must know exactly what kind of class contains the function and the exact name of the function.  Then you must have an actual reference to the object containing the function.  This often involves passing references from one class into another.  This makes the class less resuseable becuase it can only "operate" on classes that it already knows about (since you had to pass a reference into it with a specific type).

If you instead make the class raise an event, then it doesn't need to know (and doesn't care) who or what kind of class receives that event.  The class can be reused somewhere else without change since it simply broadcasts its event and anyone who has subscribed using WithEvents or AddHandler() will be notified.

Consider these four small classes:

    Public Class ClassA
        Public Sub FooA()
            MsgBox("FooA in ClassA")
        End Sub
    End Class


    Public Class ClassB
        Private myA As ClassA
   
        Public Sub New(ByVal clsA As ClassA)
            myA = clsA
        End Sub

        Public Sub FooB()
            myA.FooA()
        End Sub
    End Class


    Public Class ClassC
        Public Sub FooC()
            MsgBox("FooC in ClassC")
        End Sub
    End Class


    Public Class ClassD
        Public Event FooD()

        Public Sub RaiseFooD()
            RaiseEvent FooD()
        End Sub
    End Class

And then the following code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New ClassA
        Dim b As New ClassB(a)
        b.FooB()

        ' versus...

        Dim c As New ClassC
        Dim d As New ClassD
        AddHandler d.FooD, AddressOf c.FooC
        d.RaiseFooD()
    End Sub

ClassB is making ClassA do something by calling the method directly.
ClassD is making ClassC do something by raising an event.

But what if we wanted to make more than one class do something?

With ClassB, we would need to pass in and store more references to each object that must do something.  Then we would have to add code to make the call on each class.

With ClassD though, no changes need be made.  We simply use AddHandler to "wire" up other classes to receive the event in question.  This happens externall to ClassD.  So when ClassD raises its event, they ALL will be notified...and we didn't need to change ClassD in any way.

Make sense?
0
 
jskfanAuthor Commented:
I will test this later and get back with you.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now