473,692 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it just me, or are events just Subs in disguise ??

Hello,

I'm studying a book on VB.net Win apps, and I'm reading a section on
events and delegates and raising events.

Is it just me, or is this not just subs dressed up as something else?

I mean, for one, delegates point to subs, so when you call a delegate,
why not just call the sub dierectly and not bother adding the extra
code involved adding the delegate?

As for events, after delclaring an event you can raise it when
appropriate. Isn't this just the same as calling a sub or function?

Forgive me if I'm missing something life changing here, but I just
don't get it!

Can anyone help clarify the importance of user-defined events?

BTW, I do get it when controls raise events. Like the Click event for
a button. But adding events to your own classes seems exactly like
adding a standard sub.

-Nick
Nov 21 '05 #1
11 1867
Hi Nicky,

Yep you are missing a major point about events and delegates in general.

The beauty of both is that they dont require a "caller". With a traditional
sub the calling procedure has to know the sub exists and call it. With an
event the caller simply registers for the event and has a specified (via a
delegate) method(s) called when something of interest happens.

The pattern is more commonly called Observer/Observable. When you ask why
not just have subs in a class instead of them raising events, then ask
yourself how, when using this method, would a form know when a button a is
clicked.

Using events/delegates the form simply registers for the click event and
then assigns a method (with a signature matching the delegate) to handle
that event, when and/or if it occurs. With the method you suggest of just
using subs, the form would have to continously poll the button to find out
whether it had been clicked or the buttons would have to know the form would
exist at the time of development and know which method within that form to
call when an "event" occurs.

This quickly becomes a complete mess when compared with the de-coupled
elegance of the event driven model.

Delegates are a whole subject area unto themselves. The basics with respect
to events is that they allow any method that matches the delegate signature
and registers for the event to be used to handle the event. With your subs
ideas a developer would have to know every class that would ever be
interested in a specific event when the class is written and know which
method within that class to call in the case of that event. ... which is
extremely complex and wasteful in a small project and impossible n large
projects or with component development.

Delegates are exceptionally powerful in terms of allowing the extension of a
given classes runtime behaviour... in a manner not neccessarily lending
itself to subclassing.... callback methods/asynchronous development etc.

The best way i think for you to discover why events are neccessary is to try
to perform an event driven operation via your subs idea. You'll quickly
discover the benefits of the event driven model.
hth
Richard

"Nicky Smith" <ni***@yahoo.dk > wrote in message
news:ru******** *************** *********@4ax.c om...
Hello,

I'm studying a book on VB.net Win apps, and I'm reading a section on
events and delegates and raising events.

Is it just me, or is this not just subs dressed up as something else?

I mean, for one, delegates point to subs, so when you call a delegate,
why not just call the sub dierectly and not bother adding the extra
code involved adding the delegate?

As for events, after delclaring an event you can raise it when
appropriate. Isn't this just the same as calling a sub or function?

Forgive me if I'm missing something life changing here, but I just
don't get it!

Can anyone help clarify the importance of user-defined events?

BTW, I do get it when controls raise events. Like the Click event for
a button. But adding events to your own classes seems exactly like
adding a standard sub.

-Nick

Nov 21 '05 #2
Hi,

Events run a subroutine based on something happening. If you were
writing a class library you add events to notify the user of something. For
example a class that was periodically checking for email messages would
raise an event when a message is recieved.

Delegates are callback functions. Use them for api callback
functions or something you want to run in the background. For example I
would use a delegate when retrieving large amounts of data from a web
service. This way it can be run the background and not lock up the
computer. Another example would be the enumwindows api call

Ken
----------------

"Nicky Smith" <ni***@yahoo.dk > wrote in message
news:ru******** *************** *********@4ax.c om...
Hello,

I'm studying a book on VB.net Win apps, and I'm reading a section on
events and delegates and raising events.

Is it just me, or is this not just subs dressed up as something else?

I mean, for one, delegates point to subs, so when you call a delegate,
why not just call the sub dierectly and not bother adding the extra
code involved adding the delegate?

As for events, after delclaring an event you can raise it when
appropriate. Isn't this just the same as calling a sub or function?

Forgive me if I'm missing something life changing here, but I just
don't get it!

Can anyone help clarify the importance of user-defined events?

BTW, I do get it when controls raise events. Like the Click event for
a button. But adding events to your own classes seems exactly like
adding a standard sub.

-Nick

Nov 21 '05 #3
On Sun, 19 Sep 2004 10:29:46 +1200, "Richard Myers"
<ri************ *********@basd. co.nz> wrote:
Hi Nicky,

Yep you are missing a major point about events and delegates in general.

The beauty of both is that they dont require a "caller". With a traditional
sub the calling procedure has to know the sub exists and call it. With an
event the caller simply registers for the event and has a specified (via a
delegate) method(s) called when something of interest happens.

The pattern is more commonly called Observer/Observable. When you ask why
not just have subs in a class instead of them raising events, then ask
yourself how, when using this method, would a form know when a button a is
clicked.
Well, as I said, I do know about, and see the logic in control events.
(buttons and other graphical controls, and non-graphical controls such
as the timer). These are an everyday fact of VB and windows
programming, but the book (MCAD Windows Apps from MSPress) suggests
making a bank account class and raising an event if the account is
overdrawn. namely when the account goes in minus. That type of
behaviour is easly handled by a simple check to the balance when any
activity is generated, ie: adding or withdrawing funds.

To quote the book:

Once an event is declared, you can raise it in code when the
designated event occurs. For example, you might have a component that
represents a bank account, which could raise an Overdrawn event
whenever the balance falls below zero.

Of course there are events in VB, the whole VB system is based on
events, but that statement from the book seems questionable.

I'm starting to think that this may be just a bad example.

Using events/delegates the form simply registers for the click event and
then assigns a method (with a signature matching the delegate) to handle
that event, when and/or if it occurs. With the method you suggest of just
using subs, the form would have to continously poll the button to find out
whether it had been clicked or the buttons would have to know the form would
exist at the time of development and know which method within that form to
call when an "event" occurs.

This quickly becomes a complete mess when compared with the de-coupled
elegance of the event driven model.

Delegates are a whole subject area unto themselves. The basics with respect
to events is that they allow any method that matches the delegate signature
and registers for the event to be used to handle the event. With your subs
ideas a developer would have to know every class that would ever be
interested in a specific event when the class is written and know which
method within that class to call in the case of that event. ... which is
extremely complex and wasteful in a small project and impossible n large
projects or with component development.

Delegates are exceptionally powerful in terms of allowing the extension of a
given classes runtime behaviour... in a manner not neccessarily lending
itself to subclassing.... callback methods/asynchronous development etc.

The best way i think for you to discover why events are neccessary is to try
to perform an event driven operation via your subs idea. You'll quickly
discover the benefits of the event driven model.

hth
Richard

"Nicky Smith" <ni***@yahoo.dk > wrote in message
news:ru******* *************** **********@4ax. com...
Hello,

I'm studying a book on VB.net Win apps, and I'm reading a section on
events and delegates and raising events.

Is it just me, or is this not just subs dressed up as something else?

I mean, for one, delegates point to subs, so when you call a delegate,
why not just call the sub dierectly and not bother adding the extra
code involved adding the delegate?

As for events, after delclaring an event you can raise it when
appropriate. Isn't this just the same as calling a sub or function?

Forgive me if I'm missing something life changing here, but I just
don't get it!

Can anyone help clarify the importance of user-defined events?

BTW, I do get it when controls raise events. Like the Click event for
a button. But adding events to your own classes seems exactly like
adding a standard sub.

-Nick


Nov 21 '05 #4
On Sat, 18 Sep 2004 18:46:47 -0400, "Ken Tucker [MVP]"
<vb***@bellsout h.net> wrote:
Hi,

Events run a subroutine based on something happening. If you were
writing a class library you add events to notify the user of something. For
example a class that was periodically checking for email messages would
raise an event when a message is recieved.
Well wouldn't that just as easily be handled by a sub or a function?


Delegates are callback functions. Use them for api callback
functions or something you want to run in the background. For example I
would use a delegate when retrieving large amounts of data from a web
service. This way it can be run the background and not lock up the
computer. Another example would be the enumwindows api call
Isn't that the same as starting a new thread?

Ken
----------------

"Nicky Smith" <ni***@yahoo.dk > wrote in message
news:ru******* *************** **********@4ax. com...
Hello,

I'm studying a book on VB.net Win apps, and I'm reading a section on
events and delegates and raising events.

Is it just me, or is this not just subs dressed up as something else?

I mean, for one, delegates point to subs, so when you call a delegate,
why not just call the sub dierectly and not bother adding the extra
code involved adding the delegate?

As for events, after delclaring an event you can raise it when
appropriate. Isn't this just the same as calling a sub or function?

Forgive me if I'm missing something life changing here, but I just
don't get it!

Can anyone help clarify the importance of user-defined events?

BTW, I do get it when controls raise events. Like the Click event for
a button. But adding events to your own classes seems exactly like
adding a standard sub.

-Nick


Nov 21 '05 #5
Hi,

Events notify the user is something has happened. How would the
main program know the sub or function is running if there is no event.

Delegates are similar to threads. However a delegates unlike
threads can notify you when they are finished.

Ken
------------------
"Nicky Smith" <ni***@yahoo.dk > wrote in message
news:lf******** *************** *********@4ax.c om...
On Sat, 18 Sep 2004 18:46:47 -0400, "Ken Tucker [MVP]"
<vb***@bellsout h.net> wrote:
Hi,

Events run a subroutine based on something happening. If you were
writing a class library you add events to notify the user of something.
For
example a class that was periodically checking for email messages would
raise an event when a message is recieved.
Well wouldn't that just as easily be handled by a sub or a function?


Delegates are callback functions. Use them for api callback
functions or something you want to run in the background. For example I
would use a delegate when retrieving large amounts of data from a web
service. This way it can be run the background and not lock up the
computer. Another example would be the enumwindows api call
Isn't that the same as starting a new thread?

Ken
----------------

"Nicky Smith" <ni***@yahoo.dk > wrote in message
news:ru******* *************** **********@4ax. com...
Hello,

I'm studying a book on VB.net Win apps, and I'm reading a section on
events and delegates and raising events.

Is it just me, or is this not just subs dressed up as something else?

I mean, for one, delegates point to subs, so when you call a delegate,
why not just call the sub dierectly and not bother adding the extra
code involved adding the delegate?

As for events, after delclaring an event you can raise it when
appropriate. Isn't this just the same as calling a sub or function?

Forgive me if I'm missing something life changing here, but I just
don't get it!

Can anyone help clarify the importance of user-defined events?

BTW, I do get it when controls raise events. Like the Click event for
a button. But adding events to your own classes seems exactly like
adding a standard sub.

-Nick

Nov 21 '05 #6
On Sat, 18 Sep 2004 19:44:56 -0400, "Ken Tucker [MVP]"
<vb***@bellsout h.net> wrote:
Hi,

Events notify the user is something has happened. How would the
main program know the sub or function is running if there is no event.
Fx: psuedo code:

sub checkmail

'mail checking code adds or updates the messages object.
if messages.count > 0 then
msgbox "You've got mail"
end if

end sub

Simple. No need to write or raise an event. Granted, the call to
checkmail would be done through a timer event, but as I have
mentioned, i do understand the implementation of events and event
handlers for the vb components and controls, but I just can't see I
would need to write my own event handlers.

I've written a lot of programs in VB5, 6 and .Net and I've never
needed to 'Raise an event'. Also, I've never seen the point in raising
an error either.

If for example a database connection failed, you can raise an error
which causes an error handler to start (in vb classic this was the
old, On Error Goto and then the error handling code) - but again, this
could be done by displaying a message box and then exiting the sub or
continuing (the same as the Resume Next statement).

Delegates are similar to threads. However a delegates unlike
threads can notify you when they are finished.

No that does sound usefull.

Ken
------------------
"Nicky Smith" <ni***@yahoo.dk > wrote in message
news:lf******* *************** **********@4ax. com...
On Sat, 18 Sep 2004 18:46:47 -0400, "Ken Tucker [MVP]"
<vb***@bellsou th.net> wrote:
Hi,

Events run a subroutine based on something happening. If you were
writing a class library you add events to notify the user of something.
For
example a class that was periodically checking for email messages would
raise an event when a message is recieved.


Well wouldn't that just as easily be handled by a sub or a function?


Delegates are callback functions. Use them for api callback
functions or something you want to run in the background. For example I
would use a delegate when retrieving large amounts of data from a web
service. This way it can be run the background and not lock up the
computer. Another example would be the enumwindows api call


Isn't that the same as starting a new thread?

Ken
----------------

"Nicky Smith" <ni***@yahoo.dk > wrote in message
news:ru****** *************** ***********@4ax .com...
Hello,

I'm studying a book on VB.net Win apps, and I'm reading a section on
events and delegates and raising events.

Is it just me, or is this not just subs dressed up as something else?

I mean, for one, delegates point to subs, so when you call a delegate,
why not just call the sub dierectly and not bother adding the extra
code involved adding the delegate?

As for events, after delclaring an event you can raise it when
appropriate . Isn't this just the same as calling a sub or function?

Forgive me if I'm missing something life changing here, but I just
don't get it!

Can anyone help clarify the importance of user-defined events?

BTW, I do get it when controls raise events. Like the Click event for
a button. But adding events to your own classes seems exactly like
adding a standard sub.

-Nick


Nov 21 '05 #7
Nicky,

Thirth try, in additions to the others.

The most simple way to tell why to raise an event is in a multithreading
operation.

Thread A is busy, however Thread B has found there has to be done something
in Thread A which has created Thread B. You have to raise an event because
thread A should first complete the processes he is busy with before he can
handle the event.

Another reason is that when you are using a seperated class than you do not
know the name of the form (or other class) you are using it in.

So in that class you cannot say, If value > 1 then ??????.showmess age. Here
you needs too an event to raise.

In your form you can now instance your object "withevents " and when you make
a sub tell the event that is used by "handles myobject.event1 "

In a class itself you have never to raise an event when that is only used in
that class, than you should call the sub or function directly

This is only about the event, because a delegate is not an event however a
pointer to the address of any object in your program, that can be used for
more than only events and I do not like to mix up two things.

I hope this gives some idea's

Cor

Nov 21 '05 #8
Nicky,
In addition to the other comments.

You are correct Event handlers are subs in disguise.

You are missing 2 very important points about Events.

1. They decouple the code that is being notified from the code doing the
notification. In Bank Account sample, the bank account can raise an event
that says it is overdrawn. The form that is displaying the bank account can
handle this event and either display a message box or put a message in the
status bar or what ever. The form itself takes care of the UI, while the
Bank Account does its business (separates your application into
Domain(Business ) and Presentation(UI ).)

2. They allow multiple handlers of the event. In the Bank Account sample,
the Bank object (that owns all Bank Accounts) could handle the overdrawn
event and add a overdraft fee transaction to the Bank Account, the Bank
Account Detail Form displaying the Bank Account can display an item specific
message, the Bank Account Summary Form could change the display of the item
to.
The reason your class adds an event is EXACTLY the same reason you would add
the Click event to a form. Just as the Click event of a button allows your
Form to handle a button being clicked, so does the Overdrawn event on
BankAccount, it allows another object do what needs to be done when the bank
account is overdrawn. Rather then rely on the Bank Account doing the right
thing. What happens when the Bank Account is used in a Windows Service
(Messagebox not available) and its used in a Windows Form (MessageBox
available) and its used in a Web Form (MessageBox not available) and its
used ...

Hope this helps
Jay

"Nicky Smith" <ni***@yahoo.dk > wrote in message
news:ru******** *************** *********@4ax.c om...
Hello,

I'm studying a book on VB.net Win apps, and I'm reading a section on
events and delegates and raising events.

Is it just me, or is this not just subs dressed up as something else?

I mean, for one, delegates point to subs, so when you call a delegate,
why not just call the sub dierectly and not bother adding the extra
code involved adding the delegate?

As for events, after delclaring an event you can raise it when
appropriate. Isn't this just the same as calling a sub or function?

Forgive me if I'm missing something life changing here, but I just
don't get it!

Can anyone help clarify the importance of user-defined events?

BTW, I do get it when controls raise events. Like the Click event for
a button. But adding events to your own classes seems exactly like
adding a standard sub.

-Nick

Nov 21 '05 #9

"Nicky Smith" <ni***@yahoo.dk > wrote in message
news:ru******** *************** *********@4ax.c om...
Can anyone help clarify the importance of user-defined events?

BTW, I do get it when controls raise events. Like the Click event for
a button. But adding events to your own classes seems exactly like
adding a standard sub.


Event: Your telephone rings.

Event handler: You answer the phone...or you don't.

Imagine if the phone were designed to "pick up" as soon as it rang. You
would no longer be given a choice of whether to answer or not. This is why
you define events in components. It gives a consumer of your product the
ABILITY to handle the event if it so desires.

Now you may be designing components which are for your use and your use
only. You know that EVERY TIME a certain condition occurs, you want to do
something about it, so my example of choosing to handle the event or not
doesn't apply to you. In this case, events aren't absolutely necessary, but
if you encounter the condition in component A and you want to run code in
form B, you're going to have to make the Sub/Function in form B available to
the rest of your project (i.e., have at least Friend visibility). Worse, if
you may have multiple occurrences of form B (each of which instantiates a
component A), you'll have to provide a mechanism to keep track of which form
B this particular instance of component A relates to. Events greatly
simplify this, because any instance of form B just needs to hook up its
handler to its component A's event.
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2074
by: Kate | last post by:
I am learning VB by working on an existing application. It is dawning on me that this particular program was not designed very carefully, and I have come to the point where I have to deal with this situation: One of the forms presents the user with high-level options. Depending on the option selected, certain frames are made visible. The controls in the frames include options, checkboxes, text boxes and labels, the settings of all of...
8
2288
by: | last post by:
In global.asa I have some code outside all the Subs. I have some confirmation that it is being executed OnStart. Yet I can's see if it is executed OnEnd. The literature I have says that OnEnd the Sub Application_OnEnd is executed, but is does not say that anything else is executed Actually I even can't understand if global.asa is permited to have any code outside the Subs
13
2091
by: Steven Scaife | last post by:
I have decided to re-write the intranet site i created over a year ago. The coding is pretty awful and hard to read cos I made the mistake of not putting comments in or putting crappy comments in with the code. So I have decided to try and make things neater by grouping it into subs. So i have a sub to create the variables, a sub for the connection etc. Only problem is when i call my subs, it says that the variable is undefined. If i...
1
1200
by: Brett Hewitson | last post by:
I have created a class that contains events. I have declared an instance of the class in a form so that the form can react to the events of the class. I have noticed that when a control is dropped onto a form, you are able to create event stubs by changing to the events tab on the properties sheet and typing in the name of the method etc. I am at a loss as how to add the event subs of the class in without typing it in manually.
2
1494
by: nakhi | last post by:
Hi, I declared a dataset outside any subs, then I fill the dataset with a tables in a Private Sub1, then I want to take the data out from the dataset in Sub2. like . sub2() datagrid.datasource=ds.tables("t").defautview datagrid.databind() end sub
1
1319
by: Kishor | last post by:
Hi, I am developing an application which is to be used with power point. I am now in need of help from you guys. In my Application (developing using VB.net), I am creating the object of the power point everything is working fine, but the events are not firing. I am here pasteing code please see this. 'Declaration Public WithEvents op As New PowerPoint.Application()
2
1269
by: al | last post by:
Greetings, Can someone please tell me the what is the defference betweeen delegates and events handler??? MTIA, Grawsha
2
1309
by: EdB | last post by:
I have a datagrid named grdSubClients I have all but one column set as read only, and I'm trying to trap events when the user enters data into the one editable column. I have formatted the grid using column styles, but turning that off had no positive effect. The issue is the following events seem like what I would want Private Sub grdSubClients_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles...
5
2730
by: Christian Decker | last post by:
Hi, I'm trying to figure out how my users may use (or better misuse) the input fields of my site to to insert JavaScript. I'm already replacing all occurencies of "script" in every input field but I wanted to know if anybody here ever stumbled upon a trick to disguise the <script> tag? Greets, Snyke
0
8608
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8540
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9086
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8803
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8806
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4324
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4557
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1959
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.