SubMain - CodeIt.Right The First Time!

/Community

Support Community for SubMain Products
 Home Products Services Download Purchase Support
in Search
 
Home Forums Blogs Tutorials/CIR Tutorials/GD Downloads
Welcome to SubMain Community Sign in | Join | Help

Checking for Shared/Static Variables

Last post 06-25-2014, 3:03 AM by Misha Zhutov (SubMain). 5 replies.
Sort Posts: Previous Next
  •  06-20-2014, 1:51 AM 3833

    Checking for Shared/Static Variables

    Hi,

    I need to ensure that my code does not contain any shared/static variables

    How do we check that using the custome rules

    public override bool Check(IElementInfo element)
    {
    if (element != null)
    {
    IMethodInfo methodInfo = element as IMethodInfo;
    IVariableInfo[] variables = methodInfo.GetVariables();
    if (variables != null)
    {
    int variablesCount = variables.Length;
    for (int i = 0; i < variablesCount; i++)
    {
    IVariableInfo variable = variables


    if (variable.ReturnType != null && variable.ReturnType.Name == "Static")
    {
    base.SetDisplayedElement(variable);
    return true;
    }


    }
    }
    }
    }

    return false;
    }

     

     

    regards,
    Sushil Nembhani

  •  06-20-2014, 5:56 AM 3834 in reply to 3833

    Re: Checking for Shared/Static Variables

    Hi Sushil,

    Please clarify what is static variable?

    In C#/VB.NET you cannot mark local variable as static. You can only have local const variable.

     


    Thank you,
    Misha Zhutov
    http://submain.com - .NET Developer Tools | Microsoft VB MVP
  •  06-23-2014, 2:22 AM 3840 in reply to 3834

    Re: Checking for Shared/Static Variables

    Hi,

    My code is as below

    Public Class CustDetails

       Public Shared ErrorMsg As String

       Public Shared ErrorMsg As String

    End Class

    I have to check my code for the Shared variables ErrorMsg & generate violations

    End Class

    I have to check my code for the Shared variables ErrorMsg & generate violations
  •  06-23-2014, 2:58 AM 3845 in reply to 3833

    Re: Checking for Shared/Static Variables

    Sushil,

    These are fields (class members) - not variables.  You need the following code:

    IFieldInfo fieldInfo = codeElement as IFieldInfo;
    if (fieldInfo.IsStatic)
    {
    }

    fieldInfo = codeElement as IFieldInfo;
    if (fieldInfo.IsStatic)
    {
    }


    Thank you,
    Misha Zhutov
    http://submain.com - .NET Developer Tools | Microsoft VB MVP
  •  06-25-2014, 2:54 AM 3890 in reply to 3845

    Re: Checking for Shared/Static Variables

    Hi,

    I have written the below custom rule but after using the Rule on analyzing the code it gives error & does not analyze the code.

     using System;
    using System.Runtime.Serialization;

    using SubMain.CodeItRight.Sdk.Core.Reflection;
    using SubMain.CodeItRight.Sdk.Core.Rules;
    using SubMain.CodeItRight.Sdk.Reflection;
    using SubMain.CodeItRight.Sdk.Rules;

    namespace CustomRules3
    {
        /// <summary>
        /// Template for new rule created by wizard
        /// </summary>
        [ModificationDate("06/24/2014")]
        [Category("SubMain.Custom")]
        public class CheckSharedField : RuleBase
        {
            #region Template to be populated by rule author

            public override string[] AutoCorrectionOptions
            {
                get
                {
                    return new string[] { };
                }
            }

            public override bool Check(IElementInfo element)
            {
                 if (element != null)
                {
                    IMethodInfo methodInfo = element as IMethodInfo;
                    IClassInfo classInfo = methodInfo.Parent as IClassInfo;

                    IMemberInfo[] fields = classInfo.GetFields();

                    if (fields != null)
                    {
                        int fieldsCount = fields.Length;
                        for (int i = 0; i < fieldsCount; i++)
                        {
                            IMemberInfo field = fieldsIdea;

                            if (field.IsStatic)
                            {
                                base.SetDisplayedElement(field);
                                return true;
                            }
                        }
                    }
                 }

                return false;
            }

            #endregion

            #region Wizard Generated Code

            #region Data Entered On Wizard form

            private const string RULEID = "1889cc2d-1f9b-4e18-87de-838093880256";
            private const string NAME = "CheckSharedField";
            private const string TITLE = "Checks for the Shared/Static Field";
            private const SeverityLevel SEVERITY = SeverityLevel.Warning;
            private const RuleScopes SCOPES = RuleScopes.Any;
            private const string AUTHOR = "DESIGN TEAM";
            private const string EMAIL = "";
            private const string URL = "";
            private const RuleTargets AVAILABLE_TARGETS = RuleTargets.Field;
            private const string VIOLATION_DESCRIPTION = "Fields cannot be declared as Shared/Static";


          
            #endregion

            #region Constructor

            public CheckSharedField()
            {
                this.Name = NAME;
                this.Title = TITLE;
                this.Severity = SEVERITY;
                this.Targets = this.AvailableTargets;
                this.Scopes = SCOPES;
            }

            #endregion

            public override string ID
            {
                get
                {
                    return RULEID;
                }
            }

            public override RuleTargets AvailableTargets
            {
                get
                {
                    return AVAILABLE_TARGETS;
                }
            }

            public override string Author
            {
                get
                {
                    return AUTHOR;
                }
            }

            public override string Email
            {
                get
                {
                    return EMAIL;
                }
            }

            public override string Url
            {
                get
                {
                    return URL;
                }
            }

            public override string ViolationDescription
            {
                get
                {
                    return VIOLATION_DESCRIPTION;
                }
            }

            public override bool Correct(int correctionOptionIndex)
            {
                base.Correct(correctionOptionIndex);
                switch (correctionOptionIndex)
                {
                    default:
                        break;
                }
                return false;
            }

            #endregion
        }
    }

    regards,
    Sushil Nembhani

     

  •  06-25-2014, 3:03 AM 3891 in reply to 3890

    Re: Checking for Shared/Static Variables

    Sushil,

    Yes, that code generates "Object reference" exception.

    Please replace the code in Check method on:

    public override bool Check(IElementInfo element)
    {
      if (element != null)
      {
        IFieldInfo field = element as IFieldInfo;
        if (field.IsStatic)
        {
          return true;
        }
      }

      return false;
    }

    It is because you target Field - (AVAILABLE_TARGETS = RuleTargets.Field;)

     

     


    Thank you,
    Misha Zhutov
    http://submain.com - .NET Developer Tools | Microsoft VB MVP
View as RSS news feed in XML
 
     
 
Home |  Products |  Services |  Download |  Purchase |  Support |  Community |  About Us |