0

I'm having a problem this evening, where I don't understand why I cannot add a list to the watch window.

The problem is occurring with the following minimum reproducible example.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace demo
{
    public partial class formParts : Form
    {
        List<part> parts = new List<part>();
        List<part> partChanges = new List<part>();
        List<part> searchInputs = new List<part>();

        public formParts()
        {
            InitializeComponent();
            searchInputs.Add(new part());
            // ...
        }

        // ...

        private void button2_Click(object sender, EventArgs e)
        {
            partAccess db = new partAccess();
            if (searchInputs[0].id == 0)
            {
                parts = db.getParts();
            }
            else
            {
                parts = db.getPart(searchInputs[0].id);
            }
            refreshResultsTable();
        }

        // ...

    }
}

If I set a breakpoint for example on parts = db.getParts(); then I can add parts to the watch window, and after stepping over this line, can see parts populated with hundreds of entries that have been pulled from an SQL database. If I then continue the application and pause, I cannot see the list contents. Equally, if I don't add this breakpoint, when I add parts to the watch window, watch gives me this error:

parts error CS0103: The name 'parts' does not exist in the current context

gunr2171
  • 16,104
  • 25
  • 61
  • 88
jwilo
  • 152
  • 1
  • 9
  • Hitting the "pause" button in Visual Studio isn't really a good way to inspect variables. Breakpoints are the best, along with hovering over values while execution is in that state. – gunr2171 Nov 25 '22 at 03:15
  • 1
    If you break execution arbitrarily then who knows where the code is up to, so who knows whether that `parts` field actually does exist in that context. `parts` is a member of your `formParts` class so if the debugger is currently executing code outside that class then that field does not exist in the current context so why would the *Watch* window be able to display it? – jmcilhinney Nov 25 '22 at 03:20
  • Nicely formed question, but the error message gives you the answer. – Rufus L Nov 25 '22 at 04:08

1 Answers1

0

The watch window uses the name of the variable to resolve the specific object to show. But this will depend on the context, there can be multiple parts referring to different lists, or some completely different object. This is what the error is telling you, at the particular place you have stopped, parts does not resolve to anything.

Probably the best way to deal with this is to search for all places where parts is used, and place breakpoints at these places. If you want to find out when the list changes that should be the only possible alternatives, and then you are guaranteed to be able to inspect the list.

Another possible option might be to assign your list to a global variable, but I'm not sure how having the list globally accessible will actually help, it will not really give you any idea why the list was changed. And you may be tempted to start using it for other things.

Note also the "make object ID" feature that can sometimes be helpful to distinguish between multiple different objects with the same name.

JonasH
  • 28,608
  • 2
  • 10
  • 23
  • This answer is pushing me towards understanding, I think! Using "make object ID", I am able to view `parts` at whim - however, the list is always empty (apparently) using this. `parts` is the data source for a datagridview, so I know it is not empty - a) because I'm looking at a GUI displaying hundreds of lines of data, and b) because I'm editing that data. So in essence, I know what I am looking at exists somewhere - and it's that 'somewhere' I want to add to the watch window. – jwilo Nov 25 '22 at 11:19
  • OK - I've added a breakpoint on entry to one of the cell event handlers, and now I can see the contents of `parts` - so I now understand **how** to view such variables/lists, but do not understand **why** I cannot see them at arbitrary times, given I know they persist in memory. – jwilo Nov 25 '22 at 11:23
  • @jwilo As I tried to explain, it is because the debugger need a *context* to be able to resolve variable names to actual memory addresses. Ofc, there are things like memory debuggers that allow you to search thru memory for all objects. But that is typically not as useful when trying to debug the behavior of the program. – JonasH Nov 25 '22 at 11:52
  • I think 'context' is where I'm getting hung up. In my searching so far for 'context' in the context (no pun intended) of C# and .NET, I've found Context class, Context object, User Context, Context menu, Context-Bound, Context-Agile. This question https://stackoverflow.com/questions/12240420/what-is-a-context doesn't clear it up in my mind much either. – jwilo Nov 25 '22 at 17:26
  • @Jwilo In this case "Context" means a specific callstack. That contain all the addresses to allow the debugger to figure what the address for `this` is, and allows variable names to be resolved to actual addresses. – JonasH Nov 26 '22 at 18:20