pass parameters through delegate?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Speaking

    #1

    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.Reg ularExpression. MatchEvaluator delegate passing it another
    parameter.
    For a concrete sample I paste some lines of code :
    //my regexpression pattern;
    Regex regexer= new Regex("myBeatif ulPattern", RegexOptions.Mu ltiline);

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

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

    public string MatchHandler(Ma tch match)

    {
    //DO SOMETHING on MYVALUE
    return MYVALUE
    }

    Well,
    in the above sample I'd like to have "MatchHandl er" function like :
    public string MatchHandler(Ma tch 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( CommentMatchHan dler);


    Thanks,
    Bob


  • Peter Duniho

    #2
    Re: pass parameters through delegate?

    Bob Speaking wrote:
    [...]
    Well,
    in the above sample I'd like to have "MatchHandl er" function like :
    public string MatchHandler(Ma tch 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( CommentMatchHan dler);
    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(Ma tch 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

    Comment

    • Jesse Houwing

      #3
      Re: pass parameters through delegate?

      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.Reg ularExpression. MatchEvaluator delegate passing it
      another
      parameter.
      For a concrete sample I paste some lines of code :
      //my regexpression pattern;
      Regex regexer= new Regex("myBeatif ulPattern", RegexOptions.Mu ltiline);
      //Class MatchEvaluator can be created with a delegate to a custom
      replacing
      function
      MatchEvaluator myEvaluator = new MatchEvaluator( CommentMatchHan dler);
      //this replace the occurrences based on regexer patterns string result
      = regexer.Replace (code, myEvaluator);
      >
      public string MatchHandler(Ma tch match)
      >
      {
      //DO SOMETHING on MYVALUE
      return MYVALUE
      }
      Well,
      in the above sample I'd like to have "MatchHandl er" function like :
      public string MatchHandler(Ma tch 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( CommentMatchHan dler);
      Thanks,
      Bob
      Not directly. But you could do this:

      public class MatchEvaluatorW ithParameter
      {
      string _parameter;
      public MatchEvaluatorW ithParameter(st ring 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("myBeatif ulPattern", RegexOptions.Mu ltiline);
      MatchEvaluatorW ithParameter mwp = new MatchEvaluatorW ithParameter("y our param
      here");
      MatchEvaluator myEvaluator = mwp.MatchEvalua tor;
      //this replace the occurrences based on regexer patterns string result
      regexer.Replace (code, myEvaluator);

      Jesse


      Comment

      • Bob Speaking

        #4
        Re: pass parameters through delegate?

        Thanks guys :)
        Your suggestions and methods fit to my needs and help me understand delegate
        etc!

        Thank you,
        Bob


        Comment

        Working...