473,320 Members | 1,838 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.

pass parameters through delegate?

Hi at all,
Is possible to pass a parameter though a delegate or to override it? (I'm
newbie and I'm trying to understand delegates and their use in a real
scenario)

In my scenario I need to override
System.Text.RegularExpression.MatchEvaluator delegate passing it another
parameter.
For a concrete sample I paste some lines of code :
//my regexpression pattern;
Regex regexer= new Regex("myBeatifulPattern", RegexOptions.Multiline);

//Class MatchEvaluator can be created with a delegate to a custom replacing
function
MatchEvaluator myEvaluator = new MatchEvaluator(CommentMatchHandler);

//this replace the occurrences based on regexer patterns
string result = regexer.Replace(code, myEvaluator);

public string MatchHandler(Match match)

{
//DO SOMETHING on MYVALUE
return MYVALUE
}

Well,
in the above sample I'd like to have "MatchHandler" function like :
public string MatchHandler(Match match, string MYPARAMETER)

but is possible? :)
If I declare the function with the new parameter the code wouldn't compile
(and it's in right... it cannot take parameters in new
MatchEvaluator(CommentMatchHandler);
Thanks,
Bob
Aug 3 '07 #1
3 7132
Bob Speaking wrote:
[...]
Well,
in the above sample I'd like to have "MatchHandler" function like :
public string MatchHandler(Match match, string MYPARAMETER)

but is possible? :)
If I declare the function with the new parameter the code wouldn't compile
(and it's in right... it cannot take parameters in new
MatchEvaluator(CommentMatchHandler);
You are correct, you can't do it in exactly the way you want.

However, there are a couple of alternatives you can use to have a
delegate have access to data that you would otherwise pass as a parameter.

One is to take advantage of the fact that a delegate includes an
instance reference, if the delegate is an instance method. So you can
create a class that holds the data you want, along with a delegate
method in that class that you use for the delegate, and when called the
delegate will have access to that data.

For example:

class DelegateWrapper
{
private string _strParm;

public DelegateWrapper(string strParm)
{
_strParm = strParm;
}

public string MatchHandler(Match match)
{
// do something that uses _strParm
// return whatever
}
}

Where you create an instance of DelegateWrapper and then pass the
MatchHandler of the instance as your delegate.

Another is to use anonymous delegates, which can be placed inline where
you initialize the delegate, allowing the use of whatever data is
visible in that code.

For example:

string strParm;
MatchEvaluator myEvaluator = delegate(Match match)
{
// do something that uses strParm;
// return whatever
};

Making sure, of course, that at some point before the delegate is
called, the strParm variable is initialized to whatever you want.

These are not the only ways to do what you want, but they are IMHO a
couple of the simpler approaches available.

Pete
Aug 3 '07 #2
Hello Bob,
Hi at all,
Is possible to pass a parameter though a delegate or to override it?
(I'm
newbie and I'm trying to understand delegates and their use in a real
scenario)
In my scenario I need to override
System.Text.RegularExpression.MatchEvaluator delegate passing it
another
parameter.
For a concrete sample I paste some lines of code :
//my regexpression pattern;
Regex regexer= new Regex("myBeatifulPattern", RegexOptions.Multiline);
//Class MatchEvaluator can be created with a delegate to a custom
replacing
function
MatchEvaluator myEvaluator = new MatchEvaluator(CommentMatchHandler);
//this replace the occurrences based on regexer patterns string result
= regexer.Replace(code, myEvaluator);

public string MatchHandler(Match match)

{
//DO SOMETHING on MYVALUE
return MYVALUE
}
Well,
in the above sample I'd like to have "MatchHandler" function like :
public string MatchHandler(Match match, string MYPARAMETER)
but is possible? :)
If I declare the function with the new parameter the code wouldn't
compile
(and it's in right... it cannot take parameters in new
MatchEvaluator(CommentMatchHandler);
Thanks,
Bob
Not directly. But you could do this:

public class MatchEvaluatorWithParameter
{
string _parameter;
public MatchEvaluatorWithParameter(string parameter)
{
_parameter = parameter;
}

public string MatchEvaluator(Match match)
{
// use _parameter for your string parameter
// use match to get the info
}
}
Now from the code executing the regex:

Regex regexer= new Regex("myBeatifulPattern", RegexOptions.Multiline);
MatchEvaluatorWithParameter mwp = new MatchEvaluatorWithParameter("your param
here");
MatchEvaluator myEvaluator = mwp.MatchEvaluator;
//this replace the occurrences based on regexer patterns string result
regexer.Replace(code, myEvaluator);

Jesse
Aug 4 '07 #3
Thanks guys :)
Your suggestions and methods fit to my needs and help me understand delegate
etc!

Thank you,
Bob
Aug 4 '07 #4

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

Similar topics

9
by: Wiktor Zychla | last post by:
Hello, I wonder why the delegate declaration needs named parameters? public delegate void MyDelegate( int a, int b ); // ok public delegate void MyDelegate( int, int ); // compiler error...
4
by: yoramo | last post by:
hello can I pass a static method as a parameter to a method? if the answer is yes how do I do that ? how do I call the method ? yoramo.
6
by: VM | last post by:
I'm trying to add multithreading to my win application but I'm having trouble with the code since the method to be threaded has parameters. How can I add multithreading to a method with parameters?...
1
by: VM | last post by:
How can I pass a delegate to another method? In my code (win app), i update my datagrid with the datatable after the method loadAuditFileToTable is finished executing. Instead, I'd like to be able...
8
by: Phill | last post by:
All the event handlers seem to pass an Object and an EventArgs object. If the event doesn't need this info why pass them anyway? It is inefficient.
7
by: SB | last post by:
What is the proper way to pass a character array (char *) from a "C" dll to a C# method (delegate) in my app? Getting the dll (which simulates a third party dll) to call my delegate works fine. ...
4
by: KC Eric | last post by:
Hi all, I have a dll file, it has a class, say: class Temp, this class has a function which has a delegate as a parameter, say: public void Test(GameOverHandler _overHandler)
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.