0

i review my code with codacy and codacy says following to me

Remove the 'button1' field and declare it as a local variable in the relevant methods. 

The line that codacy mean is private Button button1;

The method in this case button1_Click

my code is (only a little example because my code is much bigger):

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) { } } } 

I dont know how to do that, can anyone explain me what to do here? Greetings

7
  • 2
    Is this code generated from the Windows Forms designer? If so, I wouldn't recommend modifying it directly. It sounds like whatever static analysis tool you're using just doesn't have rules in place for WinForms generated code. Commented Jan 22, 2019 at 23:02
  • Yes, i dont use designer.cs files,so vs write the designer code directly to the form.cs, do you know a better analytics Tool for c#? Commented Jan 22, 2019 at 23:07
  • 1
    In that case don't modify it. Your modifications could break the designer, or even in the best case the designer will just undo any modifications you make. Code reviewing generated code seems like unnecessary effort. Best to set your code review tools to ignore those. Commented Jan 22, 2019 at 23:09
  • Ok i forgot, it not means the designer code, it means to remove the line private Button button1; and declare it in the methods Commented Jan 22, 2019 at 23:10
  • If i use designer.cs codacy means partial is gratuitous in this context At public partial class Form1 : Form Commented Jan 22, 2019 at 23:14

2 Answers 2

1

In form editing tab select this button and set its GenerateMember to false. But take into account that you won't be able to use this button in code behind.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Remove the 'button1' field and declare it as a local variable in the relevant methods.

Don't.

The code you're showing is generated by the Windows Forms Designer. If you modify it, one of two things is likely to happen:

  1. Worse case - You break the form.
  2. Best case - The designer over-writes your changes next time it's invoked.

Neither is ideal, and both are entirely superfluous. It looks like what you have is a static analysis tool which is useful for analyzing code you write, but essentially pointless for code you don't write and is generated by a tool. (You also don't need to analyze code from 3rd party libraries, for example.)

Honestly, just set your static anaysis tools to ignore the generated code. (This same advice applies to unit test coverage metrics. Especially when it comes to the thousands of lines generated by SOAP service clients in Visual Studio. One legacy web service integration can kill your analysis metrics if you take it into account, and there's no real value in doing so.)

2 Comments

Ok so it is better to use designer.cs files and ignore the partial issue?
@KTownMods: Personally I'm not a fan of most static analysis in general. They provide useful metrics sometimes, but when the team is required to focus on appeasing the tool it comes at the expense of building the product. If there's a way to ignore certain files, or certain "flags", or a combination thereof, that could be ideal. But for any given thing the tool finds, it's always important to meaningfully ask if it's a false positive, rather than butcher the code just to appease the tool.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.