J
Joergen Bech
(Slightly religious question):
Suppose I have the following class:
---snip---
Public Class MyClass
Private _MyVariable As Integer
Public Property MyVariable() As Integer
Get
Return Me._MyVariable
End Get
Set(ByVal value As Integer)
'(some value checking going on here)
Me._MyVariable = value
End Set
End Property
Private Sub MyMethod1
_MyVariable = 0
End Sub
Private Sub MyMethod2
MyVariable = 0
End Sub
End Class
---snip---
Is there a recommended best practice (class library design
guidelines?) for accessing _MyVariable?
MyMethod1 takes the direct route, thereby bypassing any
value checking and side effects in the property setter.
MyMethod2 goes through the property setter, which has
the benefit of value checking/side effects, but might be
overkill considering the performance hit incurred by the
extra function call, especially if the values assigned are
known to be valid.
Still, if the priority is *maintainability*, wouldn't it be best only
to use the direct access in the constructor only and the
property setter in all other cases? To avoid multiple side
effects as a result of setting multiple properties at once
(e.g. if the class is a graphical control), would the best
practice be to disable all updates, set multiple properties,
then enable updates again, in order to use property setters
all the time internally?
What do you do?
/JB
Suppose I have the following class:
---snip---
Public Class MyClass
Private _MyVariable As Integer
Public Property MyVariable() As Integer
Get
Return Me._MyVariable
End Get
Set(ByVal value As Integer)
'(some value checking going on here)
Me._MyVariable = value
End Set
End Property
Private Sub MyMethod1
_MyVariable = 0
End Sub
Private Sub MyMethod2
MyVariable = 0
End Sub
End Class
---snip---
Is there a recommended best practice (class library design
guidelines?) for accessing _MyVariable?
MyMethod1 takes the direct route, thereby bypassing any
value checking and side effects in the property setter.
MyMethod2 goes through the property setter, which has
the benefit of value checking/side effects, but might be
overkill considering the performance hit incurred by the
extra function call, especially if the values assigned are
known to be valid.
Still, if the priority is *maintainability*, wouldn't it be best only
to use the direct access in the constructor only and the
property setter in all other cases? To avoid multiple side
effects as a result of setting multiple properties at once
(e.g. if the class is a graphical control), would the best
practice be to disable all updates, set multiple properties,
then enable updates again, in order to use property setters
all the time internally?
What do you do?
/JB