Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to add 2nd parameter to this function?

Posted on 2004-10-28
10
Medium Priority
?
644 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:Smoerble
10 Comments
 
LVL 8

Expert Comment

by:Razzie_
ID: 12441880
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
 
LVL 13

Expert Comment

by:dungla
ID: 12441964
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
 

Author Comment

by:Smoerble
ID: 12442267
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Accepted Solution

by:
der_jth earned 2000 total points
ID: 12453482
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
 

Author Comment

by:Smoerble
ID: 12471279
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
 
LVL 6

Expert Comment

by:der_jth
ID: 12471684
What do you mean you cannot? What is the problem with the wrapper approach given above?
0
 

Author Comment

by:Smoerble
ID: 12472185
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
 
LVL 6

Expert Comment

by:der_jth
ID: 12472300
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
 

Author Comment

by:Smoerble
ID: 12472323
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question