Why use Events?

  • Thread starter Thread starter Brett
  • Start date Start date
B

Brett

What are reasons to create your own events? Why not just call a class
method/function instead?

Thanks,
Brett
 
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.
 
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
 
rawCoder said:
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?
 
JohnFol said:
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?
 
-----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.
 
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
 
Brett said:
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.
 
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
 
Peter Proost said:
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?
 
Sorry for late reply,.

The events can be anything like DataReceived, DataSent, Connected,
Disconnected.

Have you seen the VB6 Winsock control.
That might help you grasp the idea of an encapsulated socket module based on
event.

HTH
rawCoder
 

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

Back
Top