Presentation is loading. Please wait.

Presentation is loading. Please wait.

Debugging applications, using properties Jim Warren – COMPSCI 280 S2 2015 Enterprise Software Development.

Similar presentations


Presentation on theme: "Debugging applications, using properties Jim Warren – COMPSCI 280 S2 2015 Enterprise Software Development."— Presentation transcript:

1 Debugging applications, using properties Jim Warren – jim@cs.auckland.ac.nz COMPSCI 280 S2 2015 Enterprise Software Development

2 Learning Objectives COMPSCI2802  To be able to take advantage of the features of an IDE such as VS in debugging your applications  Including setting break points and watch windows and working effectively in break mode  To understand the value of, and be able to apply Properties to improve the software quality of classes that you define  Additional information on debugging in VS at:  http://msdn.microsoft.com/en-us/library/k0k771bt.aspx http://msdn.microsoft.com/en-us/library/k0k771bt.aspx Given where you’re up to with Assignment 2, I thought debugging might be a popular topic!

3 Three Types of Errors in Coding COMPSCI2803  Syntax errors  Syntax Error (compile error) is a programming mistake that violates the rules of the language.  VS points out syntax error while you type program statements  VS prompts if you attempt to run a program before you fix all the syntax errors.  Run-time errors  A run-time error is a mistake that causes a program to stop unexpectedly during execution  Logic errors  A logic error is where the program runs, but a programming mistake makes the program code produce the wrong results.  Debugging efforts are particularly focused on tracking down the logic errors, but also help with understanding sources of run-time errors

4 Debugging COMPSCI2804  The Visual Studio debugger is a powerful tool that allows you to observe the run-time behavior of your program and locate logic errors.  With the debugger, you can break, or suspend, execution of your program to  examine your code,  evaluate and edit variables in your program  The debugger works with several files in order to cross-reference memory to the source files used to build the assembly  A program database (PDB) file holds debugging and project state information that allows incremental linking of a debug configuration of your program.  To debug your code, the debugger needs PDB files containing symbols for your application

5 Debugger Settings COMPSCI2805  You can specify various settings for debugger behavior,  including how variables are displayed,  whether certain warnings are presented,  how breakpoints are set, and  how breaking affects running programs.  To set debugger options  Choose Tools -> Options.  Expand Debugging

6 Debugger Roadmap COMPSCI2806  Code Stepping  Step Into  Step Over  Step Out  Breakpoints  Insert Breakpoint  Break at Function  Break Mode  Run to Cursor  Call Stack Viewing Data in the Debugger DataTips Variable Windows  Locals  Autos  Watch  Quick Watch Immediate Window Hit Counts

7 Code Stepping 7  One of the most common debugging procedures is stepping:  executing code one line at a time.  Choose Debug -> Step Into/Over/Out  Step Into/Over  Instruct the debugger to execute the next line of code  Note:  Step Into executes only the call itself for a function call, then halts at the first line of code inside the function.  Step Over executes the entire function, then halts at the first line outside the function.  Step Out resumes execution of your code until the function returns, then breaks at the return point in the calling function. Stepping controls Stop debugging

8 Breakpoints COMPSCI2808  Breakpoints tell the debugger that an application should break – i.e. pause execution, at a certain point.  When a break occurs, your program and the debugger are said to be in break mode  To set a simple breakpoint  Click on a line of executable code in the source window  Choose Debug->Toggle Breakpoint, or  Right-click, choose Breakpoint->Insert Breakpoint  To start/stop debugging  Choose Debug->Start/Stop Debugging  To stop debugging and restart  Choose Debug->Restart.  Note: Restart stops the current debugging session and restarts the project from the beginning.

9 Breakpoints (con’t) COMPSCI2809  To insert a function breakpoint  In a source window, click on the name of a function.  Choose Debug->New Breakpoint->Break at Function.  To set a breakpoint on a function call  In the Call Stack window, right-click the function call  Choose Insert Breakpoint  To delete a breakpoint  In the Breakpoints window, right-click on a breakpoint, and choose Delete from the shortcut  To disable/enable a breakpoint  Right-click on a line containing the breakpoint  Choose Disable/Enable Breakpoint from the shortcut menu

10 Break Mode COMPSCI28010  Breaks execution:  When execution reaches a breakpoint  Or when an unhandled exception occurs  Break execution manually at any time  Choose Debug->Break All  Break at the location where the cursor is set.  In a source window, right-click on a line of executable code, and  Right-click, choose Run to Cursor  Break at a specified function  Choose Debug->Windows->Call Stack  In the Call Stack window, right-click the function name and choose Run To Cursor from the shortcut menu.

11 Viewing Data in the Debugger COMPSCI28011  The Visual Studio debugger provides a variety of tools for inspecting and modifying the state of your program.  Most of these tools function only in break mode.  DataTips  Variable Windows  Locals  Autos  Watch  QuickWatch dialog box  Immediate Window  Hit Counts

12 Data Tips COMPSCI28012  Conditions  DataTips work only in break mode, and  Only with variables that are within the current scope of execution.  To display a DataTip  Enter break mode  In a source window, place the mouse pointer over any variable in the current scope.  Expanding Information  With enhanced DataTips, you can expand an array, structure, or object to view its members.  To expand a variable to see its elements  Hover the mouse cursor over the + sign that precedes the variable name.  In the case of variable assigned a LINQ query, this may cause the query to be executed

13 Variable Windows COMPSCI28013  The debugger provides a number of variable windows for displaying, evaluating, and editing variables and expressions.  Each variable window includes a grid with three columns: Name, Value, and Type.  The Name column contains variable names or expressions.  The Value and Type column display the value and data type of the variable or expression.  Variable windows  Locals  Autos  Watch

14 Locals Windows COMPSCI28014  Locals Window  It displays variables local to the current context or scope.  Usually, this means the procedure or function you are currently executing.  Note: the debugger populates this window automatically.  To open the Locals window  Enter break mode  Choose Debug->Windows->Locals  To expand a variable to see its elements  Click with the mouse on the triangle arrow that precedes the variable name.

15 Autos Windows COMPSCI28015  Autos Window  It displays variables used in the current line of code and the preceding line of code.  Again, the debugger populates this window automatically.  To open the Autos window  Enter break mode  Choose Debug->Windows->Autos

16 Watch Windows COMPSCI28016  Watch window  It is where you can add variables whose value you want to watch.  Note you can undock the window from its default position  You can add variables (e.g. “j”) or any valid expressions (e.g. “(j+i)*2”)  You can also interactively edit/change the value of a variable in a row of a watch window  You can open up to four watch windows, numbered Watch 1, Watch 2, Watch 3, and Watch 4.  To open the watch window  Enter break mode  Select the variable you want to add from the Code editor, right click on it, and click the Add Watch command  Or choose Debug->Windows->Watch 1

17 QuickWatch dialog box Handout15COMPSCI28017  The QuickWatch dialog box is similar in concept to the Watch window, but QuickWatch can display only one variable or expression at a time.  Useful when you want to take a quick look at a variable or expression without bringing up the Watch window.  QuickWatch is a modal dialog box, you have to close it before you can continue debugging  To open the QuickWatch window  Enter break mode  Select the variable you want to add from the Code editor, right click on it, and click the Quick Watch command to open the QuickWatch Dialog box  To add a QuickWatch expression to the Watch window  Click the Add Watch button  Enter the expression and press Enter.  To make it really ‘quick’ use the Ctrl+Alt+Q shortcut

18 Immediate Window COMPSCI28018  The Immediate window is used to debug and evaluate expressions, execute statements, print variable values, and so forth.  It allows you to enter expressions to be evaluated or executed by the development language during debugging.  To display the Immediate window  Enter break mode  Choose Debug->Windows->Immediate.  Type the following in the Immediate window in a break mode  To modify a variable using the Immediate window  Enter break the Immediate window  Type “age=15” and press Enter  Watch the changes in the Watch 1 window ?age ?Message ?age+3 age=15

19 Hit counts COMPSCI28019  Can further tailor the behaviour for watching a line of code  Right-click a break point and select Hit Count…  Can set the break point to only be invoked when the line of code has been reached a certain number of time (or on a multiple of n times)

20 Fields & Properties  Fields  Fields store the data a class needs to fulfill its design  Fields should (generally) be declared as Private.  Properties  Properties are retrieved and set like fields, but are implemented using property Get and property Set methods, which provide more control on how values are set or returned.  It helps isolate your data and allows you to validate values before they are assigned or retrieved.  Property has Public access  The value keyword is used to define the value being assigned by the set method.  Property can be read-only (with the Get portion only) or write-only (with the Set portion only) COMPSCI 28020 class MyPoint { private int _x; private int _y; } Attributes represent the internal “state” of a given instance of this class. public int X { get { return _x; } set { if (value > 0) _x = value; } public int Y { get { return _y; } Read-only property

21 Fields Vs Property COMPSCI 28021  Use property when:  You need to control when and how a value is set or retrieved.  The property has a well-defined set of values that need to be validated.  Setting the value causes some perceptible change in the object's state, such as an IsVisible property.  Setting the property causes changes to other internal variables or to the values of other properties.  A set of steps must be performed before the property can be set or retrieved.  Use field when:  The value is of a self-validating type.  Any value in the range supported by the data type is valid. This is true of many properties of type Single or Double.  The property is a String data type, and there is no constraint on the size or value of the string.

22 Auto-implemented properties  Auto-implemented properties make property declaration more concise when no additional logic is required in the property accessor methods  Compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors COMPSCI28022 public class emp { public decimal salary { get; set; } } public class emp2 : emp { public new decimal salary { get {return base.salary;} set { if (value >= 0) base.salary = value; else Console.WriteLine("Cannot accept negative salary!"); } Auto-implemented accessors for anonymous private variable (the ‘backing field’) A child class can override the parent property and selectively reuse the parent (‘base’) class get and set methods

23 Conclusion… and where we’re up to  IDEs offer a range of features for debugging  Breakpoints  Tooltip and window based review of variables  Windows to watch variables  Ability to manipulate variables in the running program in the Immediate window  Properties provide a logical layer over fields  You should be pretty darn focused on Assignment 2  Next lecture we flesh out C# language features around exception handling COMPSCI 28023


Download ppt "Debugging applications, using properties Jim Warren – COMPSCI 280 S2 2015 Enterprise Software Development."

Similar presentations


Ads by Google