C# 删除“button1”字段,并在相关方法中将其声明为局部变量

C# 删除“button1”字段,并在相关方法中将其声明为局部变量,c#,visual-studio,codacy,C#,Visual Studio,Codacy,我用codacy检查我的代码,codacy对我说 Remove the 'button1' field and declare it as a local variable in the relevant methods. codacy的意思是 私人按钮1 本例中的方法是单击按钮1 我的代码只是一个小例子,因为我的代码要大得多: namespace WindowsFormsApp1 { public class Form1 : Form { private Bu

我用codacy检查我的代码,codacy对我说

Remove the 'button1' field and declare it as a local variable in the relevant methods.
codacy的意思是 私人按钮1

本例中的方法是单击按钮1

我的代码只是一个小例子,因为我的代码要大得多:

namespace WindowsFormsApp1
{
    public class Form1 : Form
    {
        private Button button1;

        public Form1()
        {
            InitializeComponent();
        }
        #region Vom Windows Form-Designer generierter Code
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(349, 155);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }
        #endregion
        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}
我不知道怎么做,谁能告诉我怎么做?
问候语

在表单编辑选项卡中选择此按钮并将其GenerateMember设置为false。但要考虑到,您将无法在代码隐藏中使用此按钮

删除“button1”字段,并在相关方法中将其声明为局部变量

不要

显示的代码由Windows窗体设计器生成。如果修改它,可能会发生以下两种情况之一:

更糟糕的情况是,你打破了表格。 最佳情况-下次调用时,设计器将覆盖您的更改。 两者都不理想,而且都是完全多余的。看起来您拥有的是一个静态分析工具,它对于分析您编写的代码非常有用,但对于您不编写的代码以及由工具生成的代码来说,它基本上毫无意义。例如,您也不需要分析来自第三方库的代码


老实说,只需将静态分析工具设置为忽略生成的代码。同样的建议也适用于单元测试覆盖率度量。特别是当涉及到VisualStudio中SOAP服务客户端生成的数千行时。如果您考虑到一个遗留web服务集成可能会扼杀您的分析指标,这样做没有实际价值。

此代码是从Windows窗体设计器生成的吗?如果是这样,我不建议直接修改它。听起来好像你正在使用的任何静态分析工具都没有为WinForms生成的代码制定规则。是的,我不使用designer.cs文件,所以要将designer代码直接写入form.cs,你知道更好的c分析工具吗?在这种情况下,不要修改它。您的修改可能会破坏设计器,甚至在最好的情况下,设计器只会撤消您所做的任何修改。代码检查生成的代码似乎是不必要的工作。最好将代码审查工具设置为忽略这些。好吧,我忘了,这不是指设计器代码,而是指删除行私有按钮1;并在方法中声明它如果我在公共部分类Form1:FormOk中使用designer.cs codacy意味着partial在这种情况下是免费的,那么最好使用designer.cs文件并忽略部分问题?@KTownMods:就我个人而言,我一般不喜欢大多数静态分析。它们有时提供有用的指标,但当团队需要专注于安抚工具时,它会以构建产品为代价。如果有一种方法可以忽略某些文件、某些标志或它们的组合,那么这可能是理想的选择。但是对于工具找到的任何给定的东西,有意义地询问它是否为假阳性总是很重要的,而不是为了安抚工具而对代码进行破坏。