涕淌居

牛~冬~@~瘋~牛~涕~淌~の~涕~淌~居~~GnuDoyng's Web Log~~~Contact me~niu_dong@msn.com~ICQ:277276843~QQ:123930135~

原创 [译]Visual Basic 2005在语言上的增强(十二)默认实例和编译器警告收藏

默认实例

另一个对Visual Basic .NET的改变就是窗体的默认实例,不少从Visual Basic 6.0转向.NET开发的人员因为找不到窗体的默认实例而感到困惑。你必须在使用一个窗体对象时先创建它的实例:
Dim frm As New Form2
frm.Show()

Visual Basic 2005支持了默认实例,所以你可以使用类似下面的句法:
Form2.Show()

如果你能通过My.Forms集合来访问默认窗体实例那就更好了:
My.Forms.Form2.Show()

编译器警告
Visual Basic 2005支持编译器警告,这便允许你在程序运行时出乱子之前就尽可能地把问题搞定。警告通过在代码下划绿色波浪线的方式给出(而错误是通过蓝色波浪线给出的)。

编译器警告包括了递归属性访问,Catch语块或Case语块重叠,创建了一个没有返回值的函数,等等。我最喜欢举的警告例子就是引用一个尚未初始化的对象:
Dim cust As Customer
If cust.Name.Length = 0 Then
    '...
End If

在这里,后台编译器就会在If语句的cust下划上绿色波浪线。警告文字是这样写的:“variable 'cust' is used before it has been assigned a value.”(变量“cust”未被赋值就被使用)。一个空值引用异常将在程序运行时被引发。数不清有多少次当程序运行时我在我的代码中找到这种类型的错误,而现在,编译器能够在编译时就发现这些错误了。

顺便提醒一下,当你在代码编辑器里敲入上面的代码时,编译器最初会在Dim语句中的cust下划上绿色波浪线并给出一条“unused local variable 'cust'.”(未使用的局部变量“cust”)。一开始你会觉得这蛮讨厌的,因为你只是刚刚加上这个变量罢了,但是这项功能最终却能确保代码的简洁。

所有的错误不再是被统一地显示在IDE里的Task List里了,而是用一个新的Errors List窗口来分离错误、警告和信息(参见图2图6)。你可以在应用程序设计器的Compile面板里自行设置编译器是否标记警告或错误,面板里的复选框控件可以用来控制是关闭所有的警告还是把所有警告都视为错误。

使用Option Strict On语句可以用来自动生成多种情况的编译时错误。如果你试图隐式地进行收缩转换(有可能导致数据损失),Option Strict将在代码中标记出来:
Dim iValue As Integer
Dim lValue As Long
iValue = lValue '收缩转换错误
lValue = iValue '正确

如果使用晚期绑定你也会得到一个错误。一个对象的晚期绑定就是说通过Object类型的变量进行成员的访问:
Dim myObj As Object
Call myObj.method1() '晚期绑定错误

现在在Visual Studio中使用Option Strict来写程序是一个很好的习惯。只要可能,你都应该在Tools | Options菜单里打开Option Strict,方法是选中Project设置下Option Strict对应的复选框。或者,你可以在某个类或模块文件的顶端写上Option Strict On语句。


@以下是原文供网友参考@


Default Instances

Another change to Visual Basic .NET that has tripped up many developers migrating from Visual Basic 6.0 is the lack of a default instance for forms. You need to create an instance of the form before using it:
Dim frm As New Form2
frm.Show()

Visual Basic 2005 supports form default instances, so you can use the familiar syntax:
Form2.Show()

It's best if you access this default form instance through the My.Forms collection:
My.Forms.Form2.Show()


Compiler Warnings

Visual Basic 2005 supports compiler warnings, which give you a heads-up on issues that may cause problems at run time. A warning is shown as a green squiggly line under your code (errors are shown as blue squiggles).

Compiler warnings include recursive property access, overlapping catch blocks or case statements, creating a function without a return value, and others. My favorite warning is for a variable reference on an uninitialized object:
Dim cust As Customer
If cust.Name.Length = 0 Then
    '...
End If

Here the background compiler puts a green squiggly line under cust in the If statement. The warning text displayed reads "variable 'cust' is used before it has been assigned a value." A null reference exception could result at run time. I don't know how many times I've found this type of error in my code at run time, and now the compiler finds these errors at compile time.
Incidentally, as you type the code above in the editor, the compiler initially puts a green squiggly under cust in the Dim statement with the message "unused local variable 'cust'." At first this seems a bit annoying because you've just added the variable, but it helps you keep your code cleaner in the end.

Instead of showing all errors in the Task List in the IDE, there's a new Errors List window that separates messages into errors, warning, and messages (see Figure 2 and Figure 6). You have some control over whether the compiler flags warnings or errors on the Compile tab in the application designer, which has checkboxes for either disabling all warnings or for treating all warnings as errors.

Use the Option Strict On statement to generate compile-time errors for several scenarios. Option Strict will flag your code if you attempt an implicit narrowing conversion (one in which data could be lost):
Dim iValue As Integer
Dim lValue As Long
iValue = lValue 'narrowing conversion error
lValue = iValue 'error

You also get an error if you use late binding. An object is late bound when it is assigned to a variable of type Object:
Dim myObj As Object
Call myObj.method1() 'late binding error

Use of Option Strict is now considered a best practice for programming with Visual Studio. You should turn on Option Strict in your code wherever possible. You can turn on Option Strict in the Tools | Options menu, under the Project settings by selecting the checkbox for Option Strict. You can also put the Option Strict On statement at the top of an individual class or module file.

发表于 @ 2005年01月18日 20:42:00|评论(loading...)

新一篇: [译]Visual Basic 2005在语言上的增强(十三)显式的数组范围及小结 | 旧一篇: [译]Visual Basic 2005在语言上的增强(十一)无符号类型

用户操作
[即时聊天] [发私信] [加为好友]
疯牛涕淌
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
疯牛涕淌的公告
123930135~有蝦米代誌請Q我哦! 本blog已搬家...
文章分类
收藏
百科全書
Britannica
Encyclopedia Mythica
Encyclopedia of Philosophy
Encyclopedia.com
MSN Encarta
WIKIPEDIA
大師之家
Code/Tea/Etc...
Mind View
Panopticon Central
Robert Green's VB Blog
Rockford Lhotka
The Mountain of Worthless Information
The VB Team
VB.NET Expert
侯捷網站
話語資源
American Dialects
Ethnologue
Glossary
IPA HTML Entities
Lexicon of Linguistics
The Linguistic Society of America
Übungen Für Deutsch
Word 2 Word
WordNet
Yamada Language Guides
yourDictionary
技術社區
.NET Framework Windows Forms
ASP.NET Heaven
ASP.NET Web
C# Corner
C# Friends
C# Help
Code Guru
Dev Guru
Developer Fusion
DotNet Zone
Got DotNet
MIT開放式課程網頁
Most Valuable Professional
MSDN
SuperSite for Windows
The Code Project
VB Forums
VB Info Zine
VB.NET Heaven
VBCity
中國 Dot Net Nuke
微軟中國社區
外國朋友
Bram Blogs
Дневник elishena
Добре дошли, приятели!
Заметки обалдевшей от счастья матери
友情鏈接
Amoeba原蟲
kgd jeyp
Let's Eat Noodles at Last
Nature Is Restless
YOYO Forever
三生石坊
去日留痕
咖啡愛我
夢想飛翔
天高任我飛
奔騰不息
宜軒品
寧靜*海豚灣
形而上的憂傷
我愛茉莉
我的生活
既生甲何生乙
明天的寂寞
沫言沫語
異域星空
裝配中的腦袋
閩中龍
阿貴
風一代
與我有關
Linguistics Paradise
UÕH UỎNG SANG
閩東語論壇
只是路過
安妮寶貝
微微微笑
存档
软件项目交易
Csdn Blog version 3.1a
Copyright © 疯牛涕淌