1 / 23

COMPSCI 280 S2 2014 Enterprise Software Development

COMPSCI 280 S2 2014 Enterprise Software Development. Debugging applications, using properties Jim Warren – jim@cs.auckland.ac.nz. Slides in this section are largely based on Angela Chang's lecture notes with minor modification – thanks Angela!. Learning Objectives.

archie
Télécharger la présentation

COMPSCI 280 S2 2014 Enterprise Software Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 280 S2 2014Enterprise Software Development Debugging applications, using properties Jim Warren – jim@cs.auckland.ac.nz Slides in this section are largely based on Angela Chang's lecture notes with minor modification – thanks Angela!

  2. Learning Objectives Given where you’re up to with Assignment 2, I thought debugging might be a popular topic! • 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 COMPSCI280

  3. Three Types of Errors in Coding • 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 COMPSCI280

  4. Debugging • 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 COMPSCI280

  5. Debugger Settings • 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 COMPSCI280

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

  7. Code Stepping • 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. Step Out Command Window Call Stack Step Over Step Into Error List Break points Start Debugging Show Next Break All Watch 1 Immediate COMPSCI280 Stop Debugging Locals

  8. Breakpoints • Breakpoints tell the debugger that an application should break, 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 beginnning. COMPSCI280

  9. Breakpoints (con’t) • 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 COMPSCI280

  10. Break Mode • 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. COMPSCI280

  11. Viewing Data in the Debugger • 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 COMPSCI280

  12. Data Tips • Conditions • DataTips work only in break mode, and • Only with variables that are within the current scope of execution. • To display a DataTip • Choose Debug->StepInTo • 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. COMPSCI280

  13. Variable Windows • 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 COMPSCI280

  14. Locals Windows • 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 + sign that precedes the variable name. COMPSCI280

  15. Autos Windows • 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 COMPSCI280

  16. Watch Windows • 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 variablein a row of a watch window • You can open up to four watch windows, numberedWatch 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 COMPSCI280

  17. QuickWatch dialog box • 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 theCtrl+Alt+Q shortcut COMPSCI280

  18. Immediate Window • 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<Enter> ?Message<Enter> ?age+3<Enter> age=15 COMPSCI280

  19. Hit counts • 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) COMPSCI280

  20. Fields & Properties Attributes represent the internal “state” of a given instance of this class. class MyPoint { private int _x; private int _y; } • 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) public int X { get { return _x; } set { if (value > 0) _x = value; } } public int Y { get { return _y; } } Read-only property COMPSCI 280

  21. Fields Vs Property • 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. COMPSCI 280

  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 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 COMPSCI280

  23. Where we’re up to • You’re probably pretty darn focused on Assignment 2 • So next lecture will just be office hours during the lecture time slot • There will be one further lecture from me (Jim) after the assignment deadline COMPSCI 280

More Related