C# constructor event

后端 未结 6 817
南旧
南旧 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:19

    You can pass the handler when creating the Employee:

    private Employee(ctorDel construcEvent)
    {
        if (construcEvent != null)
            this.myEvent += construcEvent;
    }
    
    public Employee(int empID,string empName, ctorDel construcEvent)
        : this(construcEvent)
    {
        this.empID = empID;
        this.empName = empName;
    
        if (myEvent != null)
        {
            myEvent();
        }
    }
    

    And then:

    Employee emp = new Employee(id, name, new ctorDel(showMessage));
    

提交回复
热议问题