You have to know how to use the IDE's full power. You have to know the code. You have to know how to debug. Period.
Let's get started.
Open up your visual studio and create a new project. Seriously, follow the instructions all the way... open it up and create a new console project. Call it DebugDemo. You will copy the code I provide. You will place the breakpoints I specify. You WILL learn how to do this.
Still with me? Some may be put off by that paragraph. Some might be inclined to not follow instructions. Sorry, but this lab is 100% monkey see - monkey do or monkey quit programming (please).
Anyway, you have this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DebugDemo
{
class Program
{
static void Main(string[] args)
{
}
}
}
Get rid of all of your usings except system. Code now becomes:
using System;
namespace DebugDemo
{
class Program
{
static void Main(string[] args)
{
}
}
}
Good...
We're going to count to 10. Here's your code:
using System;
namespace DebugDemo
{
class Program
{
static void Main(string[] args)
{
int start = 1;
int stop = 10;
for (int current = start; current <= stop; current++)
{
Console.WriteLine(current);
}
Console.ReadLine();
}
}
}
Run this now please. You should get this output:
Quote
2
3
4
5
6
7
8
9
10
With me still? I hope so.
You see that gray band in the left end of the code? I don't have a picture... you just have to find it yourself. Anyway, click it on a line there's some code. You should see a red dot appear. Click it again and the red dot disappears.
This is called a breakpoint. What is going to happen is when you run the code in debug mode (F5), it will stop execution on that line with the red dot. When it hits, the current line will have not executed yet.
We're going to walk this code all the way until it makes perfect sense why this skill is necessary to healthy development as a developer!
First, put a breakpoint on the line:
int start = 1;
Press F5 so that you run it debug mode. You will see this line light up immediately. If you haven't changed your theme, it's bright yellow - can't miss it.
Ignore locals for a minute if you have that window. Look at the current line. See, this line has not executed yet. Mouse over the word "start". You will see that start equals 0 because it has not been assigned.
The first trick we'll talk about is called STEP OVER. This is done with the keyboard shortcut F10. When you step over, you go to the next line down from what you were looking at and stop. So, press F10.
Mouse over start again. You will see that start is now equal to 1. It has executed. We are on the new line now:
int stop = 10;
Mouse over the word stop. Notice that stop equals 0. It has not been executed yet so the assignment has not happened. Press F10. Mouse over it again. Stop is now equal to 10.
Notice that the for loop is the next stop. In fact, just a segment of the for loop is in view of the breakpoint:
int current = start;
Mouse over current. Notice that it is equal to zero. Why? Because it has not executed yet. Mouse over start and notice it's equal to 1. Why? Because that line has executed already. It was our earlier assignment.
Press F10.
Mouse over current. Notice it is now equal to 1. Why? Because it was assigned the value of start which was already assigned to 1.
Notice how different discussing code becomes by debugging? Notice that none of the code you read has to be a mystery? We'll dig into new tricks in a little bit, but I need you to stay with me on this for now. Trust me, I'm doing this out of love even if my tone is harsh and I feel like a drill instructor. It's just really important.
Anyway, we are now on a boolean condition. This is the second part of the for loop. Our highlight should now be:
current <= stop;
Mouse over current. It equals 1. Mouse over stop. It equals 10. However, mouse over <=. It equals True. We get to see the evaluation before it happens.
Press F10.
Whoa! we jumped INTO the for code! Our current debug line is:
{
Why do you think that is? Well, logic in the for loop executes first and THEN the iteration code executes. You probably already knew that but now you see it working first hand.
Press F10
Console.WriteLine(current);
Mouse over current. Notice it still equals 1. It has not been iterated yet.
Press F10
Now, check your console:
Quote
Press F10
}
No surprise there.
Press F10
current++
Well, you can F10 through that a few more times if you like OR you can put a new breakpoint on Console.ReadLine() and press F5.
That's the next trick. We can jump to the very next breakpoint OR continue execution normally by pressing F5 after hitting a breakpoint.
To recap, F5 enters debug mode or resumes execution if already in debug. It also will jump to the next debug statement. F10 steps over code. That is, it will go to the next line that you are currently viewing.
Take a look at console output while on breakpoint of Console.ReadLine()
Quote
2
3
4
5
6
7
8
9
10
Press F5 again. Now press any key in console.
OK. We're out of execution.
The next trick is step into. This is achieved via F11. Obviously, your code will have method calls and such and what step into does is go into those method calls.
Here's some new code:
using System;
namespace DebugDemo
{
class Program
{
static void Main(string[] args)
{
CountNumbers();
Console.ReadLine();
}
private static void CountNumbers()
{
int start = 1;
int stop = 10;
for (int current = start; current <= stop; current++)
{
Console.WriteLine(current);
}
}
}
}
Notice that this is the same code but we moved it to another method.
Put a breakpoint on the line:
CountNumbers();
Press F5.
See it stop on the breakpoint. Press F10. Notice that the code we have did not even get entered. You should move to your console and press a key. Take note of the numbers printed out. Everything executed identical.
Press F5.
This time press F11. Notice how it breaks into the method call. You may follow the steps in the beginning to step over all the code as before if you want.
Class dismissed. You should now be well on your way to becoming debugging masters.
For homework, I want you to view and do my design tutorials. I want you to breakpoint every open curly bracket you find and then run the application. I want you learn the differences between stepping into and stepping over very well. I want you to become familiar with the process of debugging.
For more homework, I want you to put a breakpoint on a program that you wrote. I want you to step through it all the way so pick one that ends and isn't more than 100 lines.
For more homework, I want you to write a program like the one above (as in small) and I want you to debug it as you go along. I suggest a temperature converter or something equally simple but somewhat involved.
Here are the tutorials:
Strategy
State
Factory
Last, I want you to actually thank my post if it is helpful to you (including the other tutorials). This also includes all tutorials you read on the site. Seriously, I saw one with around 50K views and only 3 thanks. What's up with that? You should be inspiring tutorial writers to write these if you are gaining from them.
Take care. Sorry if I was hard on you.






MultiQuote










|