PC Review


Reply
 
 
Tony Johansson
Guest
Posts: n/a
 
      30th Jun 2008
Hello!

Delegate is normally set up in this way using the += construction.
Here an example on event Elapsed in class Timer.
pollTimer.Elapsed += new ElapsedEventHandler(CheckForMessage);
where method CheckForMessage has signature
private void CheckForMessage(object sender, ElapsedEvebtArgs e)
{}

Here I have Comparison<T>. This is a delegate type that can be used for
sorting when the delegate
method has this signature
int method (T objectA, objectB) {}
This construction
Comparison<Vector> sorterDelegat = new Comparison<Vector>
(someSuitableMethodForSort);
works fine setting up a delete method that can be used for example when
sorting

But why is is it not possible to use += in this way. If I try with += I get
compiler error
because the syntax is not understandable by the compiler.
Comparison<Vector> sorterDelegat += new Comparison<Vector>
(VectorDelegates.Compare);
This work always when setting up a delegete method that can be used when the
event is trigged.

//Tony


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      30th Jun 2008
On Jun 30, 12:04*pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
> Delegate is normally set up in this way using the += construction.


Define "normally". You usually subscribe to an event that way, but
using delegates in LINQ to Objects almost never uses += for example.

> Here an example on event Elapsed in class Timer.
> pollTimer.Elapsed += new ElapsedEventHandler(CheckForMessage);
> where method CheckForMessage has signature
> private void CheckForMessage(object sender, ElapsedEvebtArgs e)
> {}


Okay - an event based example.

> Here I have Comparison<T>. This is a delegate type that can be used for
> sorting when the delegate
> method has this signature
> int method (T objectA, objectB) {}
> This construction
> Comparison<Vector> sorterDelegat = new Comparison<Vector>
> (someSuitableMethodForSort);
> works fine setting up a delete method that can be used for example when
> sorting
>
> But why is is it not possible to use += in this way. If I try with += I get
> compiler error
> because the syntax is not understandable by the compiler.
> Comparison<Vector> sorterDelegat += new Comparison<Vector>
> (VectorDelegates.Compare);
> This work always when setting up a delegete method that can be used when the
> event is trigged.


That's because it's *adding* a handler for an event. Here you're just
trying to declare and initialize a new variable. It's the equivalent
of writing something like:

int i += 10;

Jon
 
Reply With Quote
 
Ignacio Machin ( .NET/ C# MVP )
Guest
Posts: n/a
 
      30th Jun 2008
On Jun 30, 7:04*am, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
> Hello!
>
> Delegate is normally set up in this way using the += construction.



Not really, you use the += with events. not with delegates.
That an event has a type that needs to be a delegate is another
subject.

delegates can and are used without an event.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove a given delegate from the delegate invocation list mehdi Microsoft C# .NET 2 18th Feb 2007 11:02 PM
Delegate issue - meetings only show up after delegate opened the request nils.pommerien@gmail.com Microsoft Outlook Calendar 0 21st Jul 2006 06:16 PM
Outlook 2002 - Delegate updates in delegate calendar? =?Utf-8?B?V2F6emE=?= Microsoft Outlook Calendar 0 1st Feb 2006 11:20 AM
delegate access - meetings appear in delegate calendar! =?Utf-8?B?TWljaGFlbCBZb3JrZQ==?= Microsoft Outlook Calendar 0 13th Jan 2006 04:46 PM
Delegate Gurus: Can I create a delegate for a property (as opposed to a method)? Bill Davidson Microsoft C# .NET 6 3rd Aug 2005 04:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:07 AM.