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