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


Custom FxCop Rule - Do not use VB6 Conversion Functions

This custom FxCop rule looks to see that you have not used the older VB6 conversion functions like CStr, CDate, CInt etc. Instead, you should be using Convert.ToString(), Convert.ToDateTime(), Convert.ToInt32(), ...

If you create a VB.NET sample program with the above conversion calls, when you build the code and examine the MSIL, you will see calls to:


This rule looks for calls to the CompilerServices.Conversions namespace.

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 DoNotUseVB6Conversions : BaseMigrationControlFlowRule
    public DoNotUseVB6Conversions() : base("DoNotUseVB6Conversions")

    // Check for 'CompilerServices.Conversions' 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)
        if (callee.DeclaringType.FullName.Trim().ToUpper() ==
            base.Problems.Add(new Problem(GetResolution(), programContext));
            // Can output IL statement w/ this parameter. 
            // Must add {0} placeholder to Rules.xml <Resolution> value.
            //base.Problems.Add(new Problem(GetResolution(calleeName), programContext));
        base.VisitCall(destination, receiver, callee, arguments, 
            isVirtualCall, programContext, stateBeforeInstruction, 

Rule definition in the XML rules file:

<Rule TypeName="DoNotUseVB6Conversions" 
          Category="VBMigration" CheckId="AA1001">
        Do not use the VB6 conversion functions
         Do not use the VB6 data conversion functions  
         such as CStr, CInt, CDate, etc.
        Do not use the VB6 conversion functions. Use the 'Convert.To...' 
        methods from the 'System.Convert' namespace instead.
    <MessageLevel Certainty="99">
    <Owner />


Page 4 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 ""

Email this page

© Copyright 2019 TheScarms
Goto top of page