首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除“button1”字段,并在相关方法中将其声明为局部变量

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

Stack Overflow用户
提问于 2019-01-23 06:58:13
回答 2查看 878关注 0票数 0

我用codacy复查我的代码,codacy对我说:

代码语言:javascript
复制
Remove the 'button1' field and declare it as a local variable in the relevant methods.

codacy表示的行是私有按钮button1;

本例中的方法是button1_Click

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

代码语言:javascript
复制
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)
        {

        }
    }
}

我不知道该怎么做,有人能解释一下我该怎么做吗?问候

EN

回答 2

Stack Overflow用户

发布于 2019-01-23 07:11:45

在表单编辑选项卡中,选择此按钮并将其GenerateMember设置为false。但是要考虑到你不能在后台代码中使用这个按钮。

票数 1
EN

Stack Overflow用户

发布于 2019-01-23 07:15:44

移除'button1‘字段,并在相关方法中将其声明为局部变量。

别。

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

  1. 最糟糕的情况--你破坏了表单。
  2. 最好的情况--设计器--会在下次调用时覆盖你所做的更改。

两者都不是理想的,两者都是完全多余的。看起来你拥有的是一个静态分析工具,它对分析你写的代码很有用,但对于你不写的代码和由工具生成的代码基本上没有意义。(例如,您也不需要分析第三方库中的代码。)

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54317680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档