我用codacy复查我的代码,codacy对我说:
Remove the 'button1' field and declare it as a local variable in the relevant methods.codacy表示的行是私有按钮button1;
本例中的方法是button1_Click
我的代码是(只是一个小例子,因为我的代码要大得多):
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)
{
}
}
}我不知道该怎么做,有人能解释一下我该怎么做吗?问候
发布于 2019-01-23 07:11:45
在表单编辑选项卡中,选择此按钮并将其GenerateMember设置为false。但是要考虑到你不能在后台代码中使用这个按钮。

发布于 2019-01-23 07:15:44
移除'button1‘字段,并在相关方法中将其声明为局部变量。
别。
您显示的代码是由Windows窗体设计器生成的。如果修改它,可能会发生以下两种情况之一:
两者都不是理想的,两者都是完全多余的。看起来你拥有的是一个静态分析工具,它对分析你写的代码很有用,但对于你不写的代码和由工具生成的代码基本上没有意义。(例如,您也不需要分析第三方库中的代码。)
老实说,只需将静态分析工具设置为忽略生成的代码即可。(同样的建议也适用于单元测试覆盖率指标。尤其是当涉及到Visual Studio中SOAP服务客户端生成的数千行代码时。一个遗留的web服务集成可能会扼杀您的分析指标,如果您将其考虑在内,那么这样做没有真正的价值。)
https://stackoverflow.com/questions/54317680
复制相似问题