Re: how to wrap your brain around delegates?



"Peter Morris [Droopy eyes software]" <pete@xxxxxxxxxxxxxxxxxxxxxx> a écrit
dans le message de news: 448dae05@xxxxxxxxxxxxxxxxxxxxxxxxx

| I thought that events were multi-cast, not delegates. A delegate is just
a
| method signature declaration.

Well, this is where I am a bit flummoxed. You have to declare an event if
you are declaring a property, but you can simply declare a field of the
delegate type to support that property.

e.g.

public class NotificationEventArgs : EventArgs
{
private object aspect;

public NotificationEventArgs(object aspect)
{
this.aspect = aspect;
}

public object Aspect
{
get { return aspect; }
}
}

public delegate void NotificationHandler(object sender,
NotificationEventArgs args);

public class SubjectImplementer
{
private NotificationHandler notify;

public void OnNotify(object sender, object aspect)
{
if (notify != null)
{
NotificationEventArgs args = new NotificationEventArgs(aspect);
notify(sender, args);
}
}

public event NotificationHandler Notify
{
add { notify += value; }
remove { notify -= value; }
}
}

So the field is treated as multicast by the property !! ??

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


.



Relevant Pages

  • Re: WPF Threading Model
    ... you don't have to declare the delegate ... you can declare your OnServerDataRecieved like so: ... myserver.DataReceivedHandler handler = ... arrives at the server, however I ran into some problems because of the WPF ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Event handler or Delegate?
    ... You need to declare your own event if you're going to write code to ... SelectedItemChanged event, because in the .NET framework Panels don't ... I also had to create a new type of delegate, ... override their behaviour, ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: event and delegate
    ... It depends of how you declared your delegate and your event. ... If you declare ... public void doit() ... Assume I have a declared a delegate and an event with that delegate type. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Generics for Delphi
    ... TSHandler read fOnHandle write fOnHandle; ... So if I have an "event" in a generic class, you are saying that I have to declare a nested delegate type??? ... I see great problems with having to declare a type alias for each and every time that you want to bind a generic type. ...
    (borland.public.delphi.non-technical)
  • Re: VB6 ActiveX DLL Form & Excel
    ... in turn will notify client via events. ... The form can notify class using, ... You'll need to declare the form's variable as class module ...
    (microsoft.public.vb.general.discussion)