首页 > 解决方案 > 如何使用 Visual Studio 2017 调试器准确判断哪行代码更改了 C# 中的给定变量?

问题描述

我有一个 while(true) 循环,并且在每个循环结束时我都会更新一个列表,但是我已经能够查明在循环中间列表发生变化的地方。但是,我没有编写任何代码来解决该部分代码中的循环。当我运行代码时,称为 test1 和 test2 的 2D 数组是不同的,即使 movePiece 中没有任何地方(我使用 ctrl+F 检查)地址 boardHistory。

我想以某种方式跟踪此列表,以便在它更改时我可以看到调用堆栈和更改它的行。

while (true)
            {
                prompt();
                if (!moveSet.empty())
                {
                    inStr = moveSet.next();
                }
                else
                {
                    inStr = Console.ReadLine().ToLower();
                }

                validInput = true;
                if (!inputIsValid(inStr))
                {
                    validInput = false;
                    Console.WriteLine("invalid input");
                }

                if (validInput)
                {
                    string[] arr = inStr.Split(' ');

                    int[,] test1 = boardHistory[boardHistory.Count - 1].boardValues;
                    movePieceTrad(arr[0], arr[1]);
                    int[,] test2 = boardHistory[boardHistory.Count - 1].boardValues;

                    if (!moveValid)
                    {
                        continue;
                    }
                    else
                    {
                        turn++;
                        posOrNegTurn = -posOrNegTurn;

                        int w = tradToCoords(arr[1])[0];
                        int u = tradToCoords(arr[1])[1];

                        bool tempPawnBool = false;
                        if (Math.Abs(board[w, u]) == 1)
                        {
                            tempPawnBool = true;
                        }

                        int[] movement = {
                            tradToCoords(arr[0])[0], tradToCoords(arr[0])[1],
                            tradToCoords(arr[1])[0], tradToCoords(arr[1])[1]
                        };

                        bool tempCaptureBool = false;
                        int[,] test4 = boardHistory[boardHistory.Count - 1].boardValues;
                        int test5 = boardHistory[boardHistory.Count - 1].boardValues[w, u];
                        if (boardHistory[boardHistory.Count - 1].boardValues[w, u] != 0) // if the last board state had a piece where the last move landed
                        {
                            tempCaptureBool = true;
                        }

                        if (moveWasEnPassant)
                        {
                            board[w, u - board[w, u]] = 0;
                            tempCaptureBool = true;
                        }

                        moveHistory.Add(new Move(
                            Math.Abs(board[w, u]),
                            board[w, u] / Math.Abs(board[w, u]),
                            tempCaptureBool,
                            tempPawnBool,
                            movement
                            ));

                        moveWasEnPassant = false;

                        if (!(board[w, u] == 1 && u - tradToCoords(arr[0])[1] == 2) &&
                            !(board[w, u] == -1 && u - tradToCoords(arr[0])[1] == -2))
                        {
                            resetEnPassant();
                        }

                        int temp = board[w, u]; //the value of the piece just moved
                        int type = temp / Math.Abs(temp);

                        if (Math.Abs(temp) == 6)
                        {
                            if (type == -1) castlePiecesMoved[0, 1] = 1;
                            if (type == 1) castlePiecesMoved[1, 1] = 1;
                        }
                        if (Math.Abs(temp) == 4)
                        {
                            if (type == -1 && w == 0) castlePiecesMoved[0, 0] = 1;
                            if (type == 1 && w == 0) castlePiecesMoved[1, 0] = 1;
                            if (type == -1 && w == 7) castlePiecesMoved[0, 2] = 1;
                            if (type == 1 && w == 7) castlePiecesMoved[1, 2] = 1;
                        }

                        if (!aMoveExistsForType(posOrNegTurn))
                        {
                            if (posOrNegTurn == -1 && blackIsChecked)
                            {
                                Console.WriteLine("--White wins by checkmate--");
                                printBoard();
                            }
                            else if (posOrNegTurn == 1 && whiteIsChecked)
                            {
                                Console.WriteLine("--Black wins by checkmate--");
                                printBoard();
                            }
                            else
                            {
                                Console.WriteLine("--Draw by stalemate--");
                                printBoard();
                            }
                            //BoardObject tempBoardObject2 = new BoardObject(board, possibleEnPassants, castlePiecesMoved, posOrNegTurn);
                            //boardHistory.Add(tempBoardObject2);
                            break;
                        }
                        BoardObject tempBoardObject = new BoardObject(board, possibleEnPassants, castlePiecesMoved, posOrNegTurn);
                        boardHistory.Add(tempBoardObject);
                    }
                }
                printBoard();
            }

标签: c#visual-studiodebuggingvisual-studio-2017

解决方案


我想以某种方式跟踪此列表,以便在它更改时我可以看到调用堆栈和更改它的行。

我认为您可以使用Call Stack WindowWatch Window在循环顶部设置断点

启动时,请先设置断点,然后选择Start Debugging。在此过程下,调用堆栈窗口将显示调用堆栈并查看其具体行。

此外,当您跨断点继续执行时,当调用堆栈发生变化时,光标是受影响的行。

此外,您可以使用Watch Window来查看变量的变化,并且随着断点的进行,它会直接反映参数的细微变化。

希望它可以帮助你。


推荐阅读