473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Delegates...

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(EmployeeEx ample.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
Nov 15 '05 #1
3 1124

"Colin Basterfield" <co**************@hotmail.com> wrote in message
news:um**************@TK2MSFTNGP09.phx.gbl...
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));
frmProg.ShowDialog();

}

Unfortunately the compiler does not like this saying

C:\VSNet Dev\ClientWinApp\frmMain.cs(251): Method
'EmployeeExample.Form1.OnAddNewEmployee(EmployeeEx ample.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

Nov 15 '05 #2
Hi Daniel,

That of course works, many thanks...

Colin B

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:eE**************@TK2MSFTNGP12.phx.gbl...

"Colin Basterfield" <co**************@hotmail.com> wrote in message
news:um**************@TK2MSFTNGP09.phx.gbl...
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));
frmProg.ShowDialog();

}

Unfortunately the compiler does not like this saying

C:\VSNet Dev\ClientWinApp\frmMain.cs(251): Method
'EmployeeExample.Form1.OnAddNewEmployee(EmployeeEx ample.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


Nov 15 '05 #3

"Colin Basterfield" <co**************@hotmail.com> wrote in message
news:OM**************@TK2MSFTNGP11.phx.gbl...
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.
Colin B

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:eE**************@TK2MSFTNGP12.phx.gbl...

"Colin Basterfield" <co**************@hotmail.com> wrote in message
news:um**************@TK2MSFTNGP09.phx.gbl...
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));
frmProg.ShowDialog();

}

Unfortunately the compiler does not like this saying

C:\VSNet Dev\ClientWinApp\frmMain.cs(251): Method
'EmployeeExample.Form1.OnAddNewEmployee(EmployeeEx ample.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



Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the...
3
by: Sam | last post by:
I’m just starting to learn delegates. I’m at the very beginning. If I understand correctly, delegates are for when you want to pass a function as a parameter. For example the client provides a...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
4
by: AMDRIT | last post by:
I am trying to understand Delegates and where/when to use them. I can see one potential use of a delegate (on form closing, set the cancel property in the event arguments.) Does anyone have a...
6
by: =?Utf-8?B?Sko=?= | last post by:
I have a logger component that logs to multiple sources, ie textfile, eventlog etc. and I have two methods that depending on where I call up my logger comp. one of them will be called. For ex. if...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
6
by: =?Utf-8?B?T2xkQ2FEb2c=?= | last post by:
My question is regarding the use of delegates in C#. I see how .Net uses delegates to wire event handlers to events. It’s an object created by a single line of code by the system and that makes...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.