分享
 
 
 

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

王朝vb·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

默认实例

另一个对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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有