Search Amazon:

Page 8 of 10               << Previous  1  2  3  4  5  6  7  8  9  10  Next >>

 

Custom FxCop Rule - Verify use of VB's IsNothing or IsDBNull

Often times programmers confuse Visual Basic's IsDBNull and IsNothing statements which are not the same. IsDBNull indicates a variable evaluates to the System.DBNull type representing missing or nonexistent data. IsNothing indicates a variable has not yet been initialized. So, this FxCop rule checks for the usage of these statements and issues a warning to verify their usage.

Create a small VB.NET sample which includes these two statements then build the application. Look at the .exe with IL DASM to view its intermediate language and you will see the following calls. Knowing this, it is an easy matter to create a custom FxCop rule to determine if these statements are used.

     Microsoft.VisualBasic.Information::IsNothing
     Microsoft.VisualBasic.Information::IsDBNull

C# code for custom FxCop rule:

using System;
using Microsoft.Cci;
using Microsoft.Fugue;
using Microsoft.FxCop.Sdk;
using Microsoft.FxCop.Sdk.Introspection;

public class VerifyUseOfIsNothingIsDBNull : BaseMigrationControlFlowRule
{
    public VerifyUseOfIsNothingIsDBNull() : base("VerifyUseOfIsNothingIsDBNull")
    {
    }

    //
    // Check for 'Information::IsNothing' or 'Information::IsDBNull'
    // in the generated Intermediate Language.
    //
    public override void VisitCall(Variable destination, Variable receiver, 
           Method callee, ExpressionList arguments, bool isVirtualCall, 
           IProgramContext programContext, IExecutionState stateBeforeInstruction, 
           IExecutionState stateAfterInstruction)
    {
        string calleeName = callee.Name.Name.Trim().ToUpper();

        if (callee.DeclaringType.FullName.Trim().ToUpper() == 
            "MICROSOFT.VISUALBASIC.INFORMATION"
            && (calleeName.Equals("ISNOTHING") || calleeName.Equals("ISDBNULL")))
        {
            base.Problems.Add(new Problem(GetResolution(callee.Name.Name), programContext));
        }
        base.VisitCall(destination, receiver, callee, arguments, isVirtualCall, 
             programContext, stateBeforeInstruction, stateAfterInstruction);
    }
}

Rule definition in the XML rules file:

<Rule TypeName="VerifyUseOfIsNothingIsDBNull" 
          Category="VBMigration" CheckId="AA1001">
    <Name>
        Verify usage of IsDBNull and IsNothing methods
    </Name>
    <Description>
        IsDBNull and IsNothing methods are not equivalent
    </Description>
    <Url>
        http://www.thescarms.com/
    </Url>
    <Resolution>
        Verify usage of '{0}'. IsDBNull and IsNothing are not equivalent. 
        IsDBNull indicates a variable evaluates to the System.DBNull type 
        representing missing or nonexistent data. IsNothing indicates a 
        variable has not yet been initialized
    </Resolution>
    <MessageLevel Certainty="99">
        Warning
    </MessageLevel>
    <FixCategories>
        NonBreaking
    </FixCategories>
    <Owner />
<Rule>

 

Page 8 of 10               << Previous  1  2  3  4  5  6  7  8  9  10  Next >>

Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Comments: 1 Items to Show:     

     
Id:  1 Posted:  5/6/2007 4:45:16 PM By:  PSMACCHIA
With the CQL language provided with NDepend, coding this rule is as simple as:

WARN IF Count > 0 IN SELECT ASSEMBLIES WHERE IsDirectlyUsing "Microsoft.VisualBasic.Information.IsNothing (Object)" OR IsDirectlyUsing "Microsoft.VisualBasic.Information.DbNull(Ob ject)"

http://www.NDepend.com
 
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms