Finding all the places a variable has been set?
Often when developing with VS2010 Ultimate, I want开发者_StackOverflow 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?
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:
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.
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 .
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.
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.
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:
- C#.NET only.
- Visual Studio 2012+ only.
- Does not work if "=" is followed by an EOL.
- Does not work if "MyVariable" is followed by an EOL.
- 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.
- 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.
You can use the command "Find references" (Ctrl + K, Ctrl + R)
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.
Sometimes old techniques become the best. May be you can search entire solution with CTRL + Shift + F
精彩评论