简体   繁体   中英

Why can I only view a list in the watch window, when stopped on a breakpoint accessing that list?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM