Results 1 to 3 of 3

Thread: [RESOLVED] [2.0] Generic Event

  1. #1

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Resolved [RESOLVED] [2.0] Generic Event

    Consider this class/delegate:
    Code:
    public delegate void MessageHandler(Message message);
    
    public class CallbackSink : MarshalByRefObject
    {
      public event MessageHandler clientMethod;
    
      [OneWay] public void InvokeClient(Message message)
      {
        if (clientMethod != null)
          clientMethod(message);
      }
    }
    I'd really like to be able to do this:
    Code:
    public class CallbackSink<T> : MarshalByRefObject
    {
      public event T clientMethod;
    
      // ...
    —but it won't let me ("event must be of a delegate type").

    Is it possible at all to make the delegate type generic?

    Cheers

    - P
    Last edited by penagate; Sep 20th, 2007 at 12:41 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Generic Event

    You can apply constraints to generic type parameters but I can't think of a valid constraint for that condition.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Generic Event

    Actually, it was a silly problem. I should have realised that you're supposed to put all of the events into the event sink class instead of making a class for each one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width