Form Eventhandler can be made static: incorrect suggestion?

Hi,

I'm not sure if this is a bug in ReSharper of in VS2008, or something else.

In VS2008SP1, I create a Windows Form Application. By double clicking the Form's area I create an event handler for the Load event.

In the Form1.Designer.cs, this creates the line:

this.Load += new System.EventHandler(this.Form1_Load);


In Form1.cs, this creates the event handler:

private void Form1_Load(object sender, EventArgs e) { }


ReSharper suggest I make the eventhandler static (because I don't use any instance variables inside the method). I let ReSharper make it static. This not only changes the method to a static method, but also changes the line in the Form1.Designer.cs to:

this.Load += new System.EventHandler(Form1_Load);
(it removes the this. keyword)


That's a good thing, cause if that line wasn't changed you'd be unable to compile (if Form1_Load is static, this.Form1_Load doesn't exist).

Since everything compiles just fine, you'd think everything is ok.

But then, I resize the form in the Designer. All of a sudden the code doesn't compile anymore. Why not? The Form designer has regenerated the Form1.Designer.cs code, and now it does refer to this.Form1_Load again:

this.Load += new System.EventHandler(this.Form1_Load);



If in stead of letting ReSharper change the method to static, I type in the static keyword myself, the Form1.Designer.cs doesn't get changed. That's probably normal behavior, I should let ReSharper do the work, not type in its hints myself.




  • I think it is wrong of ReSharper to offer to make this method static. Why? Because the change not only involves changing Form1.cs (i.e. the method's definition), but also involves a change in the designer generated code. I'm under the assumption that you should never change the designer generated code.
  • In fact, I think it might be a good idea for ReSharper to show a warning for any Refactoring that would involve a change in designer generated code.
    • Of course a rename of the Form1 class should still be possible without that warning.



What do you think? Is this to be considered a bug in ReSharper? And should the option to display a warning before refactoring generated code be a feature request?
1 comment

Hello Dick,

We've actually changed this behavior, so now the upcoming ReSharper 6.0 will
not suggest to make event handlers static, since this can potentially lead
to the problems you've described. Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Hi,

I'm not sure if this is a bug in ReSharper of in VS2008, or something
else.

In VS2008SP1, I create a Windows Form Application. By double clicking
the Form's area I create an event handler for the Load event.

In the Form1.Designer.cs, this creates the line:

 System.EventHandler(this.Form1_Load);]]>

In Form1.cs, this creates the event handler:

 Form1_Load(object sender, EventArgs e) { }]]>

ReSharper suggest I make the eventhandler static (because I don't use
any instance variables inside the method). I let ReSharper make it
static. This not only changes the method to a static method, but also
changes the line in the Form1.Designer.cs to:

 System.EventHandler(Form1_Load);]]>


(it removes the this. keyword)

That's a good thing, cause if that line wasn't changed you'd be unable
to compile (if Form1_Load is static, this.Form1_Load doesn't exist).

Since everything compiles just fine, you'd think everything is ok.

But then, I resize the form in the Designer. All of a sudden the code
doesn't compile anymore. Why not? The Form designer has regenerated
the Form1.Designer.cs code, and now it does refer to this.Form1_Load
again:

 System.EventHandler(this.Form1_Load);]]>


+If in stead of letting ReSharper change the method to static, I type
in the static keyword myself, the Form1.Designer.cs doesn't get
changed. That's probably normal behavior, I should let ReSharper do
the work, not type in its hints myself.+

I think it is wrong of ReSharper to offer to make this method static.
Why? Because the change not only involves changing Form1.cs (i.e. the
method's definition), but also involves a change in the designer
generated code. I'm under the assumption that you should never
change the designer generated code.

In fact, I think it might be a good idea for ReSharper to show a
warning for any Refactoring that would involve a change in designer
generated code.

Of course a rename of the Form1 class should still be possible
without that warning.

What do you think? Is this to be considered a bug in ReSharper? And
should the option to display a warning before refactoring generated
code be a feature request?

---
Original message URL:
http://devnet.jetbrains.net/message/5283547#5283547



0

Please sign in to leave a comment.