Home All Groups Group Topic Archive Search About
Author
14 Apr 2005 1:24 PM
Brett
What are reasons to create your own events?  Why not just call a class
method/function instead?

Thanks,
Brett

Author
14 Apr 2005 1:59 PM
JohnFol
Because a method or function has to be explicitly called. An even occurs
when something within the application happens by itself, or as a result of
something, like an error condition, or a timer event being fired.  .


Also, consider a piece of code (A) that controls another piece of code(B).
How would B notify A that something has happened. It has no concept of what
has created it, and therefore cannot instigate the communication.



Show quoteHide quote
"Brett" <no@spam.com> wrote in message
news:usL8eVPQFHA.1476@TK2MSFTNGP09.phx.gbl...
> What are reasons to create your own events?  Why not just call a class
> method/function instead?
>
> Thanks,
> Brett
>
Author
14 Apr 2005 2:25 PM
Brett
"JohnFol" <OutlookExpr***@WibbleObbble.Com> wrote in message
news:zHu7e.19018$JO6.1969@newsfe6-win.ntli.net...
> Because a method or function has to be explicitly called. An even occurs
> when something within the application happens by itself, or as a result of
> something, like an error condition, or a timer event being fired.  .
>
>
> Also, consider a piece of code (A) that controls another piece of code(B).
> How would B notify A that something has happened. It has no concept of
> what has created it, and therefore cannot instigate the communication.

What kind of thing may happen?  try/catch can communicate errors but what
else are you referring to?
Author
14 Apr 2005 6:59 PM
Jason
Consider a very simple example of a button sitting on a form. When the user
clicks the button how would the button let the form know it was clicked?
Yes. If the button had a reference to the form it COULD call a public
method. But the button does not know about it so the button raises a Clicked
event.

Also consider a situation  in which two classes (say class A and class B)
have references to same instance of class C. If class C needs to announce
that something happened, how would he know who to tell. Maybe only class A
is interested or maybe both class A and B are intertested. It is better for
class C to just raise the event and whoever cares just listens for this
event to happen.

- Jason

Show quoteHide quote
"Brett" <no@spam.com> wrote in message
news:epnBe3PQFHA.3288@TK2MSFTNGP14.phx.gbl...
>
> "JohnFol" <OutlookExpr***@WibbleObbble.Com> wrote in message
> news:zHu7e.19018$JO6.1969@newsfe6-win.ntli.net...
>> Because a method or function has to be explicitly called. An even occurs
>> when something within the application happens by itself, or as a result
>> of something, like an error condition, or a timer event being fired.  .
>>
>>
>> Also, consider a piece of code (A) that controls another piece of
>> code(B). How would B notify A that something has happened. It has no
>> concept of what has created it, and therefore cannot instigate the
>> communication.
>
> What kind of thing may happen?  try/catch can communicate errors but what
> else are you referring to?
>
Author
14 Apr 2005 2:07 PM
rawCoder
The way I use them ususally.

Events are for intimation.

An Object intimating a class for some event.

You cant by regular means call a method of the class that has instantiated
the object from within the object.
Events help in this case and make life easy.

HTH
rawCoder

Show quoteHide quote
"Brett" <no@spam.com> wrote in message
news:usL8eVPQFHA.1476@TK2MSFTNGP09.phx.gbl...
> What are reasons to create your own events?  Why not just call a class
> method/function instead?
>
> Thanks,
> Brett
>
>
Author
14 Apr 2005 2:24 PM
Brett
"rawCoder" <rawCo***@hotmail.com> wrote in message
news:%235kvvsPQFHA.648@TK2MSFTNGP14.phx.gbl...
> The way I use them ususally.
>
> Events are for intimation.
>
> An Object intimating a class for some event.
>
> You cant by regular means call a method of the class that has instantiated
> the object from within the object.

Can you show a code example of what you mean here?

Show quoteHide quote
> Events help in this case and make life easy.
>
> HTH
> rawCoder
>
> "Brett" <no@spam.com> wrote in message
> news:usL8eVPQFHA.1476@TK2MSFTNGP09.phx.gbl...
>> What are reasons to create your own events?  Why not just call a class
>> method/function instead?
>>
>> Thanks,
>> Brett
>>
>>
>
>
Author
14 Apr 2005 2:43 PM
Peter Proost
Hi, I use them in usercontrols for example I've got a usercontrol with a
combobox on it and some more controls.
The combobox is filled with customers, and when I choose one the other
controls on the usercontrol get filled but I also want to notify the form
where my usercontrol is on. So in my usercontrol I code:

Public Event Customer(ByVal CustomerName as String)

and then when the comboxs' selectedindex changes I call my event like this
RaiseEvent Customer("MyCustomerName")

Next, on the form where the usercontrol is placed I can select the
usercontrol in the code editor (left combo) and select it's event Customer
(right combo) and I get something like this and can set my form.text to the
CustomerName

Private Sub UserControl1_Customer(ByVal CustomerName As String) Handles
UserControl1.Customer
    me.Text = CustomerName
end sub

hth Peter




Show quoteHide quote
"Brett" <no@spam.com> schreef in bericht
news:#arf62PQFHA.1528@TK2MSFTNGP09.phx.gbl...
>
> "rawCoder" <rawCo***@hotmail.com> wrote in message
> news:%235kvvsPQFHA.648@TK2MSFTNGP14.phx.gbl...
> > The way I use them ususally.
> >
> > Events are for intimation.
> >
> > An Object intimating a class for some event.
> >
> > You cant by regular means call a method of the class that has
instantiated
> > the object from within the object.
>
> Can you show a code example of what you mean here?
>
> > Events help in this case and make life easy.
> >
> > HTH
> > rawCoder
> >
> > "Brett" <no@spam.com> wrote in message
> > news:usL8eVPQFHA.1476@TK2MSFTNGP09.phx.gbl...
> >> What are reasons to create your own events?  Why not just call a class
> >> method/function instead?
> >>
> >> Thanks,
> >> Brett
> >>
> >>
> >
> >
>
>
Author
14 Apr 2005 3:09 PM
Brett
"Peter Proost" <pproost@nospam.hotmail.com> wrote in message
news:%23RB%23$BQQFHA.2520@tk2msftngp13.phx.gbl...
> Hi, I use them in usercontrols for example I've got a usercontrol with a
> combobox on it and some more controls.
> The combobox is filled with customers, and when I choose one the other
> controls on the usercontrol get filled but I also want to notify the form
> where my usercontrol is on. So in my usercontrol I code:
>
> Public Event Customer(ByVal CustomerName as String)
>
> and then when the comboxs' selectedindex changes I call my event like this
> RaiseEvent Customer("MyCustomerName")

But you could do a class Customer() method call here as well. What's the
difference?

Show quoteHide quote
>
> Next, on the form where the usercontrol is placed I can select the
> usercontrol in the code editor (left combo) and select it's event Customer
> (right combo) and I get something like this and can set my form.text to
> the
> CustomerName
>
> Private Sub UserControl1_Customer(ByVal CustomerName As String) Handles
> UserControl1.Customer
>    me.Text = CustomerName
> end sub
>
> hth Peter
>
>
>
>
> "Brett" <no@spam.com> schreef in bericht
> news:#arf62PQFHA.1528@TK2MSFTNGP09.phx.gbl...
>>
>> "rawCoder" <rawCo***@hotmail.com> wrote in message
>> news:%235kvvsPQFHA.648@TK2MSFTNGP14.phx.gbl...
>> > The way I use them ususally.
>> >
>> > Events are for intimation.
>> >
>> > An Object intimating a class for some event.
>> >
>> > You cant by regular means call a method of the class that has
> instantiated
>> > the object from within the object.
>>
>> Can you show a code example of what you mean here?
>>
>> > Events help in this case and make life easy.
>> >
>> > HTH
>> > rawCoder
>> >
>> > "Brett" <no@spam.com> wrote in message
>> > news:usL8eVPQFHA.1476@TK2MSFTNGP09.phx.gbl...
>> >> What are reasons to create your own events?  Why not just call a class
>> >> method/function instead?
>> >>
>> >> Thanks,
>> >> Brett
>> >>
>> >>
>> >
>> >
>>
>>
>
>
Author
14 Apr 2005 11:28 PM
Dennis
You are correct..you can write an application with a big while or do loop and
get input from the user then branch to different subs, funcitons, methods,
etc.  That's what the original basic programming language did.  However,
events will make your programming life much easier and more flexible if you
take the time to learn how to use them.

Show quoteHide quote
"Brett" wrote:

>
> "Peter Proost" <pproost@nospam.hotmail.com> wrote in message
> news:%23RB%23$BQQFHA.2520@tk2msftngp13.phx.gbl...
> > Hi, I use them in usercontrols for example I've got a usercontrol with a
> > combobox on it and some more controls.
> > The combobox is filled with customers, and when I choose one the other
> > controls on the usercontrol get filled but I also want to notify the form
> > where my usercontrol is on. So in my usercontrol I code:
> >
> > Public Event Customer(ByVal CustomerName as String)
> >
> > and then when the comboxs' selectedindex changes I call my event like this
> > RaiseEvent Customer("MyCustomerName")
>
> But you could do a class Customer() method call here as well. What's the
> difference?
>
> >
> > Next, on the form where the usercontrol is placed I can select the
> > usercontrol in the code editor (left combo) and select it's event Customer
> > (right combo) and I get something like this and can set my form.text to
> > the
> > CustomerName
> >
> > Private Sub UserControl1_Customer(ByVal CustomerName As String) Handles
> > UserControl1.Customer
> >    me.Text = CustomerName
> > end sub
> >
> > hth Peter
> >
> >
> >
> >
> > "Brett" <no@spam.com> schreef in bericht
> > news:#arf62PQFHA.1528@TK2MSFTNGP09.phx.gbl...
> >>
> >> "rawCoder" <rawCo***@hotmail.com> wrote in message
> >> news:%235kvvsPQFHA.648@TK2MSFTNGP14.phx.gbl...
> >> > The way I use them ususally.
> >> >
> >> > Events are for intimation.
> >> >
> >> > An Object intimating a class for some event.
> >> >
> >> > You cant by regular means call a method of the class that has
> > instantiated
> >> > the object from within the object.
> >>
> >> Can you show a code example of what you mean here?
> >>
> >> > Events help in this case and make life easy.
> >> >
> >> > HTH
> >> > rawCoder
> >> >
> >> > "Brett" <no@spam.com> wrote in message
> >> > news:usL8eVPQFHA.1476@TK2MSFTNGP09.phx.gbl...
> >> >> What are reasons to create your own events?  Why not just call a class
> >> >> method/function instead?
> >> >>
> >> >> Thanks,
> >> >> Brett
> >> >>
> >> >>
> >> >
> >> >
> >>
> >>
> >
> >
>
>
>
Author
15 Apr 2005 2:00 PM
Brett
I understand the form events because of user action but even with all of the
post to my thread, I still have a difficult time seeing where I would use a
custom event.

Perhaps relating it to something I'm working on will help.  I use a while
loop with tcpclient and check if
    networkStream = tcpClient.GetStream()
has anything.  Can I create an event instead and put the while loop code in
it?

Thanks,
Brett

Show quoteHide quote
"Dennis" <Den***@discussions.microsoft.com> wrote in message
news:8083C84D-D9E8-4DB3-921B-311CCF0397B1@microsoft.com...
> You are correct..you can write an application with a big while or do loop
> and
> get input from the user then branch to different subs, funcitons, methods,
> etc.  That's what the original basic programming language did.  However,
> events will make your programming life much easier and more flexible if
> you
> take the time to learn how to use them.
>
> "Brett" wrote:
>
>>
>> "Peter Proost" <pproost@nospam.hotmail.com> wrote in message
>> news:%23RB%23$BQQFHA.2520@tk2msftngp13.phx.gbl...
>> > Hi, I use them in usercontrols for example I've got a usercontrol with
>> > a
>> > combobox on it and some more controls.
>> > The combobox is filled with customers, and when I choose one the other
>> > controls on the usercontrol get filled but I also want to notify the
>> > form
>> > where my usercontrol is on. So in my usercontrol I code:
>> >
>> > Public Event Customer(ByVal CustomerName as String)
>> >
>> > and then when the comboxs' selectedindex changes I call my event like
>> > this
>> > RaiseEvent Customer("MyCustomerName")
>>
>> But you could do a class Customer() method call here as well. What's the
>> difference?
>>
>> >
>> > Next, on the form where the usercontrol is placed I can select the
>> > usercontrol in the code editor (left combo) and select it's event
>> > Customer
>> > (right combo) and I get something like this and can set my form.text to
>> > the
>> > CustomerName
>> >
>> > Private Sub UserControl1_Customer(ByVal CustomerName As String) Handles
>> > UserControl1.Customer
>> >    me.Text = CustomerName
>> > end sub
>> >
>> > hth Peter
>> >
>> >
>> >
>> >
>> > "Brett" <no@spam.com> schreef in bericht
>> > news:#arf62PQFHA.1528@TK2MSFTNGP09.phx.gbl...
>> >>
>> >> "rawCoder" <rawCo***@hotmail.com> wrote in message
>> >> news:%235kvvsPQFHA.648@TK2MSFTNGP14.phx.gbl...
>> >> > The way I use them ususally.
>> >> >
>> >> > Events are for intimation.
>> >> >
>> >> > An Object intimating a class for some event.
>> >> >
>> >> > You cant by regular means call a method of the class that has
>> > instantiated
>> >> > the object from within the object.
>> >>
>> >> Can you show a code example of what you mean here?
>> >>
>> >> > Events help in this case and make life easy.
>> >> >
>> >> > HTH
>> >> > rawCoder
>> >> >
>> >> > "Brett" <no@spam.com> wrote in message
>> >> > news:usL8eVPQFHA.1476@TK2MSFTNGP09.phx.gbl...
>> >> >> What are reasons to create your own events?  Why not just call a
>> >> >> class
>> >> >> method/function instead?
>> >> >>
>> >> >> Thanks,
>> >> >> Brett
>> >> >>
>> >> >>
>> >> >
>> >> >
>> >>
>> >>
>> >
>> >
>>
>>
>>
Author
15 Apr 2005 2:32 PM
Cor Ligthert
Brett,

Do you have problems with your newsreader I see 4 not replied answers from
you, where at least mine answers this question from you,

Cor
Author
15 Apr 2005 4:21 PM
rawCoder
Hi Brett,

I have a class that handles all the Socket related stuff and receives data.
Now when some data is received it RAISES an EVENT so that the parent class
knows there was some data received.
So the parent class can like declare the object WithEvents and hence the
class implementing Socket operations doesnt need to know how to process the
data or the business logic while the parent class handles it.

Suppose you have a custom message queue with thread synchronisation.
Now if the queue has an event that gets raised at appropraite time when some
thing is added in it,
Then the caller doesnt need to do all the plumbing related to DeQueuing.
All it needs to do is declare the queue object and handle the data added
event.

One more way events are very very useful is for inter modular communication
as specified by someone else already in the thread.
Suppose you have a class 'E' which has one event declared in it and one
public method that raises the event.
Now class A has an instance of class E and calls the public mehtod which
raises the event.
Another Class B which also has the instance and handles the event, gets the
notification and the data associated.
The beauty is that the class 'E' never knew anything about 'A' or 'B'

Hope this makes sense to you more in terms of possible implementations where
custom events can be used.
I have implemented one way or another these scenarios in more or else the
same way.
So custom events are really very helpful.

Let us know what exaclty is your confusion now ?

HTH
rawCoder

Show quoteHide quote
"Brett" <no@spam.com> wrote in message
news:%23ssG4NcQFHA.2788@TK2MSFTNGP09.phx.gbl...
> I understand the form events because of user action but even with all of
the
> post to my thread, I still have a difficult time seeing where I would use
a
> custom event.
>
> Perhaps relating it to something I'm working on will help.  I use a while
> loop with tcpclient and check if
>     networkStream = tcpClient.GetStream()
> has anything.  Can I create an event instead and put the while loop code
in
> it?
>
> Thanks,
> Brett
>
> "Dennis" <Den***@discussions.microsoft.com> wrote in message
> news:8083C84D-D9E8-4DB3-921B-311CCF0397B1@microsoft.com...
> > You are correct..you can write an application with a big while or do
loop
> > and
> > get input from the user then branch to different subs, funcitons,
methods,
> > etc.  That's what the original basic programming language did.  However,
> > events will make your programming life much easier and more flexible if
> > you
> > take the time to learn how to use them.
> >
> > "Brett" wrote:
> >
> >>
> >> "Peter Proost" <pproost@nospam.hotmail.com> wrote in message
> >> news:%23RB%23$BQQFHA.2520@tk2msftngp13.phx.gbl...
> >> > Hi, I use them in usercontrols for example I've got a usercontrol
with
> >> > a
> >> > combobox on it and some more controls.
> >> > The combobox is filled with customers, and when I choose one the
other
> >> > controls on the usercontrol get filled but I also want to notify the
> >> > form
> >> > where my usercontrol is on. So in my usercontrol I code:
> >> >
> >> > Public Event Customer(ByVal CustomerName as String)
> >> >
> >> > and then when the comboxs' selectedindex changes I call my event like
> >> > this
> >> > RaiseEvent Customer("MyCustomerName")
> >>
> >> But you could do a class Customer() method call here as well. What's
the
> >> difference?
> >>
> >> >
> >> > Next, on the form where the usercontrol is placed I can select the
> >> > usercontrol in the code editor (left combo) and select it's event
> >> > Customer
> >> > (right combo) and I get something like this and can set my form.text
to
> >> > the
> >> > CustomerName
> >> >
> >> > Private Sub UserControl1_Customer(ByVal CustomerName As String)
Handles
> >> > UserControl1.Customer
> >> >    me.Text = CustomerName
> >> > end sub
> >> >
> >> > hth Peter
> >> >
> >> >
> >> >
> >> >
> >> > "Brett" <no@spam.com> schreef in bericht
> >> > news:#arf62PQFHA.1528@TK2MSFTNGP09.phx.gbl...
> >> >>
> >> >> "rawCoder" <rawCo***@hotmail.com> wrote in message
> >> >> news:%235kvvsPQFHA.648@TK2MSFTNGP14.phx.gbl...
> >> >> > The way I use them ususally.
> >> >> >
> >> >> > Events are for intimation.
> >> >> >
> >> >> > An Object intimating a class for some event.
> >> >> >
> >> >> > You cant by regular means call a method of the class that has
> >> > instantiated
> >> >> > the object from within the object.
> >> >>
> >> >> Can you show a code example of what you mean here?
> >> >>
> >> >> > Events help in this case and make life easy.
> >> >> >
> >> >> > HTH
> >> >> > rawCoder
> >> >> >
> >> >> > "Brett" <no@spam.com> wrote in message
> >> >> > news:usL8eVPQFHA.1476@TK2MSFTNGP09.phx.gbl...
> >> >> >> What are reasons to create your own events?  Why not just call a
> >> >> >> class
> >> >> >> method/function instead?
> >> >> >>
> >> >> >> Thanks,
> >> >> >> Brett
> >> >> >>
> >> >> >>
> >> >> >
> >> >> >
> >> >>
> >> >>
> >> >
> >> >
> >>
> >>
> >>
>
>
Author
15 Apr 2005 5:09 PM
Brett
I'm understanding it better.  Staying with the socket example, which event
is your handler listening for?

Can you possibly post some psuedo code?

Thanks,
Brett

Show quoteHide quote
"rawCoder" <rawCo***@hotmail.com> wrote in message
news:uHgVLcdQFHA.356@TK2MSFTNGP14.phx.gbl...
> Hi Brett,
>
> I have a class that handles all the Socket related stuff and receives
> data.
> Now when some data is received it RAISES an EVENT so that the parent class
> knows there was some data received.
> So the parent class can like declare the object WithEvents and hence the
> class implementing Socket operations doesnt need to know how to process
> the
> data or the business logic while the parent class handles it.
>
> Suppose you have a custom message queue with thread synchronisation.
> Now if the queue has an event that gets raised at appropraite time when
> some
> thing is added in it,
> Then the caller doesnt need to do all the plumbing related to DeQueuing.
> All it needs to do is declare the queue object and handle the data added
> event.
>
> One more way events are very very useful is for inter modular
> communication
> as specified by someone else already in the thread.
> Suppose you have a class 'E' which has one event declared in it and one
> public method that raises the event.
> Now class A has an instance of class E and calls the public mehtod which
> raises the event.
> Another Class B which also has the instance and handles the event, gets
> the
> notification and the data associated.
> The beauty is that the class 'E' never knew anything about 'A' or 'B'
>
> Hope this makes sense to you more in terms of possible implementations
> where
> custom events can be used.
> I have implemented one way or another these scenarios in more or else the
> same way.
> So custom events are really very helpful.
>
> Let us know what exaclty is your confusion now ?
>
> HTH
> rawCoder
>
> "Brett" <no@spam.com> wrote in message
> news:%23ssG4NcQFHA.2788@TK2MSFTNGP09.phx.gbl...
>> I understand the form events because of user action but even with all of
> the
>> post to my thread, I still have a difficult time seeing where I would use
> a
>> custom event.
>>
>> Perhaps relating it to something I'm working on will help.  I use a while
>> loop with tcpclient and check if
>>     networkStream = tcpClient.GetStream()
>> has anything.  Can I create an event instead and put the while loop code
> in
>> it?
>>
>> Thanks,
>> Brett
>>
>> "Dennis" <Den***@discussions.microsoft.com> wrote in message
>> news:8083C84D-D9E8-4DB3-921B-311CCF0397B1@microsoft.com...
>> > You are correct..you can write an application with a big while or do
> loop
>> > and
>> > get input from the user then branch to different subs, funcitons,
> methods,
>> > etc.  That's what the original basic programming language did.
>> > However,
>> > events will make your programming life much easier and more flexible if
>> > you
>> > take the time to learn how to use them.
>> >
>> > "Brett" wrote:
>> >
>> >>
>> >> "Peter Proost" <pproost@nospam.hotmail.com> wrote in message
>> >> news:%23RB%23$BQQFHA.2520@tk2msftngp13.phx.gbl...
>> >> > Hi, I use them in usercontrols for example I've got a usercontrol
> with
>> >> > a
>> >> > combobox on it and some more controls.
>> >> > The combobox is filled with customers, and when I choose one the
> other
>> >> > controls on the usercontrol get filled but I also want to notify the
>> >> > form
>> >> > where my usercontrol is on. So in my usercontrol I code:
>> >> >
>> >> > Public Event Customer(ByVal CustomerName as String)
>> >> >
>> >> > and then when the comboxs' selectedindex changes I call my event
>> >> > like
>> >> > this
>> >> > RaiseEvent Customer("MyCustomerName")
>> >>
>> >> But you could do a class Customer() method call here as well. What's
> the
>> >> difference?
>> >>
>> >> >
>> >> > Next, on the form where the usercontrol is placed I can select the
>> >> > usercontrol in the code editor (left combo) and select it's event
>> >> > Customer
>> >> > (right combo) and I get something like this and can set my form.text
> to
>> >> > the
>> >> > CustomerName
>> >> >
>> >> > Private Sub UserControl1_Customer(ByVal CustomerName As String)
> Handles
>> >> > UserControl1.Customer
>> >> >    me.Text = CustomerName
>> >> > end sub
>> >> >
>> >> > hth Peter
>> >> >
>> >> >
>> >> >
>> >> >
>> >> > "Brett" <no@spam.com> schreef in bericht
>> >> > news:#arf62PQFHA.1528@TK2MSFTNGP09.phx.gbl...
>> >> >>
>> >> >> "rawCoder" <rawCo***@hotmail.com> wrote in message
>> >> >> news:%235kvvsPQFHA.648@TK2MSFTNGP14.phx.gbl...
>> >> >> > The way I use them ususally.
>> >> >> >
>> >> >> > Events are for intimation.
>> >> >> >
>> >> >> > An Object intimating a class for some event.
>> >> >> >
>> >> >> > You cant by regular means call a method of the class that has
>> >> > instantiated
>> >> >> > the object from within the object.
>> >> >>
>> >> >> Can you show a code example of what you mean here?
>> >> >>
>> >> >> > Events help in this case and make life easy.
>> >> >> >
>> >> >> > HTH
>> >> >> > rawCoder
>> >> >> >
>> >> >> > "Brett" <no@spam.com> wrote in message
>> >> >> > news:usL8eVPQFHA.1476@TK2MSFTNGP09.phx.gbl...
>> >> >> >> What are reasons to create your own events?  Why not just call a
>> >> >> >> class
>> >> >> >> method/function instead?
>> >> >> >>
>> >> >> >> Thanks,
>> >> >> >> Brett
>> >> >> >>
>> >> >> >>
>> >> >> >
>> >> >> >
>> >> >>
>> >> >>
>> >> >
>> >> >
>> >>
>> >>
>> >>
>>
>>
>
>
Author
14 Apr 2005 2:40 PM
Anon
>-----Original Message-----
>What are reasons to create your own events?  Why not
just call a class
>method/function instead?
>
>Thanks,
>Brett
>
>
>.
Generally, events are used to communicate an action, or
event, back to the parent object.  Say you have a form
that needs to load a huge amount of data, and you use a
DLL for this, how is the Form going to know how much data
is loaded or if it's done?  I'm sure you can write a
bunch of code, or do some fancy threading, but, in the
DLL, make an event that is triggered, and the Form
handles this event.  Think of an event as a simple
callback system.  Events aren't necessary, but I haven't
seen a really good program out there that doesn't use
events.
Author
14 Apr 2005 2:48 PM
Phill. W
"Brett" <no@spam.com> wrote in message
news:usL8eVPQFHA.1476@TK2MSFTNGP09.phx.gbl...
> What are reasons to create your own events?  Why not just call a class
> method/function instead?

Brett,

To call a Class method, you have to "know about" the Class and
what method(s) it has that you can actually call.  That means your
Event-raising class would have to know, when written, about
every other class that might, possibly, ever instantiate it.  (It's like
saying that Our Friends in Redmond "knew" about every [Form]
class we could ever write that might want to use a TextBox.
Clearly not feasible.

You could, of course, use an Interface or ancestor class, pass a
reference to an object of /that/ Type to your Event-raising Class
and call a [known] method of /that/ but, again, you have to have
the Interface or ancestor class before you can construct your
Event-raising class.

When a class raises an Event, it simply doesn't /care/ who or what
it's raising that Event /to/ (if, indeed, there's anything out there
handling the Event at all).  The "caller" of the class can then choose
whether or not to handle each Event that might be raised (the caller
"knows about" the Events that the Event-raising class exposes).

HTH,
    Phill  W.
Author
14 Apr 2005 2:48 PM
Cor Ligthert
Brett,

I made a simple sample, does that explain something to you?

\\\Needs only a textbox and a button
Private WithEvents myfield As myRule
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        myfield = New myRule
        myfield.Value = TextBox1.Text
    End Sub
    Private Sub myfield_Error(ByVal text As String) _
    Handles myfield.Error
        MessageBox.Show(text)
    End Sub
End Class
Public Class myRule
    Public Event [Error](ByVal text As String)
    Private myValue As String
    Public Property Value() As String
        Get
        End Get
        Set(ByVal Value As String)
            If Not IsNumeric(Value) Then RaiseEvent _
            Error("The Value must be numeric, nothing is changed")
        End Set
    End Property
///

I hope this helps a little bit?

Cor
Author
15 Apr 2005 3:01 PM
Brett
So why not just put the code where you need it since this is only used in
one place:

   Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        myfield = New myRule
        If Not IsNumeric(TextBox1.Text) Then
            msgbox "The Value must be numeric, nothing is changed"
        Else
            myfield.Value = TextBox1.Text
        End If
    End Sub


Show quoteHide quote
"Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message
news:uGxFQEQQFHA.576@TK2MSFTNGP15.phx.gbl...
> Brett,
>
> I made a simple sample, does that explain something to you?
>
> \\\Needs only a textbox and a button
> Private WithEvents myfield As myRule
>    Private Sub Button1_Click(ByVal sender As System.Object, _
>    ByVal e As System.EventArgs) Handles Button1.Click
>        myfield = New myRule
>        myfield.Value = TextBox1.Text
>    End Sub
>    Private Sub myfield_Error(ByVal text As String) _
>    Handles myfield.Error
>        MessageBox.Show(text)
>    End Sub
> End Class
> Public Class myRule
>    Public Event [Error](ByVal text As String)
>    Private myValue As String
>    Public Property Value() As String
>        Get
>        End Get
>        Set(ByVal Value As String)
>            If Not IsNumeric(Value) Then RaiseEvent _
>            Error("The Value must be numeric, nothing is changed")
>        End Set
>    End Property
> ///
>
> I hope this helps a little bit?
>
> Cor
>
Author
15 Apr 2005 4:29 PM
Cor Ligthert
Brett,

Because you have it in your business rules and have it in the class for
that, although you can as well thrown an exception for that.

Cor
Author
14 Apr 2005 4:08 PM
Herfried K. Wagner [MVP]
"Brett" <no@spam.com> schrieb:
> What are reasons to create your own events?  Why not just call a class
> method/function instead?

Visual Basic Language Concepts -- Events and Event Handlers
<URL:http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconUnderstandingEventHandlers.asp>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>