Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We can change the way our code executes without stopping the debugger.
-
0:00
Let's delete all the breakpoints we have so far and
-
0:03
I'll show you one of my favorite ways to start the debugger.
-
0:06
We can right-click on the line of code that we want to break at and
-
0:11
click Run To Cursor.
-
0:15
The debugger starts and runs to that point.
-
0:18
Another neat but
-
0:19
dangerous trick is to set the next line of code that should be executed.
-
0:24
Let's say we're here and
-
0:26
we want to just skip to the add song method without calling menu dot prompt.
-
0:32
That way we don't have to type in the console.
-
0:35
I can just drag the yellow arrow here down to this line.
-
0:40
This sets the next line of code to be executed.
-
0:43
Unlike using the Step Over button, the code between where we were and
-
0:48
where we are now isn't executed.
-
0:50
So the Menu.Prompt method was never called.
-
0:55
As you can imagine, this can cause some problems.
-
0:58
Let's step into the add song method.
-
1:01
Again, let's say we don't want to have to run these prompt methods.
-
1:06
Another way to set the next line of code to execute is to right click on the line
-
1:11
and click Set Next Statement.
-
1:13
Now, we just jump to past these lines here without executing them.
-
1:17
Hovering over song name and artist shows us that they're both null.
-
1:22
That could be bad.
-
1:24
Never fear, we can just set their values here instead.
-
1:35
Now, if I click the step over button, this song will be added to our list of songs.
-
1:40
Let's jump past the end of the loop and skip this prompt.
-
1:47
Now, I can use the Step Out button to bring us back to the main method.
-
1:52
We can also go backward.
-
1:54
Let's say we want to go back to see this prompt.
-
1:58
We can just drag the yellow arrow up here.
-
2:01
And then, press the Continue button.
-
2:06
We usually want to see how the code runs without us interfering too much.
-
2:10
So using these features can be problematic.
-
2:13
But as you can see, it can also be quite helpful.
-
2:16
You can also use this trick if you're stepping through the code and
-
2:19
you identify what might be causing the bug.
-
2:22
You can experiment with different and variable values and
-
2:25
ways to the code by using these features of the debugger.
-
2:29
In debuggers without these features, you have to stop change the code and
-
2:33
then rerun it.
-
2:35
Some it can take a long time to get back to where the problem is
-
2:39
only to find out that the bug isn't fixed.
-
2:42
Visual Studio has another great debugging feature that allows us to actually make
-
2:46
substantial changes in the code without actually having to stop the debugger and
-
2:50
start all over again.
-
2:51
This feature is called Edit and Continue.
-
2:54
Edit and Continue is a feature that can be enabled or
-
2:57
disabled in Visual Studio's options.
-
2:59
To make sure that it's enabled, go to the Tools menu and click Options.
-
3:04
On the left side of the dialog, scroll down to Debugging, then scroll down to
-
3:09
the Edit and Continue section down here Here we see that it's checked.
-
3:14
We can't change the value of this option while Visual Studio is in debugging mode.
-
3:20
Let's set a breakpoint back here on line 21.
-
3:23
I'll enter an option so that we get to the switch statement.
-
3:27
Let's say that at this point while debugging,
-
3:30
we notice that we accidentally had these two methods swapped.
-
3:34
We can fix this right now without stopping the debugger.
-
3:38
We can move the AddSong method down here.
-
3:45
And we can move the DisplaySongList method up here.
-
3:51
Now, if we step through this code using step into, you'll notice that we now enter
-
3:56
the DisplaySongList method instead of the AddSong method.
-
4:00
This is really handy when we're debugging and
-
4:03
notice things that we can fix right away.
You need to sign up for Treehouse in order to download course files.
Sign up