views:

30

answers:

2

In the VS2005 code-editor using C#, how do you search for text in the current function only? It allows searching over the Current Document but I cannot see how to limit the search to only the current function.

+1  A: 

Select the function's code and then a new option will be available in the dropdown list. This option is called Current Block. This will allow you to search only within that specific function.

From the source on MSDN: Quick Find, Find and Replace Window

Leniel Macaferi
A: 

Most of the time, your functions should be short enough that this really isn't an issue, and I mean that.

Occasionally, you might have an almost-legitimate need for some kind of indexing function that has a lot of distinct tokens. Even in this case, you should refactor to use a database or reflection or something, as this is the kind of thing that'll get your code featured on DailyWtf.com.

But if it's really unavoidable to have a function this long, if it's legacy code, or for some other weird reason, there are some things you can do:

  • The Find All References feature in visual studio lists all results at once, in order. You can size the window to show just the results in your function.
  • You can use partial classes to isolate your function to a specific file
  • Highlight the function with your mouse, and the Find/Replace dialog can search within selected text only.
Joel Coehoorn