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 >>


About TheScarms
About TheScarms


Sample code
version info

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

Email this page


© Copyright 2016 TheScarms
Goto top of page