装配中的脑袋

用程序装配大脑,再用大脑装配程序
随笔 - 118, 评论 - 1214, 引用 - 11

导航

工具

关于

如果想发较大的信件,请用Ninputer @ gmail.com

不要在我的Blog评论中张贴广告,除非同意向我付款。

标签

每月存档

广告



访客

 

关键字:VB8.0 VB.NET Whidbey Edit and Continue E&C VB2005

我们都有调试程序的经验,当程序发生了异常或遇到断点中断执行时,如果我们发现代码中有问题需要修改,一般的开发工具需要完全重新编译后再次运行到此处才能检验是否修改正确。有时候项目重新运行到所需位置的速度很慢或者操作非常复杂时,完全编译再调试的执行方式就显得十分笨拙和低效。在Visual Basic还能够解释执行的时代,Visual Basic程序员都有一项非常便利的调试手段,那就是编辑后继续运行。我们可以在程序中断的情况下修改代码,然后立即从此处继续运行,有时甚至可以跳过出现异常的语句进行下去。这样,即使我们要反复修改代码并运行,也可以完全在调试时进行,这是多么方便。不过Visual Basic .NET没有这一功能,这让很多Visual Basic程序员十分扫兴,因为这是VB开发中的巨大优势之一。Visual Basic 2005重新加入了这一功能,让广大Visual Basic程序员又可以享受编辑后继续运行带来的便利。这一特性是在CLR层次上实现的,因此理论上任何建立在CLR上的语言都可以实现编辑后继续运行,但是Visual Basic在所有语言中最适合拥有这个特征,因为Visual Basic在书写代码时就立即编译,只有这样,中断时作出的修改才有可能立刻得以运行。在Visual Studio 2005中,目前也确实只有Visual Basic 2005带有这个功能。

演练

首先我们看看编辑后继续运行是如何工作的。这段代码将产生被零除的异常,我们用它来展示编辑后继续运行的功能:

Dim i, j As Integer
i = 1
j = 0
MsgBox(i \ j)

运行这段代码,我们将得到一个运行时异常,如图所示。

根据异常助手的指示,我们发现问题在于j在运行时被赋予0,而在进行除法时没有对j的值进行验证。这时,关闭异常助手,便可以立刻修改代码。比如我们修改成

If j <> 0 Then MsgBox(i \ j)

我们注意到编辑器左边有个黄色的箭头。那个箭头叫做instruction pointer,指示是将要执行的代码。如图所示:

现在我们就可以单击“运行”按钮继续运行了,正如预期的那样,异常不再发生。在中断状态,我们甚至可以手工控制运行的流程,而这个操作简单到只需动动鼠标。举个例子,假设刚才的错误发生时我们希望更改对j的赋值语句,然后运行看效果如何,那么我们可以在刚才中断下来的时候,将代码改为:

Dim i, j As Integer
i = 1
j = 1
MsgBox(i \ j)

这时黄色的箭头指向MsgBox语句,如果直接继续运行还是错误的结果。于是我们可以拖动黄色的箭头,使它指向j的赋值语句。如图所示:

接下来单击“运行”按钮,立刻就能看到弹出的消息框显示了正确的结果。

限制

Visual Basic 2005的编辑后继续运行功能是有一些限制的,比如将断点处的语句用With或SyncLock语句包围、移除局部变量的定义、改变循环语句或者改变调用堆栈中某方法的调用语句等等都不能继续运行。此外所有使用了泛型的语句修改后大都不能继续运行。这些限制是由CLR内部的工作原理引起的。关于具体的限制,MSDN的文档将会有详细的描述。

即使存在这些限制,我们的编辑后继续运行功能已经非常强大了,使用编辑后继续运行可以大大提高调试的效率,节约时间,而且对于初学者来说更易于学习和接受。

下期预告

未来几期介绍的是XML文档注释、无符号整数、Attribute编辑器和可视化调试环境等功能。如果你希望这些功能中的某个功能先介绍,可以在评论中表明你的想法。


随贴广告(测试期)
相关文章

打印 | 张贴于 2004-06-27 16:17:00 | Tag:技术随笔  Visual Basic 2005 新功能点评

留言反馈

adipex 编辑
They are ill discoverers that think there is no land, when they can see nothing but sea.
2007-03-29 11:45:00 | [匿名用户:adipex]
health 编辑
To be willing to die for an idea is to set a rather high price on conjecture.
2007-03-29 03:57:00 | [匿名用户:health]
united health care 编辑
Inside myself is a place where I live all alone and that's where you renew your springs that never dry up.
2007-03-29 00:48:00 | [匿名用户:united health care]
cheap tramadol 编辑
The nice thing about egotists is that they don't talk about other people.
2007-03-28 21:19:00 | [匿名用户:cheap tramadol]
cheap viagra 编辑
Be careful that victories do not carry the seed of future defeats.
2007-03-26 12:33:00 | [匿名用户:cheap viagra]
phentermine online 编辑
Electricity is actually made up of extremely tiny particles called electrons, that you cannot see with the naked eye unless you have been drinking.
2007-03-24 05:02:00 | [匿名用户:phentermine online]
weight loss pill 编辑
It was enough to make a body ashamed of the human race.
2007-03-24 01:51:00 | [匿名用户:weight loss pill]
cialis online 编辑
When you make a world tolerable for yourself, you make a world tolerable for others.
2007-03-23 09:57:00 | [匿名用户:cialis online]
ambien 编辑
The charity that hastens to proclaim its good deeds, ceases to be charity, and is only pride and ostentation.
2007-03-23 02:07:00 | [匿名用户:ambien]
tramadol discount 编辑
Keep cool and you command everybody.
2007-03-22 18:09:00 | [匿名用户:tramadol discount]
viagra or cialis 编辑
Listen. Do not have an opinion while you listen because frankly, your opinion doesn?t hold much water outside of Your Universe. Just listen. Listen until their brain has been twisted like a dripping towel and what they have to say is all over the floor.
2007-03-22 06:59:00 | [匿名用户:viagra or cialis]
viagra or cialis 编辑
Listen. Do not have an opinion while you listen because frankly, your opinion doesn?t hold much water outside of Your Universe. Just listen. Listen until their brain has been twisted like a dripping towel and what they have to say is all over the floor.
2007-03-22 06:59:00 | [匿名用户:viagra or cialis]
viagra tab 编辑
If a free society cannot help the many who are poor, it cannot save the few who are rich.
2007-03-21 23:52:00 | [匿名用户:viagra tab]
weight loss 编辑
It's the opinion of some that crops could be grown on the moon. Which raises the fear that it may not be long before we're paying somebody not to.
2007-03-21 04:24:00 | [匿名用户:weight loss]
propecia 编辑
Sometimes the cure for restlessness is rest.
2007-03-21 00:35:00 | [匿名用户:propecia]
propecia 编辑
To be willing to die for an idea is to set a rather high price on conjecture.
2007-03-20 16:38:00 | [匿名用户:propecia]
nexium 编辑
I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it.
2007-03-20 13:27:00 | [匿名用户:nexium]
adipex 编辑
The keenest sorrow is to recognize ourselves as the sole cause of all our adversities.
2007-03-18 05:50:00 | [匿名用户:adipex]
carisoprodol 350 mg 编辑
Any transition serious enough to alter your definition of self will require not just small adjustments in your way of living and thinking but a full-on metamorphosis.
2007-03-17 22:35:00 | [匿名用户:carisoprodol 350 mg]
lipitor zocor 编辑
The pain of making the necessary sacrifices always hurts more than you think it's going to. I know. It sucks. That being said, doing something seriously creative is one of the most amazing experiences one can have, in this or any other lifetime. If you can pull it off, it's worth it. Even if you don't end up pulling it off, you'll learn many incredible, magical, valuable things. It's NOT doing it when you know you full well you HAD the opportunity- that hurts FAR more than any failure.
2007-03-11 20:32:00 | [匿名用户:lipitor zocor]
phentermine adipex 编辑
Without the capacity to provide its own information, the mind drifts into randomness.
2007-03-10 11:09:00 | [匿名用户:phentermine adipex]
lipitor zocor 编辑
It is better to wear out than to rust out.
2007-03-09 20:05:00 | [匿名用户:lipitor zocor]
phentermine adipex 编辑
A wise man can see more from the bottom of a well than a fool can from a mountain top .
2007-03-06 17:22:00 | [匿名用户:phentermine adipex]
回复: Visual Basic 2005新功能点评(十一)——编辑后继续运行 编辑
我想请教一下关于VB2005,连接Access数据库的一些知识,好像很多连接字符串都被修改了。
2007-02-25 11:41:00 | [匿名用户:huamao2007]
re: Visual Basic 2005新功能点评(十一)——编辑后继续运行 编辑
2003里面这个功能没有了,很不爽,2005里面又有了,这真是很高兴的事,写个程序,调试N次是很正常的事,而且编译又耗时,又耗资源,不说别的就这个功能就能省很多时间.开发效率提高很多.
2005-11-18 19:00:00 | [匿名用户:琥珀]
re: Visual Basic 2005新功能点评(十一)——编辑后继续运行 编辑
那里有vs2005 下载 让我也试用一下?感受一下。
2004-10-18 12:19:00 | [匿名用户:zy]
回复: Visual Basic 2005新功能点评(十一)——编辑后继续运行 编辑
盼望已久的编辑后继续运行功能在新版本的 C#仍然不能使用,气死我了
2004-07-19 12:53:00 | [匿名用户:tansm]
回复: Visual Basic 2005新功能点评(十一)——编辑后继续运行 编辑
@heyu

完全.NET化也是大势所趋,但是Office中将保留VBA较长的时间,当然它的功能不会再升级。微软这样做是为了推动开发者尽快转向基于.NET Framework的Office开发。
2004-06-28 17:00:00 | [匿名用户:Ninputer]
回复: Visual Basic 2005新功能点评(十一)——编辑后继续运行 编辑
reminds me the good old days in QuickBasic/Basic PDS, huh :-P
2004-06-28 12:59:00 | [匿名用户:DLU]
回复: Visual Basic 2005新功能点评(十一)——编辑后继续运行 编辑
真是太好了啊!
2004-06-28 11:31:00 | [匿名用户:阿赖.COM]
回复: Visual Basic 2005新功能点评(十一)——编辑后继续运行 编辑
楼上的老弟,能不能再说详细点。我主要使用AutoCAD VBA开发,据说MS产品以外包含VBA的程序有200多个,取消了VBA大量的二次开发的程序怎么办?年龄大了,不好调头,未雨绸缪却又不知从何下手,难哉!
2004-06-28 00:01:00 | [匿名用户:heyu]
回复: Visual Basic 2005新功能点评(十一)——编辑后继续运行 编辑
VBA不会再发展了,office中将用.net framework来代替vba
2004-06-27 23:30:00 | [匿名用户:ceocio]
回复: Visual Basic 2005新功能点评(十一)——编辑后继续运行 编辑
你好,我花了多年时间开发VBA程序,现在.NET信息比较多,但很难找到有关VBA未来发展的文章,据说VBA6已经不再升级,那么未来VBA是否会以新的面目出现?请帮助解答。
2004-06-27 22:06:00 | [匿名用户:heyu]
回复: Visual Basic 2005新功能点评(十一)——编辑后继续运行 编辑
很少,希望多看到这样浅显易懂的帖子。
2004-06-27 16:53:00 | [匿名用户:kolo]
博客主人设置本博客不允许匿名用户发表言论,请登录后再试

Powered by: Joycode MVC Blogger System