Auto properties : What ifs ...

Posted Fri, Mar 28 2008 13:40 by bill

Paul Vick posted a speculative post as to Automatically Implemented properties for VB10.  Although it's kind of nice, it really is just a minor modification from what C# did in 3.0 and misses a lot of the "what if" scenarios we should be asking.

What if :

  • You want to add a break point on the property set  or get?
  • You need to add some validation or logging ?
  • You want your existing code to look consistent ?

What's being proposed falls down on all those counts.  A better approach, IMO, would be to just auto collapse properties, similar to what Refactor! does. That works with existing code1, and addresses all the what ifs above.

I also expect there will be IDE issues around the syntax Paul proposes as it is no different from the opening line of a Property today. So when you hit enter at the end of that line, you'd expect code completion. So adding lines between these auto properties will potentially break code.

And when you do press enter, let's assume it does complete the code block for you:  it would need to include the backing field otherwise it risks breaking code elsewhere. That's a major change from today, where unless you use a snippet, you only get the property stubs.


What I would like to see, is allow the backing field to be declared inside the property block, such as :

   Public Property Name() As String

     Dim m_Name As String


         Return m_Name

      End Get

      Set(ByVal value As String)

         m_Name = value

      End Set

   End Property


Then the property truly can be collapsed and expanded as it is fully encapsulated.

I'd also especially like us to be able to specify the default prefix used for backing fields when generated, be it m_ or _ or m etc.  We shouldn't have to change our coding styles to match an inflexible code generator.

I think this feature needs to be looked at in terms of existing code, and code maintenance, not just a quick fix for writing pass through properties.




1. There's a bug in Refactor with properties in a Module, where it hides the property, not the backing field.

Filed under: , ,


# re: Auto properties : What ifs ...

Friday, March 28, 2008 7:57 AM by Morgan Persson

For the proposed solution there seems no way to handle a public Get and a protected Set. IIRC C# can do that.

# re: Auto properties : What ifs ...

Friday, March 28, 2008 8:28 AM by bill

Hey Morgan,

Yep, good point. In C# a auto property can have a mixed accessibility, such as :

public string Foo { get; private set; }

# re: Auto properties : What ifs ...

Sunday, March 30, 2008 6:00 PM by Nick

Bill, I have to disagree I personally hate the auto-collapsing feature of Refactor!  I actually decided to remove it purely based on how annoying this "feature" was - perhaps there's a way to disable it.....

I really like the C# implementation and if we can get something like that I think it would be a great time saver.

# re: Auto properties : What ifs ...

Sunday, March 30, 2008 10:51 PM by bill

Hey Nick,

Well we might have to agree to disagree then ;)  The C# iplementation simply wouldn't work in VB, especially such as having a mixed accessibility (private set, public get etc).

I also think the C# implementation is okay only for writing code the first time, but as soon as you want to modify anything it needs a refactoringn tool.  I expect as time goes on that is the exact complaint we'll see more and more of from C# folks .