22

Often when developing with VS2010 Ultimate, I want to check where in a codebase a value is being set (so where this is an assignment).

Is there a way, using VS2010 Ultimate, or a 3rd party debugging tool, to be able to get all the places in a codebase where a variable has been set or got?

2
  • 1
    What do you mean by 'variable'? More specifically, are you including fields and properties, or are you referring specifically to method-local variables? Commented Aug 22, 2010 at 0:08
  • @dotnetdev: I suggest either: a) removing the "or got" from your question or b) changing it to say "set only or got only" vs. "set or got". As is, your question (according to English and programming grammar rules) means you don't care if read references are found also in addition to write references (in which case you could just use VS's built-in Find All References feature). I'm pretty sure you're looking for a way to find write references only.
    – Tom
    Commented Sep 7, 2017 at 18:29

8 Answers 8

28

For what it's worth, this will finally be natively supported in VS2019.

Specifically the 'Find All References' window has a new 'Kind' column which can be filtered for 'Write' references:

enter image description here

The specific GitHub PR that added this feature is scheduled to be included in Visual Studio 2019 Preview 2 (16.0.P2) https://github.com/dotnet/roslyn/issues/22545

The full release of VS2019 is roadmapped for Q1 of 2019.

3
  • 2
    PSA for anyone reading this after the 2019 release: In the community edition write does not find places where the variable is passed into the method, just where it is edited within the method body. Commented Aug 6, 2019 at 19:11
  • 1
    Not as handy as how R# does it but good to have. Trying to not use R#, for performance reasons. Just some advanced refactorings and code gen/snippets that I'm missing.
    – codah
    Commented Jun 4, 2020 at 2:03
  • As of now, this only works in .NET languages, not Visual C++
    – bobobobo
    Commented Oct 8, 2021 at 23:06
13

You can use CTRL+SHIFT+F with regular expression : MyVariable[ \t\r\n\v\f]*=[^=], this will search for the "myVariable" by left of the "=" sign .

4
  • 2
    For VB.Net (since OP did not specify), this will also bring up all references where there is an equality check, since VB.Net doesn't use the ==.
    – Flater
    Commented Feb 18, 2016 at 14:05
  • 1
    What if that variable is passed as ref or out parameter to some other method that modifies it? Commented Feb 9, 2017 at 7:35
  • Like you, variableName = is what I search for in the code most of the times, when there's no ref or out present. Commented Feb 9, 2017 at 7:37
  • See my Sep 7'17 Answer below for a much more robust solution (and factored) using Visual Studio without 3rd party tools and that also lists all known caveats I can think of.
    – Tom
    Commented Sep 7, 2017 at 16:43
8

Yes, there is the Value Origins feature that's available in ReSharper 5.

Searching the entire solution with Ctrl+Shift+F or using Find Usages as some have suggested doesn't answer OP's question - it will show every usage of the variable, not just assignments, and swifting through that list can be tedious and time consuming.

2

Disclaimer: I'm affiliated with OzCode.

You can use the Debugging add-on OzCode, it has a feature called Setter break-point Setter break-point which hits when a property of an object changed.

enter image description here

1

Here's a more robust solution using Visual Studio without 3rd party tools:

1. For all except Post-/Pre-fix Increment and Shift Assignments:

  (^|[^\w.])MyVariable\s*([\+\-\*/%&|\^]|)=[\w\s]

2. For Post-/Pre-fix Increment and Shift Assignments:

  ((^|[^\w.])MyVariable\s*(\+\+|--)|(\+\+|--)\s*MyVariable[^\w.]|(^|[^\w.])MyVariable\s*(<<|>>)=)

3. For Out / Ref Parameters (N/A for Properties):

  (^|[^\w.])(out|ref)\s+MyVariable[^\w.]

CAVEATS:

  1. C#.NET only.
  2. Visual Studio 2012+ only.
  3. Does not work if "=" is followed by an EOL.
  4. Does not work if "MyVariable" is followed by an EOL.
  5. Depending on starting point and scope of the Find and scope of the Variable / Property, may find more / less references than necessary. When in doubt, error on side of "more", so you won't miss anything.
  6. Does not work for "."-prefixed Variables / Properties. 6.1. Unless you include it as part of the "MyVariable" (i.e. "MyStructVariable.MyStructField" or "MyObjectVariable.MyObjectField") but you risk finding too few references since there may be other Struct or Object Variables used to make Assignments to the same Struct or Object Field or Property.
1
  • 1
    Doesn't work for deconstruction either. (MyVariable1, MyVariable2, MyVariable3) = TupleReturningExpression
    – Tinister
    Commented Apr 16, 2018 at 18:35
0

You can use the command "Find references" (Ctrl + K, Ctrl + R)

-1

As Daniel Pratt asked above, I'm not sure whether you mean properties, variables or something else. However, one related function that I use a lot is "Find Usages", which can be reached by right clicking methods, classes, members etc. That finds not only assignments, but all uses - however maybe that will narrow it down enough for you to sift through manually after the assignments.

0
-1

Sometimes old techniques become the best. May be you can search entire solution with CTRL + Shift + F

3
  • Ctrl + Shift + F would be handy. I used to keep this as desktop wallpaper. microsoft.com/downloads/… HTH :) Commented Aug 23, 2010 at 14:48
  • -1. This will find all references, that is both when the value is being set or being referenced. Commented Aug 27, 2014 at 19:57
  • also it is even worse than just "Find Usages" Commented Jul 24, 2017 at 11:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.