Vb.Net CA1822

Ero*_*ocM 1 vb.net code-analysis

我运行了一个代码分析并收到了这条消息:

警告5 CA1822:Microsoft.Performance:从不使用'MainForm.CheckFileIfFileIsInUse(String)'中的'this'参数(或Visual Basic中的'Me').将成员标记为静态(或在Visual Basic中为Shared)或在方法体中使用"this"/"Me"或至少使用一个属性访问器(如果适用).d:\工作\更新\更新\ MainForm.vb

我不确定我理解它.这是它所指的行:

Dim testfile As String = thefilename & ".tst"
Run Code Online (Sandbox Code Playgroud)

它说从未使用它,但在下一行我有这个:

If IO.File.Exists(testfile) Then
    IO.File.Delete(testfile)
End If
Run Code Online (Sandbox Code Playgroud)

所以我知道它正在被使用.我在两个地方都有同样的信息我不明白为什么它说它从未使用过.

帮助一个困惑的新手找到他的方式:P

谢谢你,像往常一样

Jon*_*eet 7

它只是向您展示方法的第一行 - 该行的内容并不重要.关键是该方法不在Me任何地方使用引用,因此您可以将其声明为Shared方法.

换句话说,而不是:

Sub CheckFileIfFileIsInUse(ByVal thefilename as String)
    Dim testfile As String = thefilename & ".tst"
    If IO.File.Exists(testfile) Then
        IO.File.Delete(testfile)
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

有:

Shared Sub CheckFileIfFileIsInUse(ByVal thefilename as String)
    Dim testfile As String = thefilename & ".tst"
    If IO.File.Exists(testfile) Then
        IO.File.Delete(testfile)
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)