Reputation: 2814
THere's a switch statement with 8 cases inside, and I want to see when it hits.
Unfortunately before hitting, it hits default around 10 times.
Now I either break at switch, press F10, and continue when it hits default, or I set breakpoints on each case but default.
Any chance I can change this?
It's not possible to change the switch statement or the way the methods around it work (unfortunately).
Edit: I'm looking for something like a conditional breakpoint, but the condition being switchOnThis == (default case)
Upvotes: 2
Views: 1879
Reputation: 3509
Add another switch
with the same cases before your existing switch
:
switch (condition)
{
case 1:
case 2:
...
case 8:
break; // <-- set the breakpoint here
}
Upvotes: 0
Reputation: 1669
I came up with something.
You could take advantage of the side effects of breakpoint conditions this way:
switch
statement. The condition on this breakpoint should "raise a flag" (e.g. create a file) and evaluate to anything other than true
, so that the breakpoint is not hit. default
case. The condition on this breakpoint should "lower the flag" (e.g. delete the same file) and should also not evaluate to true
. switch
block. Condition on this breakpoint should simply check the flag (e.g. check that file exists) and return true
if the flag is still raised (i.e. a case other than default was hit). This is where execution will break when one of the case labels other than default is hit.I tried this with the following code:
static void Main(string[] args)
{
int num = 0;
bool hit = false;
do
{
Console.WriteLine("Insert an integer value (1-8 hits, 100 to quit):");
num = Convert.ToInt32(Console.ReadLine());
switch (num) // breakpoint 1 here
{
case 1: hit = true; break;
case 2: hit = true; break;
case 3: hit = true; break;
case 4: hit = true; break;
case 5: hit = true; break;
case 6: hit = true; break;
case 7: hit = true; break;
case 8: hit = true; break;
default: hit = false; break; // breakpoint 2 here
}
Console.WriteLine("hit = " + hit.ToString()); // breakpoint 3 here
} while (num != 100);
}
I placed conditional breakpoint 1 at switch (num)
with the following condition (it evaluates to nothing, thus it never breaks):
System.IO.File.Create(@"somefile.txt").Close()
Breakpoint 2 at the default
case with the following condition (also evaluates to nothing):
System.IO.File.Delete(@"somefile.txt")
Finally, breakpoint 3 at Console.WriteLine("hit =...
, with condition:
System.IO.File.Exists(@"somefile.txt")
It works as expexted, i.e. it breaks after the switch
block only if the default
case was not hit.
In order for this to work you need to configure the debugging environment to use both native and managed compatibility. Go to:
Tools -> Options -> Debugging -> General
and check Use Managed Compatibility Mode
and Use Native Compatibility Mode
.
The create/delete file idea is the only one I could come up with that has side effects and doesn't require any change to your code.
Upvotes: 1
Reputation: 18838
You can set a conditional breakpoint like the following. First suppose you have the following switch\case
:
int test = 0;
// value of the test will be set here
switch (test)
{
case 1:
// case 1
break;
case 4:
// case 4
break;
case 5:
// case 5
break;
// up to 8 cases
default:
break;
}
Then set a breakpoint in the editor, and right-click on it. You can see the Conditions...
on the menu. click on it, and write the conditions:
You can set the conditions like the following:
The conditional berakpoint breaks when one of the cases will be happened.
Upvotes: 2