Click here to Skip to main content
15,910,083 members
Home / Discussions / C#
   

C#

 
GeneralRe: Generic event, or raise event based on generic type. Pin
Gideon Engelberth12-Jan-09 11:58
Gideon Engelberth12-Jan-09 11:58 
AnswerRe: Generic event, or raise event based on generic type. Pin
S. Senthil Kumar12-Jan-09 9:55
S. Senthil Kumar12-Jan-09 9:55 
GeneralRe: Generic event, or raise event based on generic type. Pin
DaveyM6912-Jan-09 10:07
professionalDaveyM6912-Jan-09 10:07 
GeneralRe: Generic event, or raise event based on generic type. Pin
Mark Churchill12-Jan-09 16:45
Mark Churchill12-Jan-09 16:45 
GeneralRe: Generic event, or raise event based on generic type. Pin
S. Senthil Kumar12-Jan-09 18:40
S. Senthil Kumar12-Jan-09 18:40 
GeneralRe: Generic event, or raise event based on generic type. Pin
Mark Churchill12-Jan-09 19:01
Mark Churchill12-Jan-09 19:01 
QuestionRe: Generic event, or raise event based on generic type. Pin
Wendelius12-Jan-09 11:16
mentorWendelius12-Jan-09 11:16 
AnswerRe: Generic event, or raise event based on generic type. [modified] Pin
DaveyM6912-Jan-09 22:58
professionalDaveyM6912-Jan-09 22:58 
Hi Mika, not dumb questions at all!

Mika Wendelius wrote:
can it be any type, even a class that the singleton isn't aware of?


Ideally yes, but using a Singleton, I couldn't find a way of making it work.

Mika Wendelius wrote:
If the type is known to the singleton, wouldn't this lead to hardcoding the possible types... this is what you're trying to prevent.


Correct, and correct - hence the question.

Mika Wendelius wrote:
wouldn't it be sufficient to carry the type info in custom eventargs without generics


Yes, but I'd have to box/unbox to and from object. I thought it's be nice (if possible) to do it with generics. As you can probably guess, I've not needed/used generics much apart from List<T> so I'm learning as I go a little trying to get this concept to work Smile | :)

I think the only way to do this is to have seperate static instances of each type, instead of one singleton.

To explain more clearly, here's some working code. I've used a winforms app where two forms receive and send notifications. I've used the static Program class to create static instances of Notifier<T> (instead of a singleton) that the notifiction can be done through application wide. Sorry for the long post.

Edit: The code below is improved by creating a GlobalNotifier singleton, and adding the Notifier<T> instance fields and properties to there (instead of Program class) as needed. Still gotta hard code the types somewhere though Frown | :(
// TEventArgs.cs
using System;
public class TEventArgs<T> : EventArgs
{
    private T m_Value;
    public TEventArgs(T value)
    {
        m_Value = value;
    }
    public T Value
    {
        get { return m_Value; }
    }
}
// Notifier.cs
using System;
public class Notifier<T>
{
    public event EventHandler<TEventArgs<T>> Notification;
    public void Notify(T value)
    {
        OnNotify(new TEventArgs<T>(value));
    }
    protected virtual void OnNotify(TEventArgs<T> e)
    {
        EventHandler<TEventArgs<T>> eh = Notification;
        if (eh != null)
            eh(this, e);
    }
}
// Program.cs
using System;
using System.Windows.Forms;
static class Program
{
    [STAThread]
    static void Main()
    {
        Application.Run(new Form1());
    }
    private static Notifier<int> intNotifier = new Notifier<int>();
    private static Notifier<string> stringNotifier = new Notifier<string>();
    public static Notifier<int> IntNotifier
    {
        get { return intNotifier; }
    }
    public static Notifier<string> StringNotifier
    {
        get { return stringNotifier; }
    }
}
// Form1.cs
using System;
using System.Windows.Forms;
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Form2 frm2 = new Form2();
        Program.IntNotifier.Notification += new EventHandler<TEventArgs<int>>(IntNotifier_Notification);
        Program.StringNotifier.Notification += new EventHandler<TEventArgs<string>>(StringNotifier_Notification);
        Program.IntNotifier.Notify(5);
        Program.StringNotifier.Notify("This rocks!");
    }
    void IntNotifier_Notification(object sender, TEventArgs<int> e)
    {
        Console.WriteLine(e.Value);
    }
    void StringNotifier_Notification(object sender, TEventArgs<string> e)
    {
        Console.WriteLine(e.Value);
    }
}
// Form2.cs
using System;
using System.Windows.Forms;
public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        Program.IntNotifier.Notification += new EventHandler<TEventArgs<int>>(IntNotifier_Notification);
    }
    void IntNotifier_Notification(object sender, TEventArgs<int> e)
    {
        Program.StringNotifier.Notify("Got the integer " + e.Value);
    }
}


Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

modified on Tuesday, January 13, 2009 5:13 AM

GeneralRe: Generic event, or raise event based on generic type. Pin
Wendelius13-Jan-09 9:47
mentorWendelius13-Jan-09 9:47 
GeneralRe: Generic event, or raise event based on generic type. Pin
DaveyM6914-Jan-09 0:23
professionalDaveyM6914-Jan-09 0:23 
GeneralRe: Generic event, or raise event based on generic type. Pin
Wendelius14-Jan-09 2:30
mentorWendelius14-Jan-09 2:30 
GeneralRe: Generic event, or raise event based on generic type. Pin
DaveyM6914-Jan-09 3:25
professionalDaveyM6914-Jan-09 3:25 
GeneralRe: Generic event, or raise event based on generic type. Pin
Wendelius14-Jan-09 3:42
mentorWendelius14-Jan-09 3:42 
GeneralRe: Generic event, or raise event based on generic type. Pin
DaveyM6914-Jan-09 11:41
professionalDaveyM6914-Jan-09 11:41 
GeneralRe: Generic event, or raise event based on generic type. Pin
Wendelius14-Jan-09 11:59
mentorWendelius14-Jan-09 11:59 
GeneralRe: Generic event, or raise event based on generic type. Pin
DaveyM6915-Jan-09 3:13
professionalDaveyM6915-Jan-09 3:13 
GeneralRe: Generic event, or raise event based on generic type. Pin
Wendelius15-Jan-09 3:19
mentorWendelius15-Jan-09 3:19 
GeneralRe: Generic event, or raise event based on generic type. Pin
DaveyM6915-Jan-09 4:01
professionalDaveyM6915-Jan-09 4:01 
GeneralRe: Generic event, or raise event based on generic type. Pin
Wendelius15-Jan-09 10:27
mentorWendelius15-Jan-09 10:27 
GeneralRe: Generic event, or raise event based on generic type. [modified] Pin
DaveyM6915-Jan-09 23:08
professionalDaveyM6915-Jan-09 23:08 
GeneralRe: Generic event, or raise event based on generic type. Pin
Wendelius15-Jan-09 23:25
mentorWendelius15-Jan-09 23:25 
QuestionKeep current view centered on Zoom Pin
Richard Blythe12-Jan-09 7:16
Richard Blythe12-Jan-09 7:16 
AnswerRe: Keep current view centered on Zoom [modified] Pin
Luc Pattyn12-Jan-09 8:01
sitebuilderLuc Pattyn12-Jan-09 8:01 
GeneralRe: Keep current view centered on Zoom Pin
Richard Blythe12-Jan-09 9:10
Richard Blythe12-Jan-09 9:10 
QuestionAddress Pin
boiDev12-Jan-09 6:56
boiDev12-Jan-09 6:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.