涕淌居

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

原创 [译]Visual Basic 2005在语言上的增强(六)IsNot运算符和TryCast语句收藏

友情提醒,本文翻译的一处有误,请网友们务必结合评论来看本期翻译!

IsNot运算符


IsNotIs的反义运算符。在引用一个对象前,要先检查它是否已被初始化,而在过去我们都使用这种很别扭很老套的方式:
If Not myObj Is Nothing Then

IsNot使你可以进行直接的比较,免除了使用Not运算符的烦恼:
If myObj IsNot Nothing Then

同样地,你在比较两个对象实例是否不同时,也不必再使用Not运算符了:
If MyObj1 IsNot MyObj2 Then

别看这只是个简单的变化,IsNot弥补了Visual Basic语言一致性不足的一个缺点,你的代码将变得更加简洁了。

TryCast语句

在Visual Basic 2003里,你可以使用以下两种之一的方法把一个对象的类型转换为另一个类型:
Dim p As Product
p = CType(obj, Product)
p = DirectCast(obj, Product)

上面,你使用CType把一个类型转换成了另一个,而DirectCast要求这两个对象间有继承或是实现接口的关系(作者这里的讲解有误CTypeDirectCast的实际区别是DirectCast要求对象的运行时类型与指定的转换类型要相同,而CType的运行效率的运行效率略低些,涕淌注)。但这里的问题是,无论你使用哪种方法,如果obj无法被转换或铸造(转换是“convert”,铸造是“cast”,作者在有的地方混淆了convert和cast的区别,其实它们的区别就是CTypeDirectCast的区别。但是笔者认为没有必要去抠这个字眼,因此我在其他地方统一译作“转换”,涕淌注)成Product类型的话,那么将导致程序的运行时异常。

用新的TryCast语句吧。它和CType或者是DirectCast的作用方式一样,只不过如果类型转换无法完成时,将返回一个Nothing值给对象:
p = TryCast(obj, Product)
If p IsNot Nothing Then
    '使用p对象……
End If


@以下是原文供大家参考@

IsNot Operator

The IsNot operator provides a counterpart to the Is operator. Instead of the clunky and often-used check that an object has been instantiated before you reference it:
If Not myObj Is Nothing Then

IsNot lets you use a direct comparison, eliminating the Not operator:
If myObj IsNot Nothing Then

Similarly, you can eliminate the Not operator when checking if two object instances are different:
If MyObj1 IsNot MyObj2 Then

Although it's a simple change, IsNot fills a consistency gap in the Visual Basic language that helps make your code clearer.

TryCast Statement

In Visual Basic 2003, you can cast one type of object to another type in one of two ways:
Dim p As Product
p = CType(obj, Product)
p = DirectCast(obj, Product)

Here, you use CType to convert from one type to another, while DirectCast requires there be an inheritance or implementation relationship between the objects. The problem with these is that you'll get a run-time exception in either case if obj cannot be converted or cast to type Product.

Enter the new TryCast statement. You use it the same way as CType or DirectCast, except that the result returned is Nothing if the cast cannot be accomplished:
p = TryCast(obj, Product)
If p IsNot Nothing Then
    'use the p object...
End If

发表于 @ 2004年12月27日 18:39:00|评论(loading...)

新一篇: 关于Visual Basic 2005 Beta中窗体互访技术的思索 | 旧一篇: [译]Visual Basic 2005在语言上的增强(五)Using、Continue语句及Global关键字

用户操作
[即时聊天] [发私信] [加为好友]
疯牛涕淌
订阅我的博客
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 © 疯牛涕淌