How to add 2nd parameter to this function?

Hoi all...
a while ago I asked here for help for a special regEx and I got a great answer. Now I want to extend the code I got but I am too dumb to handle it :(.

This is what I got:
---------------------------------------------
private static string CodeReplacer(Match match) {
      string content=CodeToHtml(match.Groups["content"].Value);
      if (match.Groups["url"].Success) {
........
      }
            throw new ArgumentException("Unrecognized code");
}

public static string CodeToHtml(string code) {
      return Regex.Replace(code, @"\[((url=(?<url>[^\]]+)(\](?<content>((?!\[/?url).)*)\[/url)?)|((?<tag>b|ul|li|center)\](?<content>((?!\[/?\k<tag>).)*)\[/\k<tag>)|img(?<position>left|center|right|l|c|r)?=(?<imageURL>[^\]]*))\]", new MatchEvaluator(CodeReplacer), RegexOptions.Singleline|RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
}
---------------------------------------------


What I want to do now:
I want to use a second parameter inside "CodeReplacer".
Would be something like this:
---------------------------------------------
private static string CodeReplacer(Match match,  cSomeClass inObject) {
      string content=CodeToHtml(match.Groups["content"].Value,  cSomeClass inObject);
      if (match.Groups["url"].Success) {
........
      }
            throw new ArgumentException("Unrecognized code");
}

public static string CodeToHtml(string code, cSomeClass inObject) {
      return Regex.Replace(code, @"\[((url=(?<url>[^\]]+)(\](?<content>((?!\[/?url).)*)\[/url)?)|((?<tag>b|ul|li|center)\](?<content>((?!\[/?\k<tag>).)*)\[/\k<tag>)|img(?<position>left|center|right|l|c|r)?=(?<imageURL>[^\]]*))\]", new MatchEvaluator(CodeReplacer), RegexOptions.Singleline|RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
}
---------------------------------------------


This does not work, because now "CodeReplacer" does not match "new MatchEvaluator(CodeReplacer)" anymore.

Does anybody understand the code above enough to help me? :)

Thank you, bye
Smoerble

SmoerbleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Razzie_Commented:
Well, yes and no. I take it the MatchEvaluator constructor takes a CodeReplacer object as a parameter? There should be no error when adding a parameter to a method for that. Are you sure that is the error and could you maybe post the lines of code this produces the error?
0
dunglaCommented:
Smoerble,

Delegate MatchEvaluator takes only one Match as parameter and that returns a string, therefore you cannot add 2nd param to your CodeReplace function.

Maybe you should try another way instead of add 2nd param to your function :-)
0
SmoerbleAuthor Commented:
Hmmm... doesn't sound good...
I don't really understand how these two methods work together, that's why I don't know how to find a different way to do what I want...

Basicly I want to do this:
the RegEx returns sometimes a match that contains an ID of something. If this is the case, I want to do something with the diea, but for this I need an object in this method, that's why I wanted to "give" this object via a 2nd parameter.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

der_jthCommented:
The generic approach goes like this:
---
internal class CodeReplacerWrapper {

  private SomeClass inObject;

  internal CodeReplacerWrapper(SomeClass inObject) {
    this.inObject = inObject;  
  }

  internal static string CodeReplacer(Match match) {
     ...
     // Just use this.inObject here
  }
}
--

And when using the regex, do something like this:

Regex.Replace(code, @"regexgoeshere",  new MatchEvaluator(new CodeReplacerWrapper(inObject).CodeReplacer))

There might be more optimized approaches available for certain circumstances, but that one should work quite nicely unless you have huge amount of Replacements to be made in which case the wrapper construction cost might become an issue.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SmoerbleAuthor Commented:
Thanks for this.
Unfortunatly I still have no idea how to re-write this to be able to give it more parameters.
My understanding now is, that I cannot work with a wrapper like above... but if I can't how, would I recode this on my own?
0
der_jthCommented:
What do you mean you cannot? What is the problem with the wrapper approach given above?
0
SmoerbleAuthor Commented:
I need to do something like this:
-------------
  internal CodeReplacerWrapper(SomeClass inObject, SomeClass inAnotherObject) {
    this.inObject = inObject;  
  }
 internal static string CodeReplacer(Match match, SomeClass inAnotherObject) {
    if RegEx found something special, do something with inAnotherObject.
  }
-------------

From the posts above, my understanding now is, that you can't do this.
0
der_jthCommented:
Yes you can, but not just with the syntax you expect. The key point is that you cannot pass the second parameters to the method (as it would violate the delegate signature), but you can pass them to the wrapper object that the MatchEvaluator method belongs to. Please re-read my first post above and elaborate which part doesn't suit your plans. I believe we can work something out.
0
SmoerbleAuthor Commented:
Oh, I think I see now what you mean... was blind and dumb.. so I will try to get your generic approach working and come back if it fails (as I maight be still dumb) :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.