views:

137

answers:

2

When extending classes, I find it very descriptive to use the base (MyBase in VB) keyword when accessing methods in the base-class. And the this (Me in VB) keyword when accessing functions in the class extending the base-class. That goes for a "flat" structure with no inheritance as well.

I find it easier to read this:

public class World
{
    public String baseName = "World";
}


public class Hello : World
{
    public String thisName = "Hello";


    public String GetNames()
    {
        return this.thisName + " " + base.baseName;
    }
}

Than this:

...

    public String GetNames()
    {
        return thisName + " " + baseName;
    }

...

Now, my question is: is it possible to enforce the use of this and base at compile-time in Visual Studio/C#, so that the compiler throws an error or a warning if you do not use these keywords in your code?

+4  A: 

Have a look at StyleCop.

http://code.msdn.microsoft.com/sourceanalysis

There are some rules in there that should do what you want. You can configure it to show any style violations as warnings or errors.

JohnC
FYI, by default, StyleCop enforces this (as an alternative to some sort of member-data naming convention) and discourages superfluous base specifications.
Greg D
It doesn't seem to work with Visual Studio Team System 2008. The install works fine, but VS crashes when I try to use the plug-in. This is on a clean install of VS, with no addons installed.
roosteronacid
We use StyleCop 4.3 with Visual Studio Team System 2008 in both plain and (just upgraded) SP1 variants and it works fine, so there isn't any fundamental incompatibility (though unfortunately this doesn't help with your problem, just letting you know it can work OK).
Greg Beech
Then I'm gonna mark this question answered. And pray to god that I can get it to work on my installation of VS. Thanks for the info, Greg.
roosteronacid
A: 

You may also want to check out SSW's code auditor

Unfortunately there is no way that I'm aware of to enforce this in the compiler.

Odd