4

I am trying to implement this example

http://blog.evonet.com.au/post/Gridview-with-highlighted-search-results.aspx

but the only problem I am facing is the AddressOf keyword of VB.net which I am unable to convert in C#.net

can anybody help me out with this, what alternative I should use to make it work.

Thanks.

Edit: I found some searches on stackoverflow regarding similar problems but I am unable to understand them.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Ahmed
  • 645
  • 4
  • 13
  • 24

4 Answers4

8

You can just leave it out. Method groups are implicitly convertible to delegates in C#.

return ResultStr.Replace(InputTxt, new MatchEvaluator(ReplaceWords))

Or even simpler(I think this requires C# 2):

return ResultStr.Replace(InputTxt, ReplaceWords);

But since ReplaceWords is so simple, I'd consider a lambda expression(Requires C# 3):

return ResultStr.Replace(InputTxt, m => "<span class=highlight>" + m + "</span>");
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • Thanks CodeInChaos, I didn't know it was that easy I think experience speaks itself. Thanks again. – Ahmed Oct 11 '11 at 14:52
2

Based on the link you posted, I'm assuming that you want this:

Return ResultStr.Replace(InputTxt, New MatchEvaluator(AddressOf ReplaceWords))

.. in C#?

If so you don't need the AddressOf keyword at all. MatchEvaluator is a delegate type so you can simply pass over a method (ResultStr.Replace(InputTxt, ReplaceWords)). Alternatively, you could use an anonymous method for this to reduce the code, which makes sense as it's not being used elsewhere:

return ResultStr.Replace(InputTxt, delegate(Match m) {
    return "<span class=highlight>" + m.ToString() + "</span>";
});
Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
1

I think you need to translate this into:

new EventHandler(theMethod)
dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89
1

You should be able to use something like this:

return ResultStr.Replace(InputTxt, new MatchEvaluator(ReplaceWords))

Basically, in c# you don't need the 'addressOf' operator. I like having it, since it makes it real clear whats up, but c# is just like that.

Andrew
  • 8,322
  • 2
  • 47
  • 70