微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

重拾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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强制返回为文本 -------------------------------- 数字类型的格式化 --------------------------------     固定格式参数:     General Number 普通数字,如可以用来去掉千位分隔号     format$("100,1
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办法, Format 或者FormatDateTime 竟然结果和系统设置的区域语言的日期和时间格式相关。意思是尽管你用诸如 Format(Now, "MM/dd/yyyy"),如果系统的设置格式区域语言的日期和时间格式分隔符是"-",那他还会显示为 MM-dd-yyyy     只有拼凑: <%response.write
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace My ‘全局错误处理,新的解决方案直接添加本ApplicationEvents.vb 到工程即可 ‘添加后还需要一个From用来显示错误。如果到这步还不会则需要先打好基础啦 ‘======================================================== ‘以下事件
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用的爽呀,这篇文章写与2011年,看来我以前没有认真去找这个方法呀。 https://blog.csdn.net/chzjxgd/article/details/6176325 金蝶K3 BOS的插件官方是用VB6编写的,如果  能用.Net下的语言工具开发BOS插件是一件很愉快的事情,其中缘由不言而喻,而本文则是个人首创,实现在了用V
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选中的单元格进行处理 Dim m As Range, tmpStr As String, s As String Dim x As Integer, y As Integer, subStr As String If MsgBox("确定要分列处理吗?请确定分列的数据会覆盖它后面的单元格!", _
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single) 2 Dim path As String, hash As String 3 For Each fil
  Imports MySql.Data.MySqlClient Public Class Form1 ‘ GLOBAL DECLARATIONS Dim conString As String = "Server=localhost;Database=net2;Uid=root;Pwd=123456;" Dim con As New MySqlConnection
‘導入命名空間 Imports ADODB Imports Microsoft.Office.Interop   Private Sub A1() Dim Sql As String Dim Cnn As New ADODB.Connection Dim Rs As New ADODB.Recordset Dim S As String   S = "Provider=OraOLEDB.Oracl
Imports System.IO Imports System.Threading Imports System.Diagnostics Public Class Form1 Dim A(254) As String    Function ping(ByVal IP As Integer) As String Dim IPAddress As String IPAddress = "10.0.
VB运行EXE程序,并等待其运行结束 参考:https://blog.csdn.net/useway/article/details/5494084 Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long Pr