Link to home
Start Free TrialLog in
Avatar of AWestEng
AWestEngFlag for Sweden

asked on

Events and Delegates

HI!

I have a question regarding events and delegates.
What d the difference between using a delegate type for an event or not?

When should I use the delegate event and when should I not?

Not sure I understand the text I read on msdn.
SOLUTION
Avatar of brawney
brawney
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fernando Soto
Hi AWestEng;

To the question, "What is the difference between using a delegate type for an event or not?", One is working with delegates in this form involves some routine steps such as defining the delegate, declaring some variables, and creating registration / unregistration method to handle the list of morphed that will be called when an event is fired. As a short cut to doing the above C# provides the event keyword. When you compile a program that uses the event keyword the compiler automatically builds the delegate, the registration / unregistration methods as well as any other variables that is needed. The event keyword is known as syntactic sugar.  

To the question, "When should I use the delegate event and when should I not?", Use the event keyword when ever you can.

Fernando
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AWestEng

ASKER

I'm not sure I understand.

Something like this maybe?

So a delegate is always assigned to an event? (When it compiles)
 
In Vb.Net I can do this

Private Event ExecuteStateMachineEvent()  
But here I don't have any delegate!?
But when using AddHandler, is the delegate created then?
 
But I can also can do this

Private Delegate Sub TestDelegate()  
Private Event TestEvent as TestDelegate
Is there any diffrens bwtween these two, does not the AddHandler suff do the same?

 
In C# I can't do the same, here I must assign a delegate to the event, isn't that right?

public delegate void EventDataUpdatedDelegate();
public event EventDataUpdatedDelegate EventDataUpdated;  
So to my question ,when I should use a event with a delegate or not, is not exactly valid then? I always use a deleagte, but in VB I don't need to declare one, the addhandler and AddressOf fixes that?
Have I understand it correct?
 
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
but a eventhandler is a "delegate"
http://msdn.microsoft.com/en-us/library/system.eventhandler.aspx
so then it it always a delegate then?
and if the event doesn't not always use a type of delegate i still don't understand when I should use
Private Delegate Sub TestDelegate()  
Private Event TestEvent as TestDelegate
or when I only should use
 Private Event TestEvent
spelling error>  

and if the event doesn't always use a type of delegate I still don't understand when I should use

Private Delegate Sub TestDelegate()  
Private Event TestEvent as TestDelegate

or when I only should use

Private Event TestEvent  
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, I think I understand the delegate stuff.
so if I don't use a delegate for the event I can't "connect" methods to this event?
so when is this any good?
 
Private Event TestEvent  
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the stuff that make me confused is this..  
Check the code,
Two "diffrent" cases
It does the same thing, does it not?
So my question here is, why ever use the delegate in this case?

Public Class Form1
 
    Public test As New Class1
 
    Public Sub New()
 
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
 
        AddHandler test.TestEvent1, AddressOf TestSub
 
        AddHandler test.TestEvent2, AddressOf TestSub2
    End Sub
 
    Private Sub TestSub()
        MessageBox.Show("Event1")
    End Sub
 
    Private Sub TestSub2()
        MessageBox.Show("Event2")
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        test.Test()
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        test.Test2()
    End Sub
End Class
 
Public Class Class1
 
    ' case 1
    Public Event TestEvent1()
 
    ' case 2
    Public Delegate Sub TestDelegate()
    Public Event TestEvent2 As TestDelegate
 
    Public Sub Test()
        RaiseEvent TestEvent1()
    End Sub
 
    Public Sub Test2()
        RaiseEvent TestEvent2()
    End Sub
End Class

Open in new window

I think I found it,
case 1
the compiler  declares a delegate implicitly, so there is a delegate, but I don't need to declare it explicitly.
Here is the IL code for case 1 and case 2.
And here I can see (IL code) that the compiler has added a delegate to the event (Eventhandler) in case 1 too.
That was the stuff I was confused about, I thought you could declare events without using some sort of delegate in the end, but that was not the case. it ALWAYS uses a delegate when the event is connected to a "listner".

Case 1
.method public specialname instance void 
        add_TestEvent1(class EventStuff.Class1/TestEvent1EventHandler obj) cil managed synchronized
{
  .custom instance void [mscorlib]System.Diagnostics.DebuggerNonUserCodeAttribute::.ctor() = ( 01 00 00 00 ) 
  // Code size       25 (0x19)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldarg.0
  IL_0002:  ldfld      class EventStuff.Class1/TestEvent1EventHandler EventStuff.Class1::TestEvent1Event
  IL_0007:  ldarg.1
  IL_0008:  call       class [mscorlib]System.Delegate [mscorlib]System.Delegate::Combine(class [mscorlib]System.Delegate,
                                                                                          class [mscorlib]System.Delegate)
  IL_000d:  castclass  EventStuff.Class1/TestEvent1EventHandler
  IL_0012:  stfld      class EventStuff.Class1/TestEvent1EventHandler EventStuff.Class1::TestEvent1Event
  IL_0017:  nop
  IL_0018:  ret
} // end of method Class1::add_TestEvent1
 
 
Case 2
.method public specialname instance void 
        add_TestEvent2(class EventStuff.Class1/TestDelegate obj) cil managed synchronized
{
  .custom instance void [mscorlib]System.Diagnostics.DebuggerNonUserCodeAttribute::.ctor() = ( 01 00 00 00 ) 
  // Code size       25 (0x19)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldarg.0
  IL_0002:  ldfld      class EventStuff.Class1/TestDelegate EventStuff.Class1::TestEvent2Event
  IL_0007:  ldarg.1
  IL_0008:  call       class [mscorlib]System.Delegate [mscorlib]System.Delegate::Combine(class [mscorlib]System.Delegate,
                                                                                          class [mscorlib]System.Delegate)
  IL_000d:  castclass  EventStuff.Class1/TestDelegate
  IL_0012:  stfld      class EventStuff.Class1/TestDelegate EventStuff.Class1::TestEvent2Event
  IL_0017:  nop
  IL_0018:  ret
} // end of method Class1::add_TestEvent2

Open in new window

To your statement, "the compiler  declares a delegate implicitly, so there is a delegate, but I don't need to declare it explicitly.", The point of my post above was just that.
I did not get that the first time I read it.. hehe..
but when reading it again... ,you are quit right there.. :)
thx man
Thx guys!
Not a problem, always glad to help.  ;=)