Skip to main content

Keyword this (Me) is not valid in a static member

In This Article

CodeRush Classic shows the Keyword this (Me) is not valid in a static member code issue if the this (Me in Visual Basic) keyword is used within a static member.

#Fix

Make the member non static or use an instance of the current class instead of the this keyword.

#Purpose

Highlights member declarations, which would cause the Keyword ‘this’ is not valid in a static property, static method, or static field initializer compilation error.

#Example

public class MyClass
{
    public MyClass(object data)
    {
        _Data = data;
    }
    private object _Data;
    public static object GetData()
    {
        return this._Data;
    }
}

Fixes:

public class MyClass
{
    public MyClass(object data)
    {
        _Data = data;
    }
    private object _Data;
    public object GetData()
    {
        return this._Data;
    }
}
public class MyClass
{
    public MyClass(object data)
    {
        _Data = data;
    }
    private object _Data;
    public static object GetData(MyClass myClassInstance)
    {
        return myClassInstance._Data;
    }
}