vb.net “不是……什么都没有”与“……不是什么都没有”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18909880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Not ... Is Nothing" versus "... IsNot Nothing"
提问by nnnn
Does anyone here use VB.NET and have a strong preference for or against using Not foo Is Nothing
as opposed to foo IsNot Nothing
? If so, why?
这里有没有人使用 VB.NET 并且强烈偏爱或反对使用Not foo Is Nothing
而不是foo IsNot Nothing
? 如果是这样,为什么?
For Example
例如
If var1 IsNot Nothing Then
...
End If
and
和
If Not var1 Is Nothing Then
...
End If
I just want to know which one is better?
Are they both equally acceptable?
我只想知道哪个更好?
他们都同样可以接受吗?
回答by Paul Michaels
The
这
If Not var1 Is Nothing Then
Is a hangover from VB6. There didn't used to be an IsNot, and so this was the only way to determine if a variable was not Nothing
. It seems to be redundant in VB.NET.
是VB6的宿醉。过去没有 IsNot,因此这是确定变量是否不是 的唯一方法Nothing
。在 VB.NET 中似乎是多余的。
回答by Tony L.
foo IsNot Nothing
foo IsNot Nothing
The following line is straight from Microsoft's Visual Basic Coding Conventions:
以下行直接来自 Microsoft 的Visual Basic Coding Conventions:
Use the
IsNot
keyword instead ofNot
...Is Nothing.
使用
IsNot
关键字而不是Not
...Is Nothing。
回答by Tim Pietzcker
I would go with the first variant - it reads like English and is easier to follow/understand than the second one. Other than that, they are equivalent.
我会选择第一个变体 - 它读起来像英语,比第二个更容易理解/理解。除此之外,它们是等效的。
回答by sav
I found a similar question here VB.NET - IsNothing versus Is Nothing, where I feel this question was exhaustively answered. Among the answers Hyman Snipes identified http://weblogs.asp.net/psteele/410336, a blog that gives some extra detail. From those I prefer and have used
我在这里找到了一个类似的问题VB.NET - IsNothing 与 Is Nothing,我觉得这个问题得到了详尽的回答。Hyman Snipes 在回答中提到了http://weblogs.asp.net/psteele/410336,这是一个提供一些额外细节的博客。从那些我喜欢和用过的
IsNot Nothing
which also makes my code easier to read and understand.
这也使我的代码更易于阅读和理解。
回答by MikeF
Using VB 7.0
使用 VB 7.0
If var1 Is Not Nothing Then
If var1 Is Not Nothing Then
generates an "invalid use of object error" as per this "VBForums"link.
根据此“VBForums”链接生成“无效使用对象错误” 。
If var1 IsNot Nothing Then
If var1 IsNot Nothing Then
generates a "Compile error: Expected: Then or GoTo"
生成“编译错误:预期:然后或转到”
If Not IsNothing(var1) Then
If Not IsNothing(var1) Then
worked like a champ
像冠军一样工作