views:

76

answers:

5

We have a coding standard that says all shared (static) fields and methods must be called with the class name. E.g.

NameOfClass.whatever

Rather then

whatever  

Is there a tool that we can use to check this is in fact the case? (Likewise for modules)

Sorry I should have make it clearer we are using VB.NET.


This is a bigger example of what I mean.

Public Class Class1
    Public Shared Sub SharedMethod()

    End Sub

    Public Shared sharedField As Integer

    Public Sub NotSharedMethod()
        'this next line shold be written as Class1.SharedMethod
        SharedMethod()

        'this next line shold be written as Class1.sharedField
        sharedField = 5
    End Sub

End Class

see also What StyleCop like tools are there for VB.NET

+2  A: 

Sure, just create a custom rule in StyleCop. Then incorporate the use of StyleCop into your automated build process.

Sorry, I did not even realize that StyleCop did not have a VB.NET version. What you need is a static analysis tool for VB.NET. Based on this thread, it looks like Project Analyzer is an option. Unfortunately it's not free.

From the web site:

Maintain. To help maintenance, Project Analyzer lets you enforce coding standards[.]

Whatever tool you use, be sure it incorporate it into your automated build process.

Jason
+2  A: 

Yes,

Use StyleCop and write your custom rule to do your check.

Here's a reference to check how to write custom-rule for StyleCop.

alt text

this. __curious_geek
That looks like a great tool!
Andy West
**StyleCop** rocks! StyleCop is basically functional implementation of rules and guidelines described in Framework Design Guidelines [http://www.amazon.com/Framework-Design-Guidelines-Conventions-Libraries/dp/0321545613/ref=pd_sim_b_4]. Many .Net core team people have contributed to this book.
this. __curious_geek
StyleCop sounds great, but the question is tagged VB.net, and StyleCop only supports C# http://code.msdn.microsoft.com/sourceanalysis/Thread/View.aspx?ThreadId=383
MarkJ
Sorry I should have make it clearer we are using VB.NET.
Ian Ringrose
-1 as this would have been a great answer for a C# question, but I was asking about VB.NET
Ian Ringrose
+2  A: 

In the Project Properties > Compile, you can set the Warning configuration for "Instance Variable access shared member" to Error and it should generate a compiler error instead of a warning.

I'm not sure how you might do it for all projects. You could change the project template to include that option for all new projects.

Chris Dunaway
thanks, this works for access from outside of the class, but not inside the class, see the edit to my question for when it does not work.
Ian Ringrose
A: 

Sorry I never did find a good tools for this.

Ian Ringrose
A: 

You might be able to use FxCop for this purpose, and write a custom rule. Here is a good site that explains how to write custom FxCop rules.

Nick
Thanks, The problem in that the generated intermediate code is the same in both cases, so the tool needs to run on the vb.net source code.
Ian Ringrose