This is something I’m not much consistent about and always curious about what other people do.
How do you access internal properties (private or public)?
For example you’ve got this property :
Private _Name As String Public Property Name() As String Get Return _Name End Get Set(ByVal value As String) _Name = value End Set End Property
In the same class within another function which one do you prefer? and why?
_Name = 'Johnny'
or
Name = 'Johnny'
Ignore the fact that I used Name instead of Me.Name.
Personally I prefer to use the property where possible. This means you still get the validation, and you can easily put breakpoints on the property access. This doesn’t work where you’re trying to make a change to two properties which validate against each other – for instance, a ‘min and max’ pair, where each has a validation constraint such that
min <= maxat all times. You might have (C#):At some point in the future, I’d like to see C# gain the ability to declare the backing field within the property, such that it’s private to just the property:
Outside the property, you wouldn’t be able to get at
nameat all, onlyName. We already have this for automatically implemented properties, but they can’t contain any logic.