Category: event-handling

May 11 2010

Help understanding .NET delegates, events, and eventhandlers

In the last couple of days I asked a couple of questions about delegates HERE and HERE. I confess...I don't really understand delegates. And I REALLY REALLY REALLY want to understand and master them. (I can define them--type safe function pointers--but since I have little experience with C type languages it is not really helpful.)

Can anyone recommend some online resource(s) that will explain delegates in a way that presumes nothing?

This is one of those moments where I suspect that VB actually handicaps me because it does some wiring for me behind the scenes.

The ideal resource would just explain what delegates are, without reference to anything else like (events and eventhandlers), would show me how all everything is wired up, explain (as I just learned) that delegates are types and what makes them unique as a type (perhaps using a little ildasm magic)). That foundation would then expand to explain how delegates are related to events and eventhandlers which would need a pretty good explanation in there own right. Finally this resource could tie it all together using real examples and explain what wiring DOES happen automatically by the compiler, how to use them, etc. And, oh yeah, when you should and should not use delegates, in other words, downsides and alternatives to using delegates.

What say ye? Can any of you point me to resource(s) that can help me begin my journey to mastery?

EDIT One last thing. The ideal resource will explain how you can and cannot use delegates in an interface declaration. That is something that really tripped me up.

Thanks for your help.

Seth

May 11 2010

Help understanding .NET delegates, events, and eventhandlers

In the last couple of days I asked a couple of questions about delegates HERE and HERE. I confess...I don't really understand delegates. And I REALLY REALLY REALLY want to understand and master them. (I can define them--type safe function pointers--but since I have little experience with C type languages it is not really helpful.)

Can anyone recommend some online resource(s) that will explain delegates in a way that presumes nothing?

This is one of those moments where I suspect that VB actually handicaps me because it does some wiring for me behind the scenes.

The ideal resource would just explain what delegates are, without reference to anything else like (events and eventhandlers), would show me how all everything is wired up, explain (as I just learned) that delegates are types and what makes them unique as a type (perhaps using a little ildasm magic)). That foundation would then expand to explain how delegates are related to events and eventhandlers which would need a pretty good explanation in there own right. Finally this resource could tie it all together using real examples and explain what wiring DOES happen automatically by the compiler, how to use them, etc. And, oh yeah, when you should and should not use delegates, in other words, downsides and alternatives to using delegates.

What say ye? Can any of you point me to resource(s) that can help me begin my journey to mastery?

EDIT One last thing. The ideal resource will explain how you can and cannot use delegates in an interface declaration. That is something that really tripped me up.

Thanks for your help.

Seth

Aug 27 2009

How do you create a cancelable event in vb.net

IN VB.NET (not c#)...

I want to create an event than can be canceled by the listener. Just like you can cancel the closing event of a winforms form in which case the form won't close.

I have already implemented a derived class from EventArgs that has a settable Cancel property as follows:

Public Class AnnounceNavigateEventArgs
    Inherits EventArgs

    Private _cancel As Boolean = False

    ''' <summary>
    ''' Initializes a new instance of the AnnounceNavigateEventArgs class.
    ''' </summary>
    Public Sub New(ByRef cancel As Boolean)
        _cancel = cancel
    End Sub
    Public Property Cancel() As Boolean
        Get
            Return _cancel
        End Get
        Set(ByVal value As Boolean)
            _cancel = value
        End Set
    End Property

End Class

Notice that I am passing the cancel argument byRef to the constructor.

The listener I have setup is setting the property to Cancel=True. I thought ByRef meant that both _cancel and cancel would point to the same location on the stack and that setting _cancel=true would therefore make the cancel = true. But this is not the behavior I am getting. _cancel becomes true in the setter but I guess the argument to the constructor remains false.

What is the proper way to do this in vb.net?

Seth

Aug 13 2009

What is the proper way to setup events for inheritance

Hello,

Normally I setup events like this...

Public Delegate Sub MySpecialEventHandler(sender as object, e as mySpecialEventEventArgs)  '  ...I will not show the implementation of mySpecialEventArgs.
Public Event MySpecialEvent as MySpecialEventHandler  

Private Sub OnMySpecialEvent(newStatus as string)  
     Raise Event MySpecialEvent(Me,New mySpecialEventEventArgs(newStatus))  
End Sub

I have an interface...it looks like this...

Public Interface IWizardUserControl
    ReadOnly Property ShowNavigatePrevious() As Boolean
    ReadOnly Property ShowNavigateNext() As Boolean
    ReadOnly Property ShowNavigateFinish() As Boolean
    ReadOnly Property ShowNavigateCancel() As Boolean
    ReadOnly Property Description() As String
    ReadOnly Property StepCaption() As String
    ReadOnly Property PageImage() As System.Drawing.Image
    ReadOnly Property PageHelpText() As String
    Property IsValid() As Boolean
    Sub OnValidStatusChanged(ByVal validStatus As Boolean)
    Event ValidStatusChanged()
End Interface

I would like the event setup such as what you see above to be implemented in the above interface.

When you setup a delegate in the interface. No errors. But when you try to implement the delegate in the userControl Visual studio says "Delegates cannot implement interface methods" .

My goal is to have a controller class that would cast the derived class as the interface and then add a handler for the interfaces event.

Any help would be appreciated. Thanks in advance.

EDIT - I took John Saunders advice and drilled into the web.ui class. Here is what I did and it is working perfectly.

In the interface I added/changed the following...

Sub OnValidStatusChanged(ByVal validStatus As Boolean)  
Sub ValidStatusChangedHandler(ByVal sender As Object, ByVal e AsValidStatusChangedEventArgs)  
Event ValidStatusChanged As EventHandler

Most relevant point being that I ValidStatusChangedHandler is declared as a Sub not a delegate.

I implemented those in my derived class as follows...

Protected Event ValidStatusChanged As EventHandler Implements IWizardUserControl.ValidStatusChanged

Protected Sub ValidStatusChangedHandler(ByVal sender As Object, ByVal e As ValidStatusChangedEventArgs) Implements IWizardUserControl.ValidStatusChangedHandler
    OnValidStatusChanged(e.ValidStatus)
End Sub

Private Sub OnValidStatusChanged(ByVal status As Boolean) Implements IWizardUserControl.OnValidStatusChanged
    RaiseEvent ValidStatusChanged(Me, New ValidStatusChangedEventArgs(status))
End Sub

Finally in the controller class I am doing this...

Dim iwp As IWizardUserControl = DirectCast(wizardUserControl, IWizardUserControl)
AddHandler iwp.ValidStatusChanged, AddressOf iwp.ValidStatusChangedHandler

Works like a charm.

Seth