pass parameters through delegate?

  • Thread starter Thread starter Bob Speaking
  • Start date Start date
B

Bob Speaking

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
 
Bob said:
[...]
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
 
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
 
Thanks guys :)
Your suggestions and methods fit to my needs and help me understand delegate
etc!

Thank you,
Bob
 
Back
Top