Is it correct to correct properties values on the fly?
Is it correct to correct properties values on the fly?
for example: (note the .ToLower)
Public Property X() As String
Get
Return _x.ToLower
End Get
Set(开发者_运维知识库ByVal Value As String)
_x = value.ToLower
End Set
End Property
There is nothing incorrect about standardizing your properties in getter/setters. Without any context of what X represents it is hard determine if a property is the right way to access and update the value of X. Depending on the application, it might make sense to not have a public setter at all but instead have a method such as CustomerRequestedXToChange(XUpdatedValue as String)
Some improvements to your code though:
- Be sure that _x is private so that no other classes can modify the value.
- Only perform ToLower on the setter, not both. As long as you follow the next convention that should work fine.
- All calls to _x within this class should go through X, that way the value of _x will be correct.
精彩评论