1

Problem after updating VS2022 viewing the watches. After an answer with proposal to post the code, I do so to exclude the problem is caused by my code prior to reporting a bug.

The original question: Watch window at debugging: CS0103: The name '' does not exists in the current context. Version and solution specifics are stated there.

The code is merely to get something on screen.


Things I tried afterwards:

  • Creating a new solution, adding a windows form app and windows controll library. Created a class with values.

No problem here,


  • Doing the same but, also a new solution, pasted the code of StringParser into the library and the relevant code of the constructor of Form1,
  • Again a new solution where the StringParser is part of only a windows forms app,

Both the same problem.


The image contains a screenshot of the watch window. Also from the code file to show debugging state. (The code is as formatted text below).

enter image description here

All projects are 'out-of-the-box'

Library - StrangParser.cs

namespace html
{

    public enum Pin
    {
        Start,
        End,
        Both,
    }

    public class StringParser
    {

        private string  content     = "";
        public  string  Content     { get { return content; } set { if (content != value) content = value; if (content.Length > 0) { position = start = 0; end = 0; } } }

        private int     position    = -1;
        private int     start       = -1;
        private int     end         = -1;

        public bool Next()
        {

            ++position;
            if (position > content.Length)
                position = content.Length;

            if (position > end)
                end = position;

            return (position >= content.Length);

        }

        public bool Next(char to_char, bool include = true)
        {
            
            while (position < content.Length && content[position] != to_char)
            {
                ++position;
                if (position > end)
                    end = position;
            }

            if (include) ++position;

            if (position > content.Length)
                position = content.Length;

            if (position > end)
                end = position;

            return (position >= content.Length);

        }

        public bool Previous()
        {

            --position;
            if (position < 0)
                position = 0;

            if (position < start)
                start = position;

            return (position ==0);

        }

        public  string  Token
        {

            get 
            { 
                
                return start >= 0 && end <= content.Length && end > start 
                        ? content.Substring(start, end - start) 
                        : ""; 
            
            }
        
        }

        public void Pin(Pin pin)
        {

            if (pin == html.Pin.Start || pin == html.Pin.Both)
                start = position;

            if (pin == html.Pin.End || pin == html.Pin.Both)
                end = position;

        }

        public override string ToString()
        {

            if (content == null || content == "")
                return "";

            string s = content.Substring(0, start);
            string t = Token;
            string e = content.Substring(end, content.Length - end);

            if (s.Length > 15) s = "..." + s.Substring(s.Length - 15);
            if (e.Length > 15) e = e.Substring(0, 15) + "...";

            return string.Format("[{0}-{1}-{2}] {3} |--> '{4}' <--| {5}", start, position, end, s, t, e);

        }

    }

}

Form App - Form1.cs - code

using System.Windows.Forms;
using html;

namespace contentdownloader
{

    public partial class Form1 : Form
    {

        string          filecontent     = "<html><head></head><body></body></html>";

        StringParser    watch_parser    = null;
        string          watch_token     = null;

        public Form1()
        {

            InitializeComponent();


            StringParser    parser      = new StringParser();
            watch_parser = parser;

            parser.Content = filecontent;

            string token = "";
            while (!parser.Next('<'))
            {
                
                //parser.Pin(html.Pin.Both);
                parser.Next('>');
                token = watch_token = parser.Token;
                parser.Pin(html.Pin.Both);

            }

        }

    }

}


Willem
  • 302
  • 10

1 Answers1

0

You can change your code like this, define parser and token before the function InitializeComponent() of Form1:

public partial class Form1 : Form
{
    string filecontent = "<html><head></head><body></body></html>";
    StringParser parser = new StringParser();
    string token = "";
    public Form1()
    {

        InitializeComponent();
        parser.Content = filecontent;
        while (!parser.Next('<'))
        {
            parser.Next('>');
            token = parser.Token;
        }

    }
}

Here is my test result: enter image description here

Jingmiao Xu-MSFT
  • 2,076
  • 1
  • 3
  • 10
  • But why when done within the constructor it fails to display in the watch window? When I tried the OPs code, I got the same results as them. I also could not hover my cursor over the item and see its value. Seems like something is broken with Visual Studio in this scenario. – Timothy G. Dec 06 '22 at 14:13
  • @TimothyG. Both thanks for looking into it. Have filed it as a bug. – Willem Dec 06 '22 at 21:14
  • @CS1061 You can share the link where you file this problem, it will also help others if they have the similar issue. – Jingmiao Xu-MSFT Dec 07 '22 at 01:02
  • The author created too many questions and I'm a little confused :) But for the original problem (local variables in WinForms projects are not captured by debugger): It's a regression in 17.4 V.S. WinForms repo issue: https://github.com/dotnet/winforms/issues/8354 – Kirsan Dec 10 '22 at 15:10