RaiseEvent... vs Calling Sub directly?

R

Ron

Greetings,

I am trying to understand the rational for Raising Events
instead of just calling a sub. Could someone explain the
difference between the following 2 scenarios? Why would I
want to raise an event instead of just calling the sub?

Scenario1 -- Using Events
------------------------------------------------------
Public Class Form1

Private WithEvents c1 As clsTest

Sub Testing()
c1 = New clsTest()
c1.TestSub()
End Sub

Sub cTest(ByVal Msg As String) Handles c1.TestEvent
MsgBox Msg
End Sub
End Class
-----------------------------------------------------
Public Class clsTest
Public Event TestEvent(ByVal Msg As String)

Public Sub TestSub
Dim strTemp As String = "testing"
RaiseEvent TestEvent(strTemp)
End Sub
End Class

*****************************************************
*****************************************************

Scenario2 -- No Events
-----------------------------------------------------
Public Class Form1
Dim c1 As clsTest

Sub Testing()
c1 = New clsTest()
c1.TestSub
End Sub
End Class
-----------------------------------------------------
Public Class clsTest
Public Sub TestSub
Test2Sub()
End Sub

Private Sub Test2Sub()
MsgBox "Testing"
End Sub
End Class
 
G

Guest

Jan. 6, 2005

By using events, more than one method or component can handle the
same event....

Public Class Form1

Private WithEvents c1 As clsTest

Sub Testing()
c1 = New clsTest()
c1.TestSub()
End Sub

Sub cTest(ByVal Msg As String) Handles c1.TestEvent
MsgBox Msg
End Sub

Sub SECONDTEST(byval msg as string) handles c1.testevent
Do some more work here........
end sub
End Class

So if more than one person had the instance of c1 then all of them (on
seperate computers such as .Net Remoting) could display the message on their
computers. It is sort of like a newsletter where multiple people sign up for
the same event (publishing a new edition). So in conclusion, if other
components are using your class that might want to also handle the event,
then you should use events. If this reply helped you, then please click on
the "Yes" box above this message! I hope this
helps to clear up some of the details!


Joseph MCAD
 
R

Ron

Hi Joseph,

Thanks for your explanation. Yes, it did clear things up
for me. But I don't see a "Yes" box above the message for
me to click on.

Question: Your explanation sounds similar to using
Delegates. May I ask if there is a similarity here? If
yes, what would be the subtle difference between using
Events or Delegates?

Thanks for your help,
Ron
 
M

Marina

The other issue is knowing which sub to call. Let's say you are a button and
you need to mimic raisin the click event so someone knows you were just
clicked. How would you do this? You don't know what object you are on - a
form, a usercontrol? You don't know what method the developer wants to get
called to be notified that the button was clicked.

It would be impossible to implement a button click event by just calling
known methods.
 
G

Guest

Jan. 6, 2005

I don't understand what Marina means. The difference between events
and delegates is that with events you use AddHandler Obj.Event, AddressOf
Method while with delegates you can submit them across networks and the
internet. Then the component that you sent the delegate to can use
DelegateObj.Invoke which will invoke a method on the component that the
delegate came from. This acts like a callback. This way the component across
the internet can call a method on a different component when it is done
processing something without having to have the instance of the other
component. In other words... The delegate can be thought of your email
address that you give the newsletter component. The newsletter can (across
the network or internet) then use the email address to send you (callback)
the newsletter when it publishes a new one (completes processing). If you
used events then the component across the internet would have to have an
object instance (access to your computer) of the component to callback. I
hope this is still understandable! The "Yes" button should be just above the
top of the message right under the information that shows the subject, from,
and in contents. Please look again for it! Thanks and have a great day! (Feel
free to ask more!)


Joseph MCAD
 
M

Marina

I was saying, that unless you using events/delegates, there is no way to
register callbacks. The example in the post showed calling a specific
method in a specific class as a way of saying 'an event occurred'. Well, if
you are developing a button, you can't do that. What method would you call
to notify the user the someone just clicked the button? You can't - you need
to give the client a way to register for a callback.

I was giving another important example of why you would want events, vs.
calling a sub directly.
 
G

Guest

Jan. 6, 2005

Yes, you are right if you are developing controls that respond to
user generated events. I was saying things in the context of implementing
events for your own code in the same application or in a different
applications that use it directly in code, but not exposing it to the UI.
There is a difference between using the component in code and on the UI. I
believe this is where we had a misunderstanding of the context of what each
other was talking in terms of. I hope that I now have correctly interpreted
what you mean. Thanks and have a great day!


Joseph MCAD
 
M

Marina

I don't think I really understand you. In any case where the component is a
library component - meaning it could be used in any project, it would have
to follow this model. Regardless of whether or not it had anything to do
with the UI. Example: a Timer component. Has nothing to do with UI, can be
created entirely in code, but needs to let the consumer know that the time
has elapsed. Events pretty much become necessary when you write a reusable
object that has behaviors that you need to extend.

I am not sure why you are debating this, as my post was just giving another
example of why one would want to use events. I did not contradict anything
you had said in your post.
 
G

Guest

Jan. 6, 2005

I just realized that if you double click a post then it will come up
in a whole new window. That means that the "Yes" button will be at the bottom
of the message. If you could click it on my first post I would greatly
appreciate it! Thanks and I hope that your question has been answered.
 
V

Victor Purinton

Ron said:
Greetings,

I am trying to understand the rational for Raising Events
instead of just calling a sub.


In general, events are useful when you have asynchronous or otherwise
unpredictable operations. For example, you might have a class that
monitors something (file system, database, etc.) and raises an event
when certain conditions are met.

Events are especially useful when you are programming hardware devices.
You can implement a class that controls various functions of a device,
and raises events when the functions are complete. Often the device
will have to be polled, but if you encapsulate that in the class the
user of the class only sees the XXX_Complete event. It's much cleaner.
 
R

Ron

So it seems that for basic hands off applications that
just do stuff like retrieiving files from an external
source on a scheduled basis or sending out emails
(scheduled daily basis) I could get by just calling a sub
directly rather than raising events. This is mostly
what I do

Or if multiple users are simultaneously using an
application then I could see raising events when
something happens.
 
R

Ron

Thanks again for all your replies. But, with all due
respect, I don't see a "Yes" button anywhere on
this "Post a New Message" window/form. There are only 2
buttons, "Send" "Cancel". I am posting
through "Microsoft Newsgroups -
microsoft.public.dotnet.languages.vb".

Regards,
Ron
 
C

Chris

Keep in mind, Joseph, that not everyone accesses the newsgroups the
same way you do. For example, I use Google Groups and there is no
"Yes" button to press. Others may use a dedicated newsreader
application.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top