Delegates...

  • Thread starter Thread starter Colin Basterfield
  • Start date Start date
C

Colin Basterfield

Hi,

I'm trying to convert a VB.NET sample app I found to C# but the delegates
part has caught me out, I have a form which has the following delegates
defined:

public delegate void AddNewEmployee(Employee employee);

There is a method defined that uses this

private void OnAddNewEmployee(Employee employee)

{

lbEmployees.Items.Add(employee);

}

This method can to be passed into a number of forms and consequently has

private AddNewEmployee addNewEmp;

declared within the form class. There is an overloaded constructor which is
declared as:

public frmProgrammer(AddNewEmployee dele) : base()

{

InitializeComponent();

addNewEmp = dele;

}

Now the form that calls this does the following, or rather needs to do the
following:

private void btnAddProgrammer_Click(object sender, System.EventArgs e)

{

frmProgrammer frmProg = new frmProgrammer(OnAddNewEmployee);

frmProg.ShowDialog();

}

Unfortunately the compiler does not like this saying

C:\VSNet Dev\ClientWinApp\frmMain.cs(251): Method
'EmployeeExample.Form1.OnAddNewEmployee(EmployeeExample.Employee)'
referenced without parentheses


The VB.NET sample uses AddressOf, so can someone please tell me how I does
this in C#?

Many thanks, TIA

Colin B
 
Colin Basterfield said:
Hi,

I'm trying to convert a VB.NET sample app I found to C# but the delegates
part has caught me out, I have a form which has the following delegates
defined:

public delegate void AddNewEmployee(Employee employee);

There is a method defined that uses this

private void OnAddNewEmployee(Employee employee)

{

lbEmployees.Items.Add(employee);

}

This method can to be passed into a number of forms and consequently has

private AddNewEmployee addNewEmp;

declared within the form class. There is an overloaded constructor which is
declared as:

public frmProgrammer(AddNewEmployee dele) : base()

{

InitializeComponent();

addNewEmp = dele;

}

Now the form that calls this does the following, or rather needs to do the
following:

private void btnAddProgrammer_Click(object sender, System.EventArgs e)

{

frmProgrammer frmProg = new frmProgrammer(OnAddNewEmployee);
change the above line to:
frmProgrammer frmProg = new frmProgrammer(new
AddNewEmployee(OnAddNewEmployee));
 
Colin Basterfield said:
Hi Daniel,

That of course works, many thanks...

Its a tricky one...I know it confused me at first. If memory serves, C# 2
will provide automatic resolution of delegate types in situations like this,
easing things on the developer, however I am not certain.
 
Back
Top