Click here to Skip to main content
14,637,552 members
Rate this:
Please Sign up or sign in to vote.
See more:
The title of the question pretty much says it all: in Visual Studio 2019, how do I set a conditional breakpoint that calls Math.Abs()?

Eg. Math.Abs( x ) > 0

What I have tried:

I have tried prefixing the call, like this: System.Math.Abs( x ) > 0

I've also tried searching for an answer but came up blank. Oddly, I could find how to call a C++ stdlib function (like strcmp) but nothing for C#.
Posted
Updated 2-Aug-20 22:14pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

Does this help: Use breakpoints in the debugger - Visual Studio | Microsoft Docs[^] - it has a section on conditional breakpoints.

I tried it with a BP on the Console line:
for (int i = 0; i < 100; i++)
    {
    Console.WriteLine(i);
    }
I set a BP, then right clicked and selected "Conditions..."
Leaving "Conditional Expression", and "is true", in the expression box I typed "Math.Abs(i) == 5" and closed.
The breakpoint triggered only on the sixth iteration.
   
v3
Comments
Patrick Skelton 3-Aug-20 4:39am
   
Mmm... that's strange. It does indeed work fine now. I guess I must have had a syntax error or something. I'll leave the question up because it may provide a nice quick answer for anyone else who may come along. Thank you.
OriginalGriff 3-Aug-20 4:49am
   
You're welcome!

It failed for me the first time as well - I used the English spelling of "Maths" instead of the American "Math" :blush:
Rate this:
Please Sign up or sign in to vote.

Solution 2

You cannot debug the predefined methods.The other ways to see the output values is by using quickwatch or assign to variable and check the condition.
   
Comments
OriginalGriff 3-Aug-20 4:22am
   
Um ... are you sure? Conditional breakpoints using "internal functions" works fine for me ...
OriginalGriff 3-Aug-20 4:25am
   
Oh, and just in case you misunderstood the original question - you can debug into "predefined methods" including framework library ones quite easily (and it can be very handy):
https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-debug-dotnet-framework-source?view=vs-2019
Member 14885087 3-Aug-20 4:50am
   
I have tried this and works for me
int testInit = 1;
for (int i = 0; i < 100; i++)
{
if(Math.Abs(i)> 3)
{
Console.WriteLine("Test");
}
Console.WriteLine(i);
}
I put conditional break point for Math.Abs(i)> 3 with conditional expression 'i==5' and the bp has worked for me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100