[英]Conditional breakpoint in Visual Studio
I want to set a breakpoint on a certain line in C# code when some other variable is equal to a specific value, say:当某些其他变量等于特定值时,我想在 C# 代码的某一行上设置断点,例如:
MyStringVariable == "LKOH"
How can I do that?我怎样才能做到这一点?
I tried to right click on breakpoint icon -> Condition and then typed MyStringVariable == "LKOH"
and Visual Studio said it cannot evaluate it.我尝试右键单击断点图标 -> 条件,然后输入
MyStringVariable == "LKOH"
并且 Visual Studio 说它无法评估它。
if (MyStringVariable == "LKOH") Debugger.Break();
you'll need System.Diagnostics namespace你需要 System.Diagnostics 命名空间
http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break.aspx http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break.aspx
Sample code:示例代码:
static void Main(string[] args) {
string myvar;
for (int ix = 0; ix < 10; ++ix) {
if (ix == 5) myvar = "bar"; else myvar = "foo";
} // <=== Set breakpoint here
}
Condition: myvar == "bar"条件:myvar == "bar"
Works well.效果很好。
Just like in code, you need to use:就像在代码中一样,您需要使用:
MyStringVariable == "LKOH"
The double-equals is the key.双等号是关键。 Without it, it's saying it can't evaluate because your expression doesn't evaluate to a boolean.
没有它,它表示它无法评估,因为您的表达式不会评估为布尔值。
You should be able to make this work.你应该能够完成这项工作。 Are you using the Exchange instance name in the condition?
您是否在条件中使用 Exchange 实例名称? The condition should be something like
myExchange.Name == "LKOH"
not Exchange.Name == "LKOH"
.条件应该类似于
myExchange.Name == "LKOH"
而不是Exchange.Name == "LKOH"
。
By the way, using the assignment operator =
instead of the equality operator ==
will work but it will set the property and waste 1/2 hour of your time figuring out what the hell is going on.顺便说一句,使用赋值运算符
=
而不是相等运算符==
会起作用,但它会设置属性并浪费 1/2 小时的时间来弄清楚到底发生了什么。 I made this mistake just yesterday.我昨天刚刚犯了这个错误。
In my case, I forgot that I was debugging a VB application.就我而言,我忘记了我正在调试 VB 应用程序。
In VB equality is =
not ==
like many other languages, thus my conditional breakpoint needed to be myString = "someValue"
not myString == "someValue"
在 VB 中,等式是
=
not ==
像许多其他语言一样,因此我的条件断点需要是myString = "someValue"
而不是myString == "someValue"
The variable you are testing for needs to be in scope at the breakpoint.您正在测试的变量需要在断点处的范围内。
var x = "xxx";
{
var y = "yyy";
}
brak(); // x is in scope, y isn't
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.