C# constructor event

后端 未结 6 768
南旧
南旧 2021-01-21 03:53

i created a class and when i create an employee object via form , i want to give a message;

this is my class, event and delegate

public delegate void cto         


        
6条回答
  •  囚心锁ツ
    2021-01-21 04:36

    It doesn't make sense to raise an instance event in the constructor, because since the initialization of the instance is not yet complete, there can't be any handler attached to the event...

    However, you could create a static event:

    public static event ctorDel myEvent;
    
    ...
    
    Employee.myEvent += new ctorDel(showMessage);
    

    (but don't subscribe to the event every time you create an Employee, or the handler will be invoked as many times as there are instances...)

提交回复
热议问题