When to use delegates?

R

Rich P

Greetings,

I am migrating from VB.Net to C#. In one winform app where I autofill
datefields based on TextBox.Enter and KeyUp events (in VB.Net). I am
now translating this app to C#. The date autofill consists of a user
entering just numbers like 061309. On KeyUp if the field contains 6
characters and they are all numeric - my custom autofill routine will
add forward slashes and combine 20 to the 09 or combine 19 to 99, 89,
...

Would this be a scenario for delegate usage? If so, how would I
implement it? What is the criteria for delegates? All I know is they
involve events.

Thanks

Rich
 
A

Arun

Delegates are special class introduced in C# to implement the function
pointer technique from C/C++. It is type safe and used to call methods
dynamically

It can be well used for:

1. Call back functions
2. Events Handling


I believe your requirement does not require a delegate to solve.

Thanks
--
This will pass too....


Merk said:
Ben Voigt said:
foreach (TextBox txt in arrTxtDates)
{
txt.Leave += Datefields_Format;
txt.KeyUp += DateListfields_KeyUp;
txt.Enter += DateListfields_Enter;
txt.Leave += DateFld_Leave;
}
[snip]

The question that I have not answered for myself yet is if I would
want to apply a delegate to this scenario or not. I understand that

You did use delegates. The code you shows creates four delegates from
four method groups, and registers each as a handler for a different event.
It repeats all that arrTxtDates.Length times.


The article at the following link can further explain what Ben points out.
While the article explains C# events, there is a pretty good presentation of
delegates, and the relationship between delegates and events.

http://www.codeproject.com/KB/cs/event_fundamentals.aspx

Your most frequent usage of delegates would be when implementing events in
your applications. You will rarely think "I need a delegate here" -
independently of events - although it could happen.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top