Vs using C # To develop application debugging

Source: Internet
Author: User

Entry

Suppose you are a programmer with A. NET platform and use Visual Studio as a development tool.

Breakpoint: The simplest method is to set a breakpoint. When the program is executed, it is automatically interrupted and enters the debugging status. Set the breakpoint. In the code line that you think is faulty, click on the left side to display a red dot, that is, the breakpoint.

Enable Mode: Press F5, or the menu bar --- --- to start debugging, or the toolbar icon

 

Quick monitoring: You can quickly view the value of a variable or expression, or customize an expression for calculation.

 

One-step execution

There are three types: one is to execute one row (F10) each time; the other is to execute one row each time, but will jump to the called function (F11) when a function call occurs ); one is to directly execute the remaining commands in the current function and return the upper-level function (SHIFT + F11 ).

Another kind of regret is to set it to the next sentence (set next statement), that is, the statement that will be executed in the next sentence (right-click the setting or shortcut key: Ctrl + Shift + F10 ), however, when debugging data-related statements, setting the next sentence may report exceptions. For example, when you add rows to a able, existing rows cannot be added to the able repeatedly.

Monitoring

The debugger may automatically list the values of some related variables, but you may also be concerned about the values of other variables. You can add monitoring for these variables. You can also monitor the value of an expression, such as A + B. However, it is recommended that you do not modify the value of a variable in this expression. For example, if you monitor a ++, the value of a is modified during monitoring, which affects the running result of the program.

 

Debugging skills

Using shortcut keys can greatly improve our debugging efficiency. common debugging shortcut keys:

F5 start debugging

F10 executes the next line of code, but does not execute any function call.

After the F11 execution enters the function call, the code is executed one by one.

Shift + F11 the remaining row of the function where the current execution point is located.

Shift + F5 stop the current application in the running program. It can be used in interrupt and run modes.

Drag breakpoint

During debugging, we can drag the breakpoint so that the program runs to the desired place. It is usually used to verify whether this code has any impact on the running results of the program. Because we drag the code, the filtered code will not be executed. Compared with the original code, we can see the impact of removing this code.

 

Conditional interruption

If you write a for loop with a large number of cycles, the following code shows that there will be exceptions when I = 50, then we cannot press F5 50 times to debug the code, otherwise this efficiency ....

Private void conditiondebug ()
{
For (INT I = 0; I <100; I ++)
{
If (I = 50)
{
// Some error code here
Console. writeline ("I = 50 here ");
}
}
}

We can directly use the function provided by Vs to modify the value of variable I. Once I = 0 is opened, it is just entering the for loop. We set the I to 49 and press enter to debug it again, I = 50, as shown in figure

Of course, we can also directly write code in the code to achieve this purpose. The Code is as follows:

Private void conditiondebug ()
{
For (INT I = 0; I <100; I ++)
{
System. Diagnostics. Debug. Assert (I! = 50 );
If (I = 50)
{
// Some error code here
Console. writeline ("I = 50 here ");
}
}

}

When the assert (assert) in debugging is used, the following prompt box is displayed after the program is executed. Click ingore (ignore,

 

Immediate window

During debugging, the immediate window calculates the expression value, executes the statement, and prints the variable value. We enter the command (note that it must start with ">"), there will be smart prompts, And the names are all self-explanatory.

For example, if you want to know the value of I, you can enter the name> Debug. Print I (or simply use it>? I), such

 

ImmediateWindow also has more powerful usage, the return value of the calculation method (if any)

If this function is available

Int methodvalue (int)

 

{

If (A = 1)

{

Return 1;

}

Else

{

Return 0;

}

}

We can useImmediateCommand>? Class. Method (ARGs) to call this method, as shown in figure

Where p is the instance of the current class (because methodvalue is the class method, note? And expression)

 

 

Use debug for some real-time programs (such as socket. write () writes errors to log files ,. net can write the debug information to the file you specified, remember that the write in and out information is not necessarily error information, it can also be some important information about the running of your program, when you find that a module has a problem but cannot determine the location during debugging, you can use this method. If an error occurs only one day, then you must use this method.

Instance

InvolvedWS(WebServices) Debugging

In the actual development based on winform, we often use WS for data transmission. We get the collected data at the front end and pass the data to the backend through ws, after the backend completes the corresponding business logic processing, it will last in the database. We often write related code in WS, such as identity authentication, log records, and prompt information. How can we debug the code.

InvolvedJavascriptDebugging

Many programmers are confused about debugging JavaScript, because there is no good debugging tool. Some people like to use firebug to debug JavaScript, which is indeed a good choice. firebug provides a lot of JavaScript Information and is a good tool for debugging JavaScript. Next, I will introduce how to use Visual Studio to debug Javascript. in Visual Studio, debugging JS is similar to debugging C #, and breakpoint is set, the difference is that we need to pay attention when viewing element values.

InvolvedAjaxDebugging

Nowadays, Ajax is very popular, but it comes with debugging difficulties. Most Junior programmers do not know how to effectively debug the background code from the front-end to a lot of imperfect Ajax applications.

The following uses a simple example to describe how to use Visual Studio to debug JavaScript. The instance uses ajax to verify user logon. If the verification succeeds, the system prompts "Logon successful". Otherwise, the system prompts "Logon Failed ".

The following is the main code. We use jquery to implement Ajax and make a deliberate error in the background file.

The correct username and password are admin and 1.

The debugging method is as follows: Set a breakpoint at the entrance of the background, set a breakpoint at the method of calling the background in the foreground JS, and then press F5 to start debugging. After we enter the user name and password, click log on and you will find that the front-end breakpoint is triggered.

Press F5 to continue debugging. Sometimes it will jump to the jquery source code. If you continue with F5, you will find that the breakpoint is executed in the background, as shown in

The background code debugging is very simple. (PS: Sometimes you can directly go to the background for debugging without setting breakpoints on the foreground. If not, you can set breakpoints in HTML or aspx files on the foreground where errors may occur, step-by-Step debugging)

 

Some common errors during debugging (will be updated one after another ):

1. When we debug a code sentence, we suddenly jumped out unexpectedly. In fact, this sentence was just executed with an exception. We can use try... Catch to catch exceptions, check the causes of exceptions, and then handle the exceptions accordingly.

2. In ADO. net, we will use the DS. Merge () method to merge memory tables. If there is an exception, there are generally three situations:

A. One of the tables has two rows of identical data, including the primary key.

B. The two tables have different structures.

C. The type of a field in the two tables does not match. For example, if the field age is a Chinese string in Table A, it is indeed a decimal in table B.

If you have collected some common errors, I hope you can share them.

 

You will find that I is 50 now. If you are interested, you can check other usage of assert.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.