The requested page is not available for the requested platform. You are viewing the content for Default platform.

Cannot declare variable of static type

CodeRush Classic shows the Cannot declare variable of static type code issue if the current variable is of a static type.

Fix

Remove the variable and access the static class members by using the class name itself.

Purpose

Highlights the variable declaration statements, which would cause the Cannot declare a variable of static type compilation error.

Example

static class MyClass
{
    public static void OutputText(string text)
    {
        Console.Write(text);
    }
}
class TestClass
{
    public TestClass()
    {MyClass objVar = new MyClass();
        objVar.OutputText("TestClass object created");
    }
}

Fix:

static class MyClass
{
    public static void OutputText(string text)
    {
        Console.Write(text);
    }
}
class TestClass
{
    public TestClass()
    {
        MyClass.OutputText("TestClass object created");
    }
}