0

To not have the obligation of instanciating for each Form a new class I would like to have it static. My problem is that it's a class that regroup all the signature fonction that defines my delegates used in my Code (for all form when I need it). As Delegate are function pointers I'd like to know if declaring the delegate inside a static class is possible, I mean it works I'va tried but I want to know if there is a known problem about it. Here is the code:

 public static class DEL_Prototype
    {
        public delegate void DEL_delegate1(Double AValue);
    } 

and I call it :

 DEL_Prototype.DEL_delegate1 += new DEL_delegate1(myfunctiontopoint);

EDIT: Here is the broader delegate use (for instance a debugger to know how much subscriber are attached to each delegates or anything else that contains ALL the delegates used) Code

   public partial class Form1 : Form
    {
        private DEL_Prototype.DEL_delegate1 m_SetValueCbk;
        private Form2 FormwithLabel;
        public Form1()
        {
            InitializeComponent();         
            FormwithLabel = new Form2(this);
            FormwithLabel.Show();
        }
        public event DEL_Prototype.DEL_delegate1 SetValueCbk
        {
            add { m_SetValueCbk += value; DEL_Prototype.InvokationListChanged(1, m_SetValueCbk); }
            remove { m_SetValueCbk -= value; DEL_Prototype.InvokationListChanged(-1, m_SetValueCbk); }
        }
    }

Form2

public partial class Form2 : Form
{
    private Form1 ThisForm1;
    public Form2() { }
    public Form2(Form1 Form1link)
        : this()
    {
        ThisForm1 = Form1link;
        InitializeComponent();
    }
    protected void SetValueCbkFN(Double value)
    {            ;        }
    private void button1_Click(object sender, EventArgs e)
    {
        ThisForm1.SetValueCbk += new DEL_Prototype.DEL_delegate1(this.SetValueCbkFN);
    }
    private void button2_Click(object sender, EventArgs e)
    {
        ThisForm1.SetValueCbk -= new DEL_Prototype.DEL_delegate1(this.SetValueCbkFN);
    }
}
public static class DEL_Prototype
{
    public delegate void DEL_delegate1(Double AValue);

    public static void InvokationListChanged(int dir, Delegate Name)
    {
        string msg = dir < 0 ? "Someone unsubscribed from the event" : "Someone subscribed to the event";
        if (Form1.ActiveForm.InvokeRequired)//thread safe implementation
        {//if control created in the same thread that label1 its else otherwise invoke method the change text asynchronously
            msg = string.Concat(msg + " subscribe number: " + "0");
            Form1.ActiveForm.Invoke(new MethodInvoker(() => { MessageBox.Show(msg); }));
        }
        else
        {
            if (Name != null) msg = string.Concat(msg + " subscribe number: " + Name.GetInvocationList().Count().ToString());
            else msg = string.Concat(msg + " subscribe number: " + "0");
            MessageBox.Show(msg);
        }
    }
} 

Does it make sens?

Thanks a lot.

JudgeDreed
  • 143
  • 1
  • 13

0 Answers0