Click here to Skip to main content
15,888,401 members
Everything / Delegates

Delegates

delegates

Great Reads

by Sergey Alexandrovich Kryukov
Derived work based on the article by Sergey Ryazanov "The Impossibly Fast C++ Delegates": this good solution is fixed and further developed using C++11.
by David Lafreniere
A C++17 standards compliant delegate library capable of targeting any callable function synchronously or asynchronously
by Anton Chibisov
This tutorial showcases how to implement C++ delegates which are capable of being bound to methods and functions having arbitrary signature, i.e., any number and type of parameters and return value.
by Muraad Nofal
A haskell monad/(applicative)functor like interface in C# that extends IEnumerable.

Latest Articles

by David Lafreniere
A C++ standards compliant delegate library capable of targeting any callable function synchronously or asynchronously
by David Lafreniere
A C++17 standards compliant delegate library capable of targeting any callable function synchronously or asynchronously
by yutaraa
Get, move, copy and delete event handler delegates of Windows form controls.
by Jon McKee
How to do it and why it works

All Articles

Sort by Updated

Delegates 

23 Mar 2018 by #realJSOP
Google is your friend. Anonymous Functions (C# Programming Guide) | Microsoft Docs[^]
11 Dec 2017 by ________________
You do not need to create delegate twice: public static bool CheckisOdd(int s) { if (s % 2 == 0) { return false; } else { return true; } }
15 May 2012 by abhinav_soni
Support for Delegates, Events and Pass by ref in Java using bytecode manipulation
21 May 2016 by Abtin Bina
i have an object that it have a 'Thread' property like this class Client{ public Thread ClientThread; // it is that i want to change //some property //some method }how can i set the ClientThread in a started threadlike what i have triedWhat I have...
30 Nov 2013 by agent_kruger
i would like to know the concept of Delegates and lambda.Example code will be appriciated. Thanks In Advance.
6 Jan 2017 by Akhil Mittal
This article of the series "Diving into OOP" will explain all about events in C#. The article focusses more on practical implementations and less on theory.
11 Jul 2016 by Alberto Nuti
In a console application, there is often the need to ask (and validate) some data from users. For this reason, I have created a function that makes use of generics and delegates to speed up programming.
23 Jan 2018 by Ammar Shaukat
I'm working on a software where software issues commands for hardware panel and once a command is issued, its response received after few seconds . there are different functions for different hardware commands like public void FunctionA() { StartCommandA(); } and other functions on the...
23 Jan 2018 by Ammar Shaukat
Make a list of Action Delegates and add Status Code for each function . let's say 0 mean function has invoked and 1 mean function is not invoked yet. then use LINQ Query to check which function has called and which one is remaining. Change the status of function invocation from OnReponse ...
3 Feb 2015 by andrea contoli
Presentation and testing of some procedural bitmap creation algorithms .
12 Jul 2016 by Anisuzzaman Sumon
Dear Experts,Just let me know is the following code executed as asynchronously ?My Code is shown belowpublic class CompanyController : Controller {public delegate void CompanyAuditRecordKeeper(CompanyModel company, string operationType); public ActionResult Create(CompanyModel...
13 Apr 2015 by Anton Chibisov
This tutorial showcases how to implement C++ delegates which are capable of being bound to methods and functions having arbitrary signature, i.e., any number and type of parameters and return value.
9 Dec 2011 by awaisdar
consider me a very beginner and please answer the q.i headed that it is use to reference methods and events.one thing is fine, that we invoke event whenever we want grammatically. but what about methods?i think there is some threading concept?Asynchronous and Synchronous...
9 Dec 2011 by awaisdar
i have some ques.i have understood the concept of handling the event using delegates (as some extent not whole). but there is some thing else here.Asynchronous and Synchronous Methods.1.What is the difference between the two? simple example.2. i have a method that takes 30 mint to complete,...
3 Nov 2015 by Ayesha_K
Somebody suggested me to do it with Jquery . I don't know where to start from but the scenario is that I need an online shopping store website,in which on clicking on 'ORDERNOW" button on "Add to cart"'s window ,the notification is pushed on admin panel which tells about that particular order's...
4 Dec 2014 by barneyman
the easiest approach would be to write a COM object in the DLL - create an ATL simple object with a methods (say) HRESULT Register(IUnknown *eventCallback), HRESULT UnRegister(void)(don't bother with dual/dispatch, custom is fine, but make it automation safe)Use tlbimp (on the generated)...
15 Jan 2015 by Bhushan Mulmule
This write-up will take you through all the steps that are required to understand lambda expression thoroughly.
4 Oct 2016 by BillW33
As _duDE_ said, if you send data packets asynchronously there is no way to know the order in which they will be received. What you have to do on the receiving end is to reassemble the packets into the correct order. To do this they must be sent with some field that indicates the correct order....
5 Apr 2012 by bobishkindaguy
SOLVED - PLEASE SEE COMMENTS BELOW IF YOU ARE INTERESTED IN HOW THIS WORKED OUT, SINCE IT IS A 2-PART SOLUTION, BOTH OF WHICH ARE POTENTIALLY USEFUL TO OTHERS.Often, users will go to my program's "import data" window to initiate an import of data from a file or spreadsheet to our database....
7 Dec 2018 by C Pottinger
Hello Code Project Gurus I know it is possible to have delegates operate with values that are known at the time the delegate is called - that's simply a parameter e.g. Func areaOfCircle = r => Math.PI * Math.Pow(r, 2); Func diameterOfCircle = r => 2 *...
5 Jun 2018 by Chander Parkash Mourya
DataReceived event should be subscribed before calling Open() method of SerialPort class. As per your result event is called many times- In my case event will be called only in case of any response comes from another paired port. For ex. Paired ports are - "COM5" "COM3" so when any...
30 May 2014 by CHill60
ProgramFOX has solved your problem, however you may also find this CodeProject article useful - C# - Delegates 101 - A Practical Example[^]
29 Dec 2012 by Christian Graus
You can't do anything OO over the network, because you have two different exes running. You can't use delegates between two exes on two machines.
2 Jun 2014 by CPallini
Here you are (see MSDN[^]) static void Main(string[] args) { Democlass d = new Democlass(); ExampleDelgate e1 = new ExampleDelgate(d.Add); ExampleDelgate e2 = delegate(int x, int y){ Console.WriteLine(x+y);}; // anonymous method e1(20, 10); e2(20,...
4 Feb 2019 by CPallini
As far as know, without considering 'dirty hacks' (trying to defeat the strongly typed nature of C++), you cannot do that. I would use just one function signature and then adapt all of my functions to comply with it. Within this approach you might possibly find useful std::function[^].
20 May 2012 by cpsglauco
How to use delegate in List.Find() predicate in C#
29 Dec 2012 by Cyberbird85
Hello everyone!I'm trying to develop an application in c# i currently have a code similar to the one attached below.Basically what it does is:Having multiple form, each of which would like to send message through the network, using my NetworkCommunicator class's SendMessage method.The...
30 Dec 2013 by Daniel Brousser
solved by using interfaces, thanks
29 Dec 2013 by Daniel Brousser
the system:C# object ---> C++ CLI bridge ---> C++ native code.some functionality of the C# object, uses C++ objects (via CLI)I need to pass a delegate of a member C# function, all the layers down, to C++,so that C++ can call it when needed.Is this even possible?thanks.
26 Mar 2017 by Danilow
Simulating C# event handlers in Java
6 Dec 2018 by Dave Kreskowiak
You cannot change the delegate definition at runtime. A delegate is nothing but a pointer to a function. The parameter list of the delegate must match the parameter list of the function that it points to. If it doesn't you can unbalance the stack as the call is pushing parameters on the stack...
19 Dec 2022 by David Lafreniere
A C++17 standards compliant delegate library capable of targeting any callable function synchronously or asynchronously
19 Dec 2022 by David Lafreniere
A C++ standards compliant delegate library capable of targeting any callable function synchronously or asynchronously
30 Aug 2013 by Dev Leader
Let’s see how events might be causing some leakage in your application.
11 Dec 2017 by dhivya N
I am a new learner of c#. I want to find a number is odd or not using delegate anonymous function. I am getting the the above error. Where am i going wrong? What I have tried: public delegate bool oddOREvenDelegate(int s); class Program { static void Main(string[] args) ...
10 Sep 2014 by DiponRoy
Let's make a lambda expression from a property name of a particular entity, and use it for OrderBy shorting
27 Mar 2014 by Divakar Raj M
I am trying to interactively change button properties in the UI visually. I have a sole button in my Windows Forms application and this is my code for the button clickprivate void button1_Click(object sender, EventArgs e) { button1.BackColor = Color.Red; ...
4 Jul 2015 by Dominic D Williams
The power of delegates outside of events.
6 Oct 2021 by dotarsoyak
Hello I have an implementation of Flutter Navigator 2.0 in my app, wich one has a SearchDelegate implemented too, but when I do tap on the search box search button, my app launch an error, the console prints this message: I think I have a bad...
8 Nov 2015 by DotNetSteve
Using delegates to group, conquer cross cutting concerns and create dynamic validators
8 Nov 2015 by DotNetSteve
Using delegates to group, conquer cross cutting concerns and created dynamic validators
5 Mar 2017 by DotNetSteve
Creating an extension class for View Models to save public properties using Generics and Delegates, replacing a reflection implementation
18 Jan 2017 by Doug Langston
A Visual Studio 2015 project that shows one way to pass information between Windows Forms
4 Jul 2012 by ErickTreeTops
i'm trying to understand delegates and events and wrote a simple app based on an example.namespace DelegateTest{ public partial class Form1 : Form { BroadcastClass B = new BroadcastClass(); ListenToClass1 L1 = new ListenToClass1(); ...
6 Jan 2012 by Estys
Yes, your second "if" is messing things up.Try If x.Text = "" Then x.Focus() ErrPvdr.SetError(x, "Cannot be a blank input") ElseIf x.Name = "txtUprice" And (Not IsNumeric(x.Text)) Then x.Focus() ErrPvdr.SetError(x, "Cannot...
16 Dec 2021 by Eugene Rutsito
I have four (4) class libraries in c#. Three (3) (InitiatorModule, TransactorModule, LoggerModule) of which are higher level and one (1) (PublisherModule) is a lower level. All the three higher level projects have project reference to the lower...
23 Apr 2018 by F-ES Sitecore
You need to tell the content page what the specific type of the master page is before you can use any public properties\methods on that master page. This can be done using the MasterType derective Working with ASP.NET Master Pages Programmatically[^] Master page has this dropdown ...
2 Jun 2020 by Gauravg9598
I want to know that where delegate in c# has been used in Dot Net Projects. How we can used delegate in Dot Net Projects. How to that in project where we use delegate What I have tried: I want to know that where delegate in c# has been used in...
29 Nov 2015 by George Jonsson
I think the problem is that you when you pass the event argument, you don't pass it is a value but as a reference. See Parameter passing in C#[^]As you only create one instance of MessageEventArgs me = new MessageEventArgs();, you only have one place in the memory where your data is...
11 Dec 2017 by George Swan
public static void Main (string[] args) { //define a delegate that takes an int and returns a bool Predicate CheckIsOdd=i=>i%2!=0; //call the delegate and write the result Console.WriteLine(CheckIsOdd(5)); //An alternative approach using an anonymous function...
19 Aug 2017 by Graeme_Grant
Expanding on Mehdi Gholam's solution, here is a real-life example of where the method ProcessFileType "discovers" the type of data that it needs to convert to, looks up the type of data, then executes the conversion from JSON string and returns a class structure of a specific type. // only some...
16 Dec 2018 by Graeme_Grant
The UI runs in the main thread, so you can't access the UI from a different thread. So you have a flag that the Spinner Visibility is bound to using data binding, say is called IsBusy. Then you communicate to the UI via the flag. Secondly, as OriginalGriff mentioned, you then offload the...
17 Aug 2023 by hans.sch
I'm playing with C++/CLI and calling into a .NET assembly. There is no specific use case, I simply want to learn. I wrote the assembly in C#. It exports a class with a few methods, and a delegate type which is used in one of the methods:...
2 Nov 2011 by HAOYE850815
Dear guys, I am currently working on a GPS tracking software. It should implement a function to trigger GPS data logging if GPS enter in a specific area and stop logging after going out of boundary.Anyway, My problem is if I select GPS as a observable object and make a GPSMonitor class...
22 Apr 2018 by helpwithmycode
INFORMATIVE BACKGROUND: I have a ddl control in master site, i would like to call the 'selectIndexChange event' that sits in master page, and i want to call it in a content page. the reason is because i have an adio.net function that populates a grid in my content page. when i select a new item...
14 Nov 2013 by Imadoddin
Hi, I have a masterpage in which I have dropdown list, I want to handle SelectedIndexChanged Event of that dropdown list in cantent page.. So far I didMasterPage:Public Event SelectedIndexChange(ByVal sender As Object, ByVal e As System.EventArgs)Public Shared Sub...
6 Jan 2015 by Inimicos
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace delegateexample{ delegate int somedelegate(int i); class someclass { public static int somemethod(int i) { return i...
4 Oct 2017 by İsmail Fatih ILTAR
Hi, i have to write C# class and method from C codes. My C codes are below. function_ex takes function pointer which takes custom_pac_type_t pointer.Problem is about method declaration. i can't use Func cause there is a ref parameter. I must use delegates but i couldnt. How can i pass func...
28 Jun 2013 by Jeneesh K. Velayudhan
27 Jun 2013 by Jeneesh K. Velayudhan
I have one Initialize() in my class and I am calling a delegate wrapper method from this function as follows (the call back method will call around 20 times in a sec),public Initialize(){ CX_OUTPUT_CALLBACK cb = new CX_OUTPUT_CALLBACK(OutDelegateCb); ...
14 Nov 2013 by Jobless Creature
This Link may be of help...Save & Exit in Web Application[^]
23 Jan 2018 by johannesnestler
have a look at the Task class Task Class (System.Threading.Tasks)[^] and to the ContinueWith methods. This is what you want: call methods each in it's own Task (thread) - wait for method to execute while not blocking the rest of your app. After executing continue with next method call (in it's...
25 Jan 2015 by John L. Vidal
Events in .NET are great. There is the "event" keyword that gives a fast way to start to use them, but..
12 Nov 2013 by John Pravin
Asynchronous Programming with Task Parallel Library.
1 Dec 2017 by Jon McKee
How to do it and why it works
25 Aug 2015 by K. Naveen. Bhat
The article shows how we can solve various technical problems easily with the help of delegates.
17 Nov 2014 by KarstenK
It is an easy task. Look at this code from Microsoft.You could ofcourse use your dll and function.
5 Dec 2014 by KarstenK
I have added callback support to my article Calling all stations which handles now also callbacks from and into C++. Step through the code and see my solutions.
30 Nov 2013 by Karthik_Mahalingam
Try these linkmight be useful for you.C# Delegates, Anonymous Methods, and Lambda Expressions – O My![^]this link contians basics of lambda very informative...
4 Oct 2016 by Leo Chapiro
If you send and receive the data asynchronous, there is no guarantee to get this data in the same sequence as sent.
8 Nov 2014 by Mahdi K.
A detailed description for Delegate, Func, Predicate, Action and Lambda Expression.
26 Mar 2012 by Manjunathabe011
Hi All,I have a scenario where I need to do double hop authentication. I want to Impersonate the Users in WCF service layer but want to pass the same Windows credentials of Client to DAL. My DB needs windows authentication. How can I achieve this. Thanks,Manju
24 Jul 2014 by Marisco
I'm having a tough time getting the result from my controller, it has a function that contains a delegate funciont passint as a parameter.so the result is awais empty and then goes back after its finished.HTLMName:
15 May 2020 by martonthenagy
This may be useful for You: .net - Call has been made on garbage collected delegate in C#? - Stack Overflow[^]
6 Jan 2015 by Matt T Heffron
If I understand your question, you've almost got it correct in your code above: class Program { static void Main(string[] args) { somedelegate process = new somedelegate(someclass.somemethod); // NOTE: no parens () process(4); } ...
15 May 2020 by MAU787
hi alli have one application which calls one function from dll.that dll invokes one delegate for capturing fingerprint.but after 4-5 capture i m getting error..A callback was made on a garbage collected delegate of type...
18 Aug 2023 by Maxim Kartavenkov
had you try the next way: int MyCallback(int i) {} public delegate int IntCallback(int i); IntCallback^ callback = gcnew IntCallback(MyCallback); // Use callback variable as an argument
11 Mar 2014 by mbarbac
Implementing the Repository and Unit of Work Patterns dynamically using Kerosene ORM
3 Dec 2014 by mccaber82
Hi,I come from a .net background and I am fairly new to developing in C++. I am building a library which carry's out low level functionality, wrapped by a C# library so it can be used in other projects.I am well used to events in C# and raising events when something meaningful happens. ...
23 Dec 2012 by Mehdi Gholam
Posted from the comment:Check if the "optimize code" is set in your project.
14 Jun 2013 by Mehdi Gholam
Read the following :http://msdn.microsoft.com/en-us/library/ms228976.aspx[^]http://msdn.microsoft.com/en-us/library/system.reflection.eventinfo.addeventhandler.aspx[^]
18 Aug 2017 by Mehdi Gholam
First don't use reflection unless you need it and you know what you are doing, so if your method counts is low use a switch statement: switch(comm.Method) { case "method1" : this.Method1(comm.Data); break; ...// and so on } You can also use a Dictionary with...
27 May 2014 by Member 10755393
hi this vijayi have 2dropdownlist controls in usercontrols and if i selected dropdownvalues iwant to show that values in aspx textbox controls using delegates and events,iwant clear code iam new for .net
30 May 2016 by Member 12555296
I have create a small class CountDown , that contains a timer.Class is very simple : receive time target, and start a CountDown with a timer.When target is reached, my personal event fireusing System;using System.Collections.Generic;using System.Linq;using System.Text;using...
26 Jul 2020 by Member 14686752
The client is in c# and the server is in C++. Client is as follows: client.cs namespace clinetspace { public class client { [DllImport("Server.dll")] static public extern void CallServer(IntPtr obj, int a); ...
5 Apr 2018 by Member 1960377
I am working on a window application (written using Vb.net).In the main form Load event another form 'frmcrew_managment' is also loaded.In this stage for RefreshClick event of Main form attaches 'Refreshadata' method ( handler) of 'frmcrew_managment' . As a result after the main form is loaded...
4 Oct 2016 by Member 7754814
Hi,I am facing issue while consuming data in event in window application. Data received is not in same sequence as sent from class where event is raised. Here is complete scenario.There are 2 threads. In one thread, I receive data packets over TCP continuously and put it in array. In other...
19 Sep 2011 by Member 7835126
Hello!I'm trying to use a async Delegate to show and populate a form but the form show up and stays Not Responding.Normally the form opens OK when it's sync with the code.Example situation:Before Class:Public Delegate Sub MyAsyncDelegate()In primary sub:Dim del As...
20 Oct 2013 by Member 8392278
Does anyone have a simple way of making this TreeView WinForm app multithreaded – the UI locks up. I have experimented with the backgroundworker , treeview.invoke and delegates but need help in these areas. Thank you in advance for any assistance.
13 Apr 2012 by Member 8404471
Hey everyone,I'm trying to send the value from a serial port via myPort.DataReceived() to a textlabel on my windows form. Error is of course, that I can't access the control from a different thread. I know i need to use invoke for that but can't get it right. I tried this:void...
2 Apr 2016 by Member 9346617
lets' say I have 2 buttons. Add & Substract.I will write two seperate functions "Add" & "Substract" & i will call them accordingly on each button click.Then why should I go for delegate when things are already known to me in advance which function to call on which button click ?What I...
19 Dec 2011 by Mich_90
I would be grateful if you help me to find the answer.I have started working with ASP.NET controls and there appeared a question: "why events in ASP controls are defined as delegates, and not as methods"? Thank you in advance.
14 Jun 2013 by Michelle Phoenix
Hello,I am building a form which has various controls on it, and each control has various methods that are handled, such as onLoad, Click etc. When certain methods are fired, I need to write a log entry that that specific control's _something method was fired.However, instead of adding...
27 Jun 2013 by MK-Gii
Hello Everyone, hello Tom,I got this solved and the link you gave me was very uselfull (the for delegates in MSDN)The solution for this was that I passed delegate to the function instead of passing the function name. And before that I assign the function to delegate like this:private...
26 Jun 2013 by MK-Gii
Hello Guys,I am having a hard time with passing a function name as a parameter to another function.I have the following setup:1. There are 2 classes (A and B)2. I am trying to write a function in class A which would be executing methods from class B.I know I can pass a class instance...
29 Oct 2013 by Mohammad Sharify
Hi allI use a class to write opengl program in C++,I want to use glutDisplayFunc(display)and this my display function:void graphic::dispaly(){ glClear(GL_COLOR_BUFFER_BIT); //call a function glutSwapBuffers();}Now ,I face this error:3 IntelliSense: argument of type...
18 Sep 2013 by Muraad Nofal
A haskell monad/(applicative)functor like interface in C# that extends IEnumerable.
19 Aug 2017 by MYQueries1
I am using the Invoke method to call method using reflection. Performance of this not great. Is there any other solution What I have tried: void ExecuteMethodViaReflection(TestObj comm) { var paramNames = comm.paramNames; //of type string array var paramData = comm.Data; //of type...
30 May 2017 by n.podbielski
Explanation how to convert from one delegate type to second delegate of unknown or dynamically created type when their signatures match.
5 Jan 2012 by Nara Har
Hello All,I have a form and I want to ensure that none of the input fields should be left blank and the txtUPrice should hold only numeric values.The below is the code that I devised.Initially when I checked for focus issues (Initial IF structure) it went fine. But when I...