Skip to main content

Cannot declare variable of static type

In This Article

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");
    }
}