User Tools

Site Tools


programming:csharpdebugging

Debugging in Visual Studio

Break-Points

The most basic Visual Studio tool to use in debugging is break-points. Break-points places a restriction on a line of code that the current line and lines proceeding the break-point don't execute until you tell it to continue. This allows the user to examine, step by step, the order of things being executed, as well as examine the data that is being worked with. Data such as variables that have been instantiated, other break-points in the code, stack traces and many other things can be looked at.
To set a break-point, click in the section of white space beside the line numbers in the UI next to the line you want to set a break point at. You'll see a red dot appear next to the line number that indicates that a break-point has been set. There are other things you can do with breakpoints in Visual Studio, which will be talked about next.

Example of a set break point:

Step Functions

Located in the toolbar during execution of an application, you'll find various arrow symbols that perform different functions that allow the user to navigate code during execution time.

Step Over

One of the basic toolbar functions is the step over function. Step over allows a program that has hit a break-point to execute the next line of code. This is particularly useful for those who want to examine steps and changes occurring in a program one line at a time. Note that multiple changes can occur when one line of code executes.

Step Into

Also located in the toolbar is the step into function. the step into function is different from the step over function, in that it allows you to go to a method/function definition that is being called in the code. This can be done anytime the line in question is a method/function call. Once the method/function is executed, it returns the user back to the next line after the stepped into method/function call.

Step Out

Another function on the toolbar is step out, which is the opposite of step into. This allows you to leave the definition of the method/function call you are currently on and go to the next step after. This means that it completes the call to the method/function and moves on, so keep that in mind.

Step Backward/Forward

Finally, there are the two “historical debugging” functions that allow the user to look at previously executed code. Step backward brings the code back one step in execution, engaging historical debugging mode. Keep in mind that this doesn't undo execution of previous code, it just allows you to look at the code execution history. Step forward does the inverse, but only up to the line of code you first used the step backward function on, or when historical debugging mode is disengaged.

Watch Variables

A Watch Variable is an object or variable in your code that you would like to keep track of. For instance you would like to watch an array as it changes.

The window should pop up by default in Visual Studios however if you closed it, the window can be hard to refind.

First While your program is running goto Debug menu

Then goto Windows drop down menu

Once there goto Watch

There will be four watches. They all do the same thing, pick one.

Now this window looks good, but how do we use it?

Glad you asked! Here's how…

First click on an empty space

Type a variable or object then hit enter. For myself I am putting the instance variable named player.

If the variable exists in the current context of where your program is at it should appear as shown below.

Because my variable is an instance of a class object, it has some drop down of its own that we can expand and view all variables in that class object.

This window can help you track down any potential logic errors you may have just by looking at how the data changes over time.

Logical Errors

A logical error is a mistake in a program's source code that results in incorrect or unexpected behavior. It is an error at runtime that may simply produce the wrong output or may cause a program to crash while running. Many different types of programming mistakes can cause logic errors. For example, assigning a value to the wrong variable, multiplying two numbers instead of adding them can produce unwanted results also small typos that do not produce syntax errors can cause logic errors.

IF Statements

A Common mistake in if statements is when you are accidentally assigning a variable instead of testing something for example if you have

  if (x = 1)
  {
     //Code;
  }

What the statement x = 1 is trying to do is assign the value '1' to the object 'x'

What you really want is something like this

  if (x == 1)
  {
    //Code;
  }

Another Common mistake for if statements is having multiple of the same conditions or having inline if statement for example

  if (x == 0)
  {
    //code;
  }
  if (x == 1)
  {
    //code;
  }
  if (x == 0)
  {
    //code;
  }

The problems above are as follows.

1) The if statements are all inline so all if statements will be ran

2) The third if statement that has the condition (x == 0) is unnecessary and redundant because of the first if statement condition.

A simple fix could be this.

  if (x == 0)
  {
    //code;
  }
  else if (x == 1)
  {
    //code;
  }
  else
    //code;

This does not repeat code and only runs one if statement

Loops

For loops you can have a few things wrong one common mistake is the infinite loop it may look something like

int x = userinput();
 
while (x != 0)
{
  if (x > 0)
    {
      //Code;
    }
  else if (x < 0)
    {
      //Code;
    }
  else
    //Code;
 
}

The problem with this code is that the variable x never changes with in the loop, there for the loop never stops.

A Simple fix is

int x = -1;
 
while (x != 0)
{
  x = userinput();
 
  if (x > 0)
    {
      //Code;
    }
  else if (x < 0)
    {
      //Code;
    }
  else
    //Code;
 
}

By declaring the variable outside the loop and having the user input values when they are in the loop your loop has a possibility to end.

Back to the Programming Main Page

programming/csharpdebugging.txt · Last modified: 2020/10/01 23:03 by anichinis