VB.NET中,为何选择使用Is而非IsNot进行类型比较?
- 内容介绍
- 文章标签
- 相关推荐
本文共计472个文字,预计阅读时间需要2分钟。
我正在修改一些旧的VB代码,如果遇到异常,希望函数能尽快返回。如果是System.UnauthorizedAccessException,则函数应继续执行。所以我没有得到XY的ed,我知道这是一个奇怪的要求,但我正在使用“
Try doSomeStuffWithFiles(files) Catch ex As Exception MsgBox("Far Field: error in reading / writing to far field file." & Chr(13) & ex.Message) Exit Sub End Try
所以我添加了几行:
Catch ex As Exception MsgBox("Far Field: error in reading / writing to far field file." & Chr(13) & ex.Message) If TypeOf ex IsNot System.UnauthorizedAccessException Then Exit Sub End If End Try
现在,我不是VB的专家,但据我所知,这是完全有效的VB.它还与MSDN上的TypeOf的示例代码完全匹配.但是,此代码无法编译.我收到此错误:
Error 21 'Is' expected. C:\FilePath 3114 26 Project Error 22 'UnauthorizedAccessException' is a type in 'System' and cannot be used as an expression. C:\FilePath 3114 32 Project
如果我将该行更改为
Catch ex As Exception MsgBox("Far Field: error in reading / writing to far field file." & Chr(13) & ex.Message) If TypeOf ex Is System.UnauthorizedAccessException Then Exit Sub End If End Try
然后一切都编译好了. (没有逻辑倒退)
我正在使用visual studio 2013,并以.net framework 2.0为目标.
那么IsNot无效的原因是什么?
它可以在Visual Studio 2015中使用它,但如果你看一下 VS2013 version of the docs,你只会看到TypeOf …被列出,所以你需要使用Not TypeOf … Is.目标.NET Framework版本没有什么区别.如果您正在使用VS2015,TypeOf … IsNot将编译.
本文共计472个文字,预计阅读时间需要2分钟。
我正在修改一些旧的VB代码,如果遇到异常,希望函数能尽快返回。如果是System.UnauthorizedAccessException,则函数应继续执行。所以我没有得到XY的ed,我知道这是一个奇怪的要求,但我正在使用“
Try doSomeStuffWithFiles(files) Catch ex As Exception MsgBox("Far Field: error in reading / writing to far field file." & Chr(13) & ex.Message) Exit Sub End Try
所以我添加了几行:
Catch ex As Exception MsgBox("Far Field: error in reading / writing to far field file." & Chr(13) & ex.Message) If TypeOf ex IsNot System.UnauthorizedAccessException Then Exit Sub End If End Try
现在,我不是VB的专家,但据我所知,这是完全有效的VB.它还与MSDN上的TypeOf的示例代码完全匹配.但是,此代码无法编译.我收到此错误:
Error 21 'Is' expected. C:\FilePath 3114 26 Project Error 22 'UnauthorizedAccessException' is a type in 'System' and cannot be used as an expression. C:\FilePath 3114 32 Project
如果我将该行更改为
Catch ex As Exception MsgBox("Far Field: error in reading / writing to far field file." & Chr(13) & ex.Message) If TypeOf ex Is System.UnauthorizedAccessException Then Exit Sub End If End Try
然后一切都编译好了. (没有逻辑倒退)
我正在使用visual studio 2013,并以.net framework 2.0为目标.
那么IsNot无效的原因是什么?
它可以在Visual Studio 2015中使用它,但如果你看一下 VS2013 version of the docs,你只会看到TypeOf …被列出,所以你需要使用Not TypeOf … Is.目标.NET Framework版本没有什么区别.如果您正在使用VS2015,TypeOf … IsNot将编译.

