加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net – 为什么用“Is”编译但不用“IsNot”编译?

发布时间:2020-12-17 07:17:22 所属栏目:百科 来源:网络整理
导读:我正在攻击一些旧的VB代码,如果捕获到异常,我希望函数尽早返回,但如果它是System.UnauthorizedAccessException,则该函数应该继续.所以我没有得到 XY’ed,我知道这是一个奇怪的要求,但我正在用C#重写代码,我只需要查看结果.我知道可能有更好的方法.这是原始代
我正在攻击一些旧的VB代码,如果捕获到异常,我希望函数尽早返回,但如果它是System.UnauthorizedAccessException,则该函数应该继续.所以我没有得到 XY’ed,我知道这是一个奇怪的要求,但我正在用C#重写代码,我只需要查看结果.我知道可能有更好的方法.这是原始代码:

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将编译.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读