Computer Programming Visual Studio Debugging Computer Programming Using
Computer Programming Visual Studio Debugging
Computer Programming Using the Debugger The problem The traditional approach The Visual Studio/Express Debugger Breakpoints Locals and Watch windows Code Stepping Advanced features
Computer Programming Debugging – the problem You should always test your programs to identify any problems and confirm that they meet the initial design specifications If your program doesn’t behave as you intended, you’ll need to debug any errors and/or correct the design of your coding The most basic type of debugging (still widely used even by advanced programmers) is "print debugging" – you place instructions into your code to print out some text or a number somewhere. This lets you to see which part of your code has been reached, and what specific variables may contain at that point. The Visual Studio/Express IDE provide much better tools, which are just as easy to use, once you learn the most basic principles - which is the topic of this lecture
Computer Programming Using the debugger The best approach to debugging and correcting design errors in. NET programs is to use the Visual Studio/Express Debugger to find, understand, and then fix, any bugs and coding mistakes It is a much more efficient approach than the traditional student method of bug-busting: guess at a solution – try again guess at another solution – try again etc. , etc. ”
Computer Programming Debug/Release modes. . . First, you may not have noticed, but you can build and execute a program in either debug or release mode Debug mode adds extra information (stored in a. pdb file) which allows the debugger to: ; track the values of variables; step through the code line-by-line; modify variables at runtime; and other useful debugging things In Debug mode your executable file (*. exe) is created in the path: /solution. Name/project. Name/bin/Debug (note: the solution. Name and the project. Name are set when you first save the solution – often they are goiven the same name )
Computer Programming Debug/Release modes. . . When your program is tested and working you should rebuild it in Release mode: this creates a smaller exe file that is optimised for running faster In Release mode your executable file (*. exe) is created in the path: /solution. Name/project. Name/bin/Release (this is the version you would normally pass-on to others if you wish to share your program)
Computer Programming Debug essentials: Breakpoints The most basic debugger feature are ‘breakpoints’ When running in debug mode the application will pause and return to the editor when it hits a breakpoint. It lets you see what the program is doing at that point of execution. You can then: inspect the current values of variables; evaluate expressions; begin stepping through the code line-by-line to see where the flow of execution moves to
Computer Programming Debug essentials: Breakpoints cause your code to pause at that point You can set as many as you like Set/Clear by clicking in the left-edge of a code line Once halted – you can watch each statement execute
Computer Programming Debug essentials: Breakpoints The first really cool thing you can do once a Break. Point has halted the program execution, is. . . just hover your mouse over any of the variable names printed in your code, and the IDE tells you what they contain!
Computer Programming Debug essentials: Locals listing This is often enough in itself to help you understand what is going wrong. But if you want to see the values of all the Locals (i. e. all local variables in the current method that is being executed) just select. . .
Computer Programming The Locals window lists all the current variables: – their contents and data type. What’s more, as you step through your code (as we’ll see in a mo. ) the table is constantly updated to show values change as the program executes
Computer Programming Debug essentials: Watch window You can also ask for the Watch window, and then add the names of specific program items that you wish to follow. . .
Computer Programming Debug essentials: Code Stepping Often when debugging, you want to watch and understand the program execution flow. The line that will be executed next is highlighted in yellow when a Break. Point has been activated. You have several options on how to move on from this point. . . First, clicking the green arrow in the toolbar (or typing F 5) will restart the program execution. It will either take you to the next breakpoint (if one exists and is encountered) or run until the normal end
Computer Programming Debug essentials: Code Stepping To advance line-by-line use F 11 on the keyboard, or the Step Over button on the Debug toolbar. . . Where a method call is encountered, this will “Step Into” the method code and proceed line-by-line You may prefer to “Step Over” (F 10) a method call – that is, execute it and come back to the calling point without going through the method code itself on a line-by-line basis
Computer Programming Debug essentials: Code Stepping You can also use the current line pointer to move the current execution point backward and forward within your code …it can be useful, for instance, to pop back, and go again Finally, the Immediate window is very powerful, allowing you to both view and modify current variable values
Computer Programming Debug essentials: Advanced B/Ps In Visual Studio (but not in Visual Express) you can do even more with Break. Points… Condition – lets you specify a condition that must be true or changed, for the breakpoint to be ‘hit’. This lets you halt execution only under certain circumstances Hit Count – lets you define an alternative condition, based on the amount of times the breakpoint has been hit. E. g. You might decide a breakpoint should not halt execution until it has been ‘hit’ a certain number of times
Computer Programming Using the debugger In general, the combination of examining current variable values and stepping through each statement to follow the execution flow, will be enough for you to understand the behaviour of your coding and identify the reason(s) why it is not behaving as you had anticipated Once you understand the problem, you are then in a much more powerful position to redesign the code so that it meets your program specifications
- Slides: 17