ErocM
ErocM

Reputation: 329

Vb.Net CA1822

I ran a Code Analysis and got this message:

Warning 5 CA1822 : Microsoft.Performance : The 'this' parameter (or 'Me' in Visual Basic) of 'MainForm.CheckFileIfFileIsInUse(String)' is never used. Mark the member as static (or Shared in Visual Basic) or use 'this'/'Me' in the method body or at least one property accessor, if appropriate. D:\WORK\Update\Update\MainForm.vb

I'm not sure I understand it. This is the line it's referring to:

Dim testfile As String = thefilename & ".tst"

It's saying that it is never used but in the very next line I have this:

If IO.File.Exists(testfile) Then
    IO.File.Delete(testfile)
End If

So I know it's being used. I have this same message in two places I don't get why it's saying it's never used.

Help a confused newbie find his way :P

Thanks as usual, Eroc

Upvotes: 1

Views: 1560

Answers (4)

Michael Petrotta
Michael Petrotta

Reputation: 60902

The message is referring to the entire method CheckFileIfFileIsInUse. It's telling you that nothing in that method is accessing instance members of the class, so you might as well declare the method Shared.

Shared Sub CheckFileIfFileIsInUse(ByVal thefilename as String)

Upvotes: 2

Aaron M
Aaron M

Reputation: 2563

Declare your method CheckFileIfFileIsInUse as shared. The instead of declaring a new instance of the method, just reference it directly.

Use This
MyClass.CheckfileIfFileisInUser(filename)

Instead of

 Dim newclass as Myclass
 newclass.CheckfileIfFileisInUser(filename)

Upvotes: 2

Johnno Nolan
Johnno Nolan

Reputation: 29659

The error is saying that you don't need a instance of the class (your form) to use the Method CheckFileIfFileIsInUse

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500525

It's simply showing you the first line of the method - the contents of that line is unimportant. The point is that the method doesn't use the Me reference anywhere, so you can declare it to be a Shared method instead.

In other words, instead of:

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

Have:

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

Upvotes: 7

Related Questions