CA1822 Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • despairingFreshman
    New Member
    • Jun 2009
    • 6

    CA1822 Error

    I've got the Warning :
    Microsoft.Perfo rmance : The 'this' parameter of '....' is never used.Mark the member as static or use this in the method body or at least one prperty accessor, if appropriate.

    to this code:

    class CallFile
    {
    public String FindFile(string path)
    {
    try
    {
    XmlDocument ProjectFile = new XmlDocument();
    ProjectFile.Loa d(path);

    XmlNamespaceMan ager nsManager = new XmlNamespaceMan ager
    (ProjectFile.Na meTable);
    nsManager.AddNa mespace
    ("ns", "http://schemas.microso ft.com/developer/msbuild/2003");

    String Wert = ProjectFile.Sel ectSingleNode
    ("/ns:Project/ns:PropertyGrou p/ns:ProductVersi on",nsManager). InnerText;
    Console.Write(W ert + Environment.New Line );
    return Wert;
    }//try
    finally
    {
    Console.WriteLi ne("");
    }//finally
    }//Method
    }



    But when I try to solve the warning and make the method static I get following error:

    Memeber '.......' cannot be accessed with an instance reference, qualify it with a type name instad

    Can anyone please explain me, how I could solve error and warning?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Post the exact error message that you are getting. Most likely you are passing a parameter to a method but never using that parameter in that method.

    Comment

    • despairingFreshman
      New Member
      • Jun 2009
      • 6

      #3
      Well, I thought I did.
      I only leave out names given from me (file,namespace ...).

      I'll will insert exact copy:
      Warning 1
      CA1822 : Microsoft.Perfo rmance : The 'this' parameter (or 'Me' in Visual Basic) of 'CallFile.FindF ile(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.

      And when I make method static this error occures:
      Error 1
      Member 'MatchMaker.Cal lFile.FindFile( string)' cannot be accessed with an instance reference; qualify it with a type name instead

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Don't make it static if it doesn't need to be. The warning is because an instance method is not accessing any instance variables. The warning won't stop your code from compiling and running. The compiler is just telling you that instance methods usually access instance variables and if a method doesn't use instance variables then it can be called independently of any instance of the containing class meaning it can be made static.

        Comment

        • despairingFreshman
          New Member
          • Jun 2009
          • 6

          #5
          Thank you!!
          I think I came a littel bit closer.
          It'd run without static, but I wanted to make "nice" code and solve all warnings occurring during static Code Analysis...but I think I will just ignore it :->

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            If you do decide to make the method static then you use the class name to access it not an instance variable. If it doesn't access instance variables then it probably should be made static.

            Comment

            Working...