开发者

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:

  1. Be sure that _x is private so that no other classes can modify the value.
  2. Only perform ToLower on the setter, not both. As long as you follow the next convention that should work fine.
  3. All calls to _x within this class should go through X, that way the value of _x will be correct.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜