Visual Studio中显示错误的值在调试时? [英] Visual Studio showing wrong values while debugging?
问题描述
我调试我的代码时,我看到了一些奇怪的。基本上,它来到了这一点:
I was debugging my code when I saw something weird. Basically it came down to this:
你在这里看到是一个unsigned int值为负...
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.
这怎么可能呢?
<子>我只有C#Visual Studio 2010中,所以我不知道它在其他版本或不同语言存在这个问题。我会改变的标题和标签相应的,如果是这样的话。
推荐答案
在调试,Visual Studio将。跟踪在目前情况下每个变量的
While debugging, Visual Studio will keep track of every variable in the current context.
考虑下面的一段代码:
void SomeMethod1()
{
int j = 0;
}
void SomeMethod2()
{
int i = 0;
}//Breakpoint on this line.
在程序中断,目前的情况下是 SomeMethod2
。此时,开发商是无法检查 j值
将是什么。这是因为诠释J
不是当前上下文中的变量。
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.
< STRONG>背后OP的描述的行为的实际原因:
Visual Studio的检查,如果在名称强>该变量存在于当前上下文和不检查如果在变量本身在目前情况下存在。
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.
因此,如果我们改变变量 j的名称
到 I
SomeMethod1
,我们可以突然查看其价值。想象一下,这将是多么奇怪得到,如果我在
是一个字符串: SomeMethod2
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:
:当你应该知道这个?
只要你有一段代码在那里它不是立即清除当前上下文是什么。我在下面这段代码中遇到这样的问题:
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;
}
我调试方法2
,但我想我可以查看方法1
以及中的变量,但这情况并非如此,因为方法1
都有自己的内容。
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屋!