重拾VB628:Code Debugging

来自MSDN-2001-OCT: Visual Tools and Languages/Visual Studio 6.0 Documentation/Visual Basic Documentation/Using Visual Basic/Programmer’s Guide/Part 2: What Can You Do With Visual Basic/Debugging Your Code and Handling Errors/

1. Debugging your codes

(1) 3种错误:complie errors,run-time errors,logic errors.

(2) The Visual Basic title bar always shows you the current mode.

(3) The Locals window shows the value of any variables within the scope of the current procedure.

(4) If you select Break on All Errors from the Default Error Trapping State option group on the General tab on the Options dialog box (available from the Tools menu),Visual Basic disables error handlers in code,so that when a statement generates a run-time error,Visual Basic enters break mode. If Break on All Errors is not selected,and if an error handler exists,it will intercept code and take corrective action.

2. Some Debugging techniques

(1) Although it’s possible to set a breakpoint in a MouseMove event procedure or in a Timer event,this can cause unexpected results. The normal flow of events is disrupted when entering break mode; single-stepping through the code from within these procedures may present different behavior than that which would occur in run mode.

(2) Be sure to remove any Stop statements before you create an .exe file. If a stand-alone Visual Basic application (.exe) encounters a Stop statement,it treats it as an End statement and terminates execution immediately,without any QueryUnload or Unload events occurring.

It's usually better to use the Assert method rather than a Stop statement. The Assert method halts execution only when a specified condition isn't met; unlike the Stop statement,calls to the Assert method are automatically removed when the application is compiled.

(3) Visual Basic allows you to step into individual statements,even if they are on the same line. A line of code can contain two or more statements,separated by a colon (:). Visual Basic uses a rectangular outline to indicate which of the statements will execute next. Breakpoints apply only to the first statement of a multiple-statement line.

(4) With Visual Basic,you can set a different line of code to execute next,provided it falls within the same procedure.

3. Using the Immediate window

(1) Debug.Print [items][;]

(2) A question mark (?) is useful shorthand for the Print method.

(3) Referencing an unloaded form in the Immediate window (or anywhere else) loads that form.

(4) In break mode,you can set values with statements like these in the Immediate window:

BackColor = 255

(5) Although most statements are supported in the Immediate window,a control structure is valid only if it can be completely expressed on one line of code; use colons to separate the statements that make up the control structure. The following For loop is valid in the Immediate window:

For I = 1 To 20 : Print 2 * I : Next I

(6) You can use the Immediate window to run a procedure repeatedly,testing the effect of different conditions. Each separate call of the procedure is maintained as a separate instance by Visual Basic. This allows you to separately test variables and property settings in each instance of the procedure.

Visual Basic maintains a listing of the procedures executed by each command from the Immediate window. Newer listings are at the top of the list. You can use the Call Stack dialog box to select any instance of a procedure,and then print the values of variables from that procedure in the Immediate window.

4. 事件过程里的断点

(1) 在Mouse Down里设端点的话,the application assumes that the mouse button is still pressed down. You don't get a MouseUp event until you press the mouse button down again and then release it. 在Key Down里设断点也类似。

(2) If you break execution during a GotFocus or LostFocus event procedure,the timing of system messages can cause inconsistent results.

(3) The development environment cannot raise events while a modal form or message box is displayed,because of potential conflicts in the debugger. Therefore,events are suppressed until the modal form or message box is dismissed.

Suppression of events only happens in the development environment. Once a project is compiled,events will be raised even when a modal form or message box is displayed.

5. Special Debugging Considerations

(1) Testing and Using Command-Line Arguments: To test code that uses Command,you can specify sample command-line arguments from within the Visual Basic environment. The application evaluates sample command-line input the same way it does if the user types the argument.

(2) If you do not want debugging statements included in the application you distribute to users,use conditional compilation to conveniently delete these statements when the Make EXE File command is used.

(3) A Debug.Assert statement will never appear in a compiled application,but when you're running in the design environment it causes the application to enter break mode with the line containing the statement highlighted (assuming that the expression evaluates to False).

(4) Compile on Demand and Background Compile是缺省开着的,可以把它们关上或者Make an executable或者Choose Start With Full Compile from the Run menu来force Visual Basic to check the entire application for compile errors.

6. When you encounter a bug difficult to track down

(1) First and foremost,make a backup.

(2) Isolate the code. Attempt to identify the line or lines of code generating the error. Select the code,copy it,start a new project,paste the code into the new project,run the new project,and see if the error still occurs.

(3) Create a log file. Call the following procedure from various points in your program. You should pass in a string of text which indicates the current location of the code executing in your program.

Sub LogFile ( Message As String)
Dim LogFile As Integer
LogFile = FreeFile
Open "C:/VB/LogFile.Log" For Append As # LogFile
Print # LogFile , Message
Close # LogFile
End Sub

Sub Sub1 ()
'...
Call LogFile( "Here I am in Sub1")
'...
End Sub

(4) 替换控件:If possible,remove any third party controls and custom controls from your project. Replace them with Visual Basic standard controls.

(5) 简化操作系统:If you cannot resolve the problem with any of the above methods,then it is time to eliminate all other non-Visual Basic causes from the problem search space. Copy your AUTOEXEC.BAT and CONFIG.SYS files to backup files. Comment out any and all drivers and programs from these two files that are not absolutely essential to running your program under Windows. Change your Windows video driver to the standard Windows VGA driver. Shut down Windows and reboot your machine. This will eliminate the possibility that there is some other program or driver which is interfering with your program.

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...