Visual Studio在调试时显示错误的值? [英] Visual Studio showing wrong values while debugging?

查看:455
本文介绍了Visual Studio在调试时显示错误的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试我的代码,当我看到一些奇怪的东西。基本上是这样的:





你在这里看到的是一个无符号的int,带负值...



我也注意到,变量没有值弹出(?)和其他人。但是这个变量是唯一一个有不可能的价值的变量。



这可能如何?



我只有C#Visual Studio 2010,所以我不知道这个问题存在于其他版本或不同的语言。如果是这样的话,我会更改标题和标签。

解决方案

调试时,Visual Studio会跟踪当前上下文中的每个变量。



考虑以下代码:

  void SomeMethod1()
{
int j = 0;
}

void SomeMethod2()
{
int i = 0;
} //此行上的断点。

当程序中断时,当前上下文为 SomeMethod2 。在这一点上,开发人员无法检查 j 的价值是什么。这是因为 int j 不是当前上下文中的变量。



OP描述行为背后的实际原因:


Visual Studio检查名称是否该变量存在于当前上下文中,如果变量本身存在于当前上下文中,不会检查


因此,如果我们将 j 的名称更改为 i $ c> SomeMethod1 ,我们可以突然看到它的值。想象一下,如果 i SomeMethod2 是一个字符串:







每当你有一段代码,不立即清楚当前的上下文是什么。我在以下代码中遇到了这个问题:

  private double CalculateFast(double d)
{
//根据d的大小,[方法1]或[方法2]的速度更快。
//运行它们,并使用第一种方法的答案来完成。
Tasks.Task.Factory.StartNew(()=>
{
//方法1
}

//方法2

返回答案;
}

我正在调试方法2 但是我以为我可以查看方法1 的变量,但情况并非如此,因为方法1 有自己的上下文。


I was debugging my code when I saw something weird. Basically it came down to this:

What you see here is an unsigned int with a negative value...

I also noticed that some surrounding variables didn't have a value pop-up(?) and others did. But this variable was the only one that had an impossible value.

How is this possible?

I only have C# Visual Studio 2010, so I don't know it this problem exists in other versions or for different languages. I'll change the title and tags accordingly if this is the case.

解决方案

While debugging, Visual Studio will keep track of every variable in the current context.

Consider the following piece of code:

void SomeMethod1()
{
    int j = 0;
}

void SomeMethod2()
{
    int i = 0;
}//Breakpoint on this line.

When the program breaks, the current context is SomeMethod2. At this point the developer is unable to check what the value of j would be. This is because int j is not a variable in the current context.

The actual reason behind OP's described behavior:

Visual Studio checks if the name of the variable exists in the current context and doesn't check if the variable itself exists in the current context.

So if we change the name of variable j to i in SomeMethod1, we can suddenly view its "value". Imagine how weird it would get if i in SomeMethod2 was a string:

When should you know this?

Whenever you have a piece of code where it's not immediately clear what the current context is. I encountered this problem in the following piece of code:

private double CalculateFast(double d)
{
    //Depending on the size of d, either [Method 1] or [Method 2] is faster.
    //Run them both and use the answer of the first method to finish.
    Tasks.Task.Factory.StartNew(() = >
    {
        //Method 1   
    }

    //Method 2

    return answer;
}

I was debugging Method 2 but I thought I could view the variables of Method 1 as well. But this wasn't the case because Method 1 has its own context.

这篇关于Visual Studio在调试时显示错误的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆