Language enhancements I'd like to see.

Awhile back I wrote about my Whidby hopes, dreams, and concerns. My friend Michael wrote me today with a wishlist of his own. I realize it's kind of late in the game for additional language features, but his ideas are good (in my opinion) so I'm going to post them anyway in the hope that maybe, just maybe someone at Microsoft might take note.

1. Different scope/access modifieres for the property getter and setter.

public int Property1

   get 
   { 
      return m_iProperty1; 
   }
}

private int Property1

   set 
  { 
      m_iProperty1 = value; 
   }
}

I've personally run into this one. I tend to run into this when I want to inherit from a class that has a readonly property, and I want to set the property in the base class without exposing the setter to the rest of the world. I end up having to write a SetPropertyName(value as object) procedure (sorry about the mixed C#/VB syntax) in the child class. FXCop doesn't like the SetPropertyName() method and recommends that I "use a property setter" instead. D'oh!

2. Add a new access modifier for data members that can only be accessed through property methods even from within the containing class.

Michael's C# example (given the above property sample):

secret int m_iProperty1;
private void AMethod()

   m_iProperty1 = 2; // Compiler error 
   Property1 = 2; // Ok, no error
}

My VB .NET Example:

Public Property MyValue() As String

Private _Value as string = String.Empty   'Scope limited to getter and setter

Get

return _Value

End Get

Set(ByVal Value As String)

me._Value = Value

End Set

End Property

Comments

# re: Language enhancements I'd like to see.

Tuesday, May 31, 2005 5:11 PM by Wedgebert

I know VB is getting (back) the differing scopes of properties.

The secret thing would be pretty nice. It would clean up the code since the variable declartion would be in the property.

# re: Language enhancements I'd like to see.

Tuesday, May 31, 2005 5:17 PM by Stewart Robertson

I think you'll find whidbey already does your first wish (differently scoped getter/setters for properties)

public int Testint
{
get
{
return 1;
}
protected set
{
//set stuff
}
}

or in VB:
Public Property TestInt() As Integer
Get
Return 1
End Get
Protected Set(ByVal value As Integer)
'Set Stuff
End Set
End Property

Very handy feature for the exact reason you've already covered - being able to add an 'internal' modifier to a set is also useful

# Your wish is our command

Tuesday, May 31, 2005 5:30 PM by TrackBack

Good news for some .NET developers

# Your wish is our command

Tuesday, May 31, 2005 5:31 PM by TrackBack

Good news for some .NET developers

# re: Language enhancements I'd like to see.

Wednesday, June 01, 2005 11:50 AM by Michael Stone

Glad to see we get half of this. Shame about the other half though. I like Wedgebert's idea of putting the 'secret' member in the property:

<pre>
public int Property1
{
int m_iProperty1;
get
{
return m_iProperty1;
}
set
(
m_iProperty1 = value;
)
)
</pre>

Very concise syntax. Keeps the member hidden even from the rest of the class so you have to use the property to access it.

I realize, of course, that both of these features could be accomplished through subclassing but I thought this would be more elegant.

# re: Language enhancements I'd like to see.

Wednesday, June 01, 2005 11:52 AM by Michael Stone

Oh, I see it was Chris who came up with the idea in his VB example. This VB stuff's all greek to me...

Leave a Comment

(required) 
(required) 
(optional)
(required)