Reputation: 30855
Is there ever a situation where I should do the following in .NET instead of using a property with read/write capability?
private S as string
public function GetS() as string
return S
end function
public sub SetS(byval NewS as string)
S = NewS
end function
Do properties simply provide a more efficient way for doing the same thing?
Will properties be any slower than the above accessor functions in a high performance application?
Upvotes: 5
Views: 11414
Reputation: 11
With N-Tier designs, in my BO, I store property values with an unset value, then set it when accessed the first time.
Private _aValue As integer =-1
Private ReadOnly Property aValue As integer
Get
If _aValue = -1 Then
_aValue = DA.General.GetaValue()
End If
Return _aValue
End Get
End Property
In this way, I never worry if I parse properties in the correct order, since I basically lazy load them.
Upvotes: 1
Reputation: 144
Another decision around whether to use properties or functions is when instantiating the parent object. VB has that wonderfully convenient syntax, e.g.
Dim MyStudent as Student With {.Name = "Bobby", .Age = 10}
Only properties are available in the With section.
I maintain an app which has a lot of persisted objects in a database, which a lot of relationships between objects. e.g. a student has a teacher, and the teacher is peristed in the DB.
I typically use a WriteOnly property to set the Teacher, since this is a light operation, but given there is potentially expensive DB access to retrieve the Teacher, I use a function to retrieve. In other words:
Public Writeonly Property Teacher() as Teacher
Public Function GetTeacher() as Teacher
This lets me instantiate a Student in the form With {.Teacher = SomeTeacherObject}
but GetTeacher encourages caching of the Teacher object in user code and not just using it as a Property which may or may not result in multiple DB access calls.
If anyone has any comments on this approach I would love to hear them.
Upvotes: 3
Reputation: 704
Yes, at least in my experience I try to avoid properties and use function and routines in place. Here is my reasons why:
Less Code, while it's not a HUGE amount, defining properties in VB requires at least 2 extra lines of code per get or set. The Overhead is double the lines of actual code for a single assignment or return operation. Although small this makes reading code much more difficult, VB is already an obsessively wordy language.
Private ReadOnly Property Foo As String
Get
Return Bar
End Get
End Property
vs
Private Function Foo As String
Return Bar
End Function
Properties are less flexible you must return what the same value you get. In Other words you can't set the value using a String
and get using an Integer
or overload setting with a String
, or an Integer
.
Now to be fair the Reasons I use Properties is to get the =
syntax for setting, which this doesn't apply when you have a read only property. Also Properties can be set in the VS editor in the properties dialog.
Upvotes: 2
Reputation: 11457
Properties are actually syntactic sugar for methods called get_MyProperty
and set_MyProperty
, so there is no performance difference.
Upvotes: 3
Reputation: 564413
Properties, internally, are nothing but a pair of methods. They basically evaluate to a get and set accessor method.
You should use properties, unless the property is going to cause some unexpected, potentially long running side effect, or there is some other good reason to use a method.
For details, I suggest reading the Property Usage Guidelines on MSDN. In particular, use a method when:
Otherwise, I'd use a property. Brad Abram's blogged some other details, including good reasons why certain API functions use methods (mostly because they could cause cross-computer communication, which would fall into the "side effect" category).
Upvotes: 10