views:

418

answers:

9

I have the following code:

public interface IMyInterface
{
    DataGrid ItemsInGrid { get; set; }
}

partial public class MyClass: Window, IMyInterface
{
   public DataGrid ItemsInGrid 
   {
       get { return grdItemsInGrid}
       set { grdItemsInGrid= value; }
   }
}

In a Different file:

partial public class MyClass: Window
{
   private System.Windows.Forms.DataGrid grdItemsInGrid;

   // Reference to private variable here
   // This is the designer portion that actually sets up the 
   // private variable to be shown on the form.
}

Now resharper wants me to convert ItemsInGrid to an autoproperty (public DataGrid ItemsInGrid{ get; set; })

How can this be an equal transformation? An autoproperty would create a hidden backing variable that would not match up to the private grdItemsInGrid right?

Is resharper broken? or is there something in C#.NET that I am not aware of?


EDIT: I am sensing a common miss-communication here. grdItemsInGrid is a grid that is displayed on a form (MyClass actually implements Window). The idea is to grant access to grdItemsInGrid to scenarios where MyClass is passed as an IMyInterface to a method.

+1  A: 

It is an equal transformation. You're exactly right about it using a hidden backing variable.

Here's the MSDN page on the language feature: http://msdn.microsoft.com/en-us/library/bb384054.aspx

fatcat1111
It is not an equal transformation if he has other code that references that private backing variable. However, I don't use Resharper (gasp!) so maybe Resharper is smart enough to verify the private member is unused.
JMD
Alternatively, it would rewrite any code that uses the backing variable to access it through the property instead.
Anon.
@JMD - I believe it does exactly that
Isaac Cambron
@Anon, I would definitely NOT want it to do that. Resharper may replace all accesses with the auto-property, but I can then turn around and make the property explicitly implemented, meaning those `get` and `set` would now be called when I didn't expect them to be. I'll hope that @icambron is right, that Resharper verifies there are no references to the private backing variable before removing it. :)
JMD
@JMD, you have a pretty unusual stand on this issue. Usually, a property is a logical abstraction over a field, allowing you to change behaviour on set/get without that the code using it must be aware of it. A simple property accessor has no overhead whatsoever. (inlined) Which means, that you are expected only to use the field directly in cases where you know for good reason NOT to use the property. Just using the field for some unproven assumption (performance) or dogmatic habit, and thus bypassing the abstraction w/o any real value doesn't seem to be very logical to me...
Robert Giesecke
@Robert, I believe you are either misunderstanding or overstating my "dogmatic" stand on the subject. I don't think I have any such stance. If you worked with me you'd know I'm very pragmatic and I try to consider implications. Consider that "some other developer" modifies the explicit get/set accessors on that property such that it now has other side effects, possibly minor or maybe not. Do you still want Resharper to replace the private backing variable with references to the property itself?
JMD
WRT "dogmatic", I use that term, when I see people doing things because they always did. It kinda read like that. But that is the internet, can't know everything about someone except the few kb of text... @R# Its default rules are as such, that they will pop up as suggestions only if it would be a real refactoring in the classic sense (no behavioral change), it will inspect the accessors. No tool can prevent you from cases where "some other developer" makes a mistake. Using the field directly would make it hard to easily and transparently add support for e.g. change notification.
Robert Giesecke
+5  A: 

Autoproperties remove the need for a visible backing variable. Nothing is broken here. It's just a very convenient feature.

Aaron
I think his concern is that code that references grdItemsInGrid may still reference that variable instead of the new hidden backing variable.
AaronLS
If that's the case, he can just delete the backing variable and let the compiler let him know.
johnc
A: 

Have you tried it? Maybe it is going to delete the grdItemsInGrid and then fix up any references to it so that they instead use the property.

AaronLS
A: 

I don't see your issue? No, the autoproperty would not match grdItemsInGrid, but for what would you need grdItemsInGrid? Your class would interact with ItemsInGrid directly as you don't have access to the hidden backing field, but in your logic you don't need that access anyway.

Michael Stum
When I pass an instance of MyClass as a parameter of IMyIterface then I want to be able to show/hide/access MyClass.grdItemsInGrid via IMyInterface.ItemsInGrid
Vaccano
+1  A: 

Your interface contract defines the name of a property, not the private backing variable.

So the suggested refactoring is valid.

Mitch Wheat
Would the downvoter please leave a comment. Thanks.
Mitch Wheat
A: 

Auto property is a VS 2008 feature that prevent lazy programmers (like me ;) ) to use public fields in their classes and compiler will do the job for you (behind the scene as you said) and there will be a private variable backing your property (you can use Reflector to see what's happening) As long as implicit interface implementation cares there should be a property with the given name declared in your class it doesn't matter if it's auto property or not.

Beatles1692
Actually it's a C#3.0 feature, regardless of IDE
johnc
Actually it's a compiler feature :)
Beatles1692
It is part of the C# 3 language to allow auto properties.
Vaccano
I know but the actual work is done by compiler itself it's more like a complier directive and in programming languages like Lisp or Ruby you can write your own compiler directives.
Beatles1692
True. I was not sure I believed you so I looked it up: http://msdn.microsoft.com/en-us/library/bb384054.aspx The first paragraph says "...the compiler creates a private, anonymous backing field can only be accessed through the property's get and set accessors..."
Vaccano
+1  A: 

If you're not going to do anything special with that private field, you can safely accept resharper's suggestion. It will work exactly the same way. I'm not sure if reshaper will remove the private member in your case, since is named grdItemsInGrid and not itemsInGrid.

Fernando
A: 

As far as I can see this is a bug in resharper. The private variable in question is a element on a form. I did not show this because I did not realize it was relevant. Because the form stuff is in a different file (it is a partial class). Resharper does not realize that the private variable is being referenced and thinks I can just go to an auto property.

Once I referenced the private variable in the same file then ReSharper stopped hinting that I should convert to an auto property. (But it did not realize that it was referenced in the partial class for purposes of this refactor.)

Vaccano
How is this a bug if you didn't have a reference to the field?
oɔɯǝɹ
I do have a reference to it. It is just in a different file. (As I said it is a partial class.) Resharper is not seeing the reference in the other file.
Vaccano
The amount of confusion about this question is amazing. Anyways, the fact that resharper treats usage of a private field differently if it is in the same file or in a different partial implementation pretty much seals this as a bug. You might want to report it, along with the two test cases that show the difference in behavior: http://www.jetbrains.net/devnet/docs/DOC-279
Chris
I logged a bug for this at http://www.jetbrains.net/jira/browse/RSRP-150833
Vaccano
I think that now the issue is understood, it would be a good idea for the downvoters on this answer to convert their down votes into up votes. It would only be fair.
Mark Booth
A: 

There is no logic involved in either setter or getter. So how on earth could this refactoring change your code in any way?

ReSharper knows this and will show you the refactoring in question because it is an obvious one.

What makes you think, there's any change in how your code behaves?

Robert Giesecke
I want to expose the private variable that is used in the window via the interface
Vaccano
So? What behavioral change would ReSharper introduce to your code? -> None. It wouldn't have proposed the change if that would change your code's behavior.
Robert Giesecke
By going to the hidden private variable (ie the auto property) the refactor that resharper is suggesting would have my property no longer referencing the grid that is displayed on the form
Vaccano
Ah, sorry. Didn't get that. Well yeah, that would be a bummer... (the Winforms designer wouldn't be able to cope with that, I guess)
Robert Giesecke