VB Net Debugging (Console)

Similar documents
Outline. Debugging. In Class Exercise Solution. Review If Else If. Immediate Program Errors. Function Test Example

Programming Logic - Beginning

After completing this appendix, you will be able to:

Using loops and debugging code

1 Introduction to MARS

Concept: Debugging. Goal. Description. Implementation. Use the debugging tools built into LabVIEW.

Debugging and Handling Exceptions

1. Move your mouse to the location you wish text to appear in the document. 2. Click the mouse. The insertion point appears.

Introduction to IntelliJ

2 TUTORIAL. Overview. VisualDSP Getting Started Guide 2-1 for SHARC DSPs

Step through Your DATA Step: Introducing the DATA Step Debugger in SAS Enterprise Guide

Starting Embedded C Programming CM0506 Small Embedded Systems

Introduction. Key features and lab exercises to familiarize new users to the Visual environment

Under the Debug menu, there are two menu items for executing your code: the Start (F5) option and the

Supplement: Visual C++ Debugging

Debugging in Small Basic is the process of analysing a program to detect and fix errors or improve functionality in some way.

A Tutorial for ECE 175

Chapter 2.6: Testing and running a solution

Parallel Debugging. ª Objective. ª Contents. ª Learn the basics of debugging parallel programs

SDKs - Eclipse. SENG 403, Tutorial 2

MICROSOFT WORD 2010 BASICS

Visual Studio.NET. Although it is possible to program.net using only the command OVERVIEW OF VISUAL STUDIO.NET

1.00 Lecture 2. What s an IDE?

12/14/2016. Errors. Debugging and Error Handling. Run-Time Errors. Debugging in C# Debugging in C# (continued)

BasicScript 2.25 User s Guide. May 29, 1996

3 TUTORIAL. In This Chapter. Figure 1-0. Table 1-0. Listing 1-0.

NEW CEIBO DEBUGGER. Menus and Commands

11Debugging and Handling. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

Using the Xcode Debugger

MARK SCHEME for the May/June 2011 question paper for the guidance of teachers 9691 COMPUTING. 9691/23 Paper 2 (Written Paper), maximum raw mark 75

INFS 214: Introduction to Computing

Reference Services Division Presents WORD Introductory Class

(Subroutines in Visual Basic)

Introduction to Problem Solving and Programming in Python.

SQL Server. Management Studio. Chapter 3. In This Chapter. Management Studio. c Introduction to SQL Server

Word - Basics. Course Description. Getting Started. Objectives. Editing a Document. Proofing a Document. Formatting Characters. Formatting Paragraphs

Microsoft Office Word 2010

Eclipse Tutorial. For Introduction to Java Programming By Y. Daniel Liang

The NetBeans Debugger: A Brief Tutorial

Computer Science II Lab 3 Testing and Debugging

Text Input and Conditionals

7 The Integrated Debugger

Introduction to Programming. Writing Programs Syntax, Logic and Run-time Errors

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

Laboratory Assignment #4 Debugging in Eclipse CDT 1

MadLab JellyBean. version 1.0. Written by James Hutchby Copyright MadLab Ltd All Rights Reserved.

We first learn one useful option of gcc. Copy the following C source file to your

Teach Yourself Microsoft Word Topic 10 - Margins, Indents and Tabs

Assembly Language Fundamentals

Jerry Cain Handout #5 CS 106AJ September 30, Using JSKarel

THE JAVA FOR STATEMENT

Prerequisites for Eclipse

Security Coding Module - Buffer Overflow Data Gone Wild CS1

ME 142 Engineering Computation I. Debugging Techniques

OCR J276 GCSE Computer Science

1. Managing Information in Table

Lab 8 - Vectors, and Debugging. Directions

ENGG1811 Computing for Engineers Week 9 Dialogues and Forms Numerical Integration

Introduction to C# Applications

Debugging. CSE 2231 Supplement A Annatala Wolf

Object Oriented Programming with Visual Basic.Net

Introduction. The Google Documents interface When you create a document in Google Documents, the interface for Documents will appear.

Code::Blocks Student Manual

GETTING STARTED WITH ECLIPSE Caitrin Armstrong

The Turing Environment

Supplement H.1: JBuilder X Tutorial. For Introduction to Java Programming, 5E By Y. Daniel Liang

Supplement II.B(1): JBuilder X Tutorial. For Introduction to Java Programming By Y. Daniel Liang

WORD 2010 TIP SHEET GLOSSARY

Microsoft Office Word 2016 for Mac

First, let s just try to run the program. When we click the button we get the error message shown below:

Eclipse IDE. 1 of 20 9/27/06 8:15 AM

Visual Basic 2010 How to Program by Pearson Education, Inc. All Rights Reserved.

Microsoft Word Part I Reference Manual

Lab 0 Introduction to the MSP430F5529 Launchpad-based Lab Board and Code Composer Studio

NetBeans Tutorial. For Introduction to Java Programming By Y. Daniel Liang. This tutorial applies to NetBeans 6, 7, or a higher version.

11 Using JUnit with jgrasp

Part 4 - Procedures and Functions

At the shell prompt, enter idlde

Code::Blocks Student Manual

Bonus Lesson: Working with Code

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

Section 2. Opening and Editing Documents

Create a unit using United Streaming and PowerPoint. Materials: Microsoft PowerPoint, Internet access, United Streaming account

HOW TO USE CODE::BLOCKS IDE FOR COMPUTER PROGRAMMING LABORATORY SESSIONS

Watch the video below to learn the basics of working with text in Word. *Video removed from printing pages. Using the insertion point to add text

MULTIPROG QUICK START GUIDE

GUI Design and Event- Driven Programming

Intro to MS Visual C++ Debugging

Visual Basic Program Coding STEP 2

CSCI Object Oriented Design: Java Review Errors George Blankenship. Java Review - Errors George Blankenship 1

CSCI Object-Oriented Design. Java Review Topics. Program Errors. George Blankenship 1. Java Review Errors George Blankenship

How to access other maps when viewing or editing a map

Module 4: Working with MPI

Flow of Control: Loops

Getting Started (1.8.7) 9/2/2009

An Introduction to Komodo

Introduction to the ThreadX Debugger Plugin for the IAR Embedded Workbench C-SPYDebugger

Resource 2 Embedded computer and development environment

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing

Grids (tables) is one type of content available in the web Front end s tabs.

Transcription:

VB Net Debugging (Console) Introduction A bug is some sort of error in the code which can prevent your program from running properly. When. you write a substantial program always assume that it contains bugs. When you discover a bug you may or may not know how your code has caused it. Debugging is about finding these causes. Visual Basic has a variety of debugging tools, which are discussed here. The later part of the chapter looks at how your code might handle run-time errors, for which the normal process of debugging not relevant. It makes sense to know about Visual Basic's debugging tools reasonably soon after beginning the language. Handling run-time errors in code is a more advanced topic. Types of error There are three types of error: Syntax error Run-time error Logic or semantic error A syntax error is a mistake in the grammar of Visual Basic. Examples include misspelling keywords (e.g. Lop rather than Loop), forgetting to write a period (.) between a control's name and its property and so on. They are usually simple to fix since the compiler can pick them out for you A run-time error causes the program to stop working. An example was shown in figure 3.5, where trying to store too large a number into a Short variable caused overflow. A logic error results from a mistake in the logic of your code. Examples are using the wrong logical operators (AND/OR) in loop conditions or assigning an incorrect value to a variable. It is usually the hardest type of error to track down. The topic of debugging in this chapter is not concerned with syntax or run-time errors, but with tracking down logic errors. The topic of error handling is concerned with run-time errors so that your program won't crash. The three program modes At any time a given program is in one of three modes - design mode, run mode or break mode. Every program you have written has used the design and run modes. If your program produces a run-time error it automatically invokes break mode. You can also get into this mode by clicking the Break button. You can then hover the cursor over any variable and see its current value. Visual Basic's Debug toolbar and menu The Debug toolbar and menu are shown below. You'll be using several of these items in this exercise. To get the toolbar select View/Toolbars/Debug from the main menu. Stepping through code The Debug toolbar has three options, and the Debug menu two of these three options, for stepping through code - executing it one line at a time. The two options they both share are the most useful: Step Into Runs the current line of code. If this line calls a general procedure this procedure is entered. Step Over Runs the current line, but if this is a general procedure call the procedure is executed without stepping through its code. VBNetDebugging-console 1

Using Breakpoints A breakpoint is a place in the code where the program will temporarily stop. You can set as many of these as you like. When the program stops at a breakpoint you can examine the contents of variables and expressions in the Watch window or in the Immediate window. The programs below shows how to use the Watch window the Immediate window. Watches A watch allows you to see the value of a variable or expression as the program runs. As you step through the code this value is displayed in the Watch window. You can only add a watch when the program is in break mode. PROGRAM: Breakpoints, setting Watches and stepping through code Specification Demonstrate how to set a breakpoint, set watches on variables and expressions, and how to step through code. 1. Open a new project. 2. Type the following code: Sub Main() Dim Number, Counter As Integer Number = 2 For Counter = 1 To 10 Console.WriteLine("2 to the power {0} is {1} ", Counter, Number) Number = Number * 2 Next Counter Console.ReadKey() End Sub 3. Run the program and confirm that it keeps doubling the current number as shown below. 2 to the power 1 is 2 2 to the power 2 is 4 2 to the power 3 is 8 2 to the power 4 is 16 2 to the power 5 is 32 2 to the power 6 is 64 2 to the power 7 is 128 2 to the power 8 is 256 2 to the power 9 is 512 2 to the power 10 is 1024 4. To set a watch on a variable we must be in break mode. One way of getting into this mode is to set a breakpoint somewhere in the program. Click in the Margin Indicator bar to the left of Number = 2 as shown below. The line of code will be highlighted in brown and a red ball marker will appear next to it in the bar. VBNetDebugging-console 2

5. Run the program. Execution will stop at the breakpoint, which will now be highlighted in yellowwith a yellow arrow in the red marker ball. To set a watch on the variable Number, highlight it and right-click, and then select Add Watch. The Watch window will appear at the bottom of the screen. The current value of Number is shown as 0 because the breakpoint line code, which sets it to 2, has not yet been executed. 6 You can also put watches on expressions. In the Watch window add another watch as shown below. Type in Number > 500 and press Enter. We want to know when Number holds a vale. greater than 500. At the moment the expression is False. 7. Now we are going to execute the code line by line and see what happens in the Watch window. you can step through the code either by using the F8 key, clicking the Step Into icon on the Debug toolbar, or selecting the VBNetDebugging-console 3

Step Into item from the Debug menu. Press the F8 key once. The start of the For...Next loop is now highlighted and the value of Number is now 2 in the Watch window. 8. Press the F8 key three more times and the value of Number will be doubled to 4. Keep pressing this key until its value becomes 512. At this point the expression in the Watch window is True. 9. Select Debug/Stop Debugging or click Stop Debugging on the Debug toolbar to stop the program. Remove the breakpoint by clicking on the circle in the Margin Indicator bar. VBNetDebugging-console 4

The Immediate Window The Immediate window lets you do a variety of things such as look at and change the contents variables. As with the Watch window these can only be done in break mode. PROGRAM: The Immediate Window Specification : Demonstrate the use of the Immediate Window This is a simple program that asks the user to enter a product's unit price and the quantity bought. If the total price exceeds 200 there is a 10% discount. It outputs the total price after discount. 1. Open a new project and type the following into the form's Load event procedure: Sub main () Dim Quantity As Short Dim Price, Discount, TotalPrice As Decimal Console.Write("Enter price :") Price = console.readline() Console.Write("Enter quantity") Quantity = console.readline() If Price * Quantity > 200 Then Discount = Price * Quantity * 0.1 'discount is 10% End If TotalPrice = Price * Quantity - Discount Console.Writeline( Total price is {0}",TotalPrice) End Sub 2 Run the program to check that it works. 3 Place a breakpoint on the line with the If condition. 4 Run the program so that it stops at the breakpoint. Select Debug/Windows/Immediate from the main menu to get the immediate window. 5 In the Immediate window set the values of Price and Quantity as shown below. Press Enter after each line. VBNetDebugging-console 5

6. Press the F8 key twice to step through the code to the end of the If statement. In the Immediate window display the value of the variable Discount by typing in?discount and pressing Enter. The result is shown below. The discount is 30 (10% of 50 x 6). The D after the 30 indicates that the decimal numbering system is used. 7. Remove the breakpoint and stop debugging the program. VBNetDebugging-console 6