newbie Event question

B

boaz

Hi,

I am not quite understand the syntex for Event.

Class My_Class
Event My_Event(ByVal parm1 As String)

Sub Raise_My_Event()
Rasie... My_Event("ABC")
End Sub
....
End Class

Dim WithEvents My_Obj As New My_Class

Sub My_Handler(ByVal parm2 As String) Handles My_Obj.My_Event
....
End Sub

1) "ABC" is passed to parm1 for sure.
2) Does the number of parameters for My_Handler have to match with My_Event?
3) It doesn't have to match the spelling, does it? (i.e.) Can't use parm2.
must name it parm1.
4) Is "ABC" being passed to parm2 automatically?



--
 
H

Herfried K. Wagner [MVP]

* "boaz said:
Hi,

I am not quite understand the syntex for Event.

Class My_Class
Event My_Event(ByVal parm1 As String)

Sub Raise_My_Event()
Rasie... My_Event("ABC")
End Sub
...
End Class

Dim WithEvents My_Obj As New My_Class

Sub My_Handler(ByVal parm2 As String) Handles My_Obj.My_Event
...
End Sub

1) "ABC" is passed to parm1 for sure.

Yes, if it is passed in the 'RaiseEvent' statement.
2) Does the number of parameters for My_Handler have to match with My_Event?
Yes.

3) It doesn't have to match the spelling, does it? (i.e.) Can't use parm2.
must name it parm1.

Doesn't need not match.
4) Is "ABC" being passed to parm2 automatically?

Yes.
 
J

Jeremy Todd

boaz said:
Hi,

I am not quite understand the syntex for Event.

Class My_Class
Event My_Event(ByVal parm1 As String)

Sub Raise_My_Event()
Rasie... My_Event("ABC")
End Sub
...
End Class

Dim WithEvents My_Obj As New My_Class

Sub My_Handler(ByVal parm2 As String) Handles My_Obj.My_Event
...
End Sub

1) "ABC" is passed to parm1 for sure.
2) Does the number of parameters for My_Handler have to match with My_Event?
3) It doesn't have to match the spelling, does it? (i.e.) Can't use parm2.
must name it parm1.
4) Is "ABC" being passed to parm2 automatically?

Yep to all of those. As for 3), the spelling doesn't have to match, but
it makes things more consistent if they do.

Just FYI, there are some conventions on how to define and raise events.
I'm sure you'd eventually figure these out from the docs, but they're not
always clear at first.

First, nearly all events have the signature MyEvent(sender As Object, e
As EventArgs). If you need to pass more data to the event, use a class
derived from EventArgs to do it (for example, look up, oh, MouseEventArgs in
the docs). The sender parameter is important because in VB.NET, unlike
previous versions, you can have one method handle the events from lots of
different sources, and so sender gives you a way to tell which object the
event originated from.

Second, certain common event signatures have predefined names. For
example,

Public Event ItemChanged(sender As Object, e As EventArgs)

and

Public Event ItemChanged As EventHandler

are pretty much equivalent -- i.e. "EventHandler" is a predefined event
type with the signature (Object, EventArgs). In my experience, EventHandler
is the type of event you'll want to raise maybe 80% of the time.

Third, events should be raised in an overridable sub called OnEventName.

So, here's a prototype for a class that raises an event:

Class MyClass

' This defines the event
Public Event ItemChanged As EventHandler

' This is the property the event describes.
' In the Set section, there is a call to the OnItemChanged
' method, which raises the event. The value passed to it
' is EventArgs.Empty, which is a special instance of the
' EventArgs class that just says there's no special data for
' the event.
Public Property Item As Object
Get
' ...
End Get
Set(value As Object)
' ...
OnItemChanged(EventArgs.Empty)
End Set
End Property

' This method will raise the ItemChanged event.
' It is declared Protected Overridable so that inherited classes
' can raise the event themselves, or alter the behavior of what
' happens when the event is raised.
Protected Overridable Sub OnItemChanged(e As EventArgs)
RaiseEvent ItemChanged(Me, e)
End Sub
End Class

Sorry if this is more than you wanted to know! The .NET event model
takes a little getting used to, but it makes a lot of sense once you
understand how it's set up.

Good luck!
Jeremy
 

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