views:

279

answers:

4
+1  Q: 

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

+6  A: 

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
Jon Skeet
And then also consider whether this method really still belongs as part of this type or, being shared, could more logically be associated with a different, more generic type
Joel Coehoorn
Everything in this project is within one form including the CheckFileIfFileIsInUse and the sub calling it. I would think that I do not need to make is shared if that is the case correct? Would I not ignore this 'warning' then?
ErocM
You don't *have* to make it shared, no - it's just that it doesn't *need* to be an instance method. The suggestion is that you make it a shared method to avoid the *appearance* that it depends on the instance itself.
Jon Skeet
Even if this is a single form application? I assume this is for readability/maintainability? Should I be doing this as a standard then even if it might not be used outside the form?
ErocM
A: 

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

John Nolan
+1  A: 

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)
Michael Petrotta
+2  A: 

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)
Aaron M
This answer and the accepted answer helped me understand it better. Thanks for the reply.
ErocM