Rather new to the OOP approach, I got some questions concerning efficiency.
In my class I have some private functions. Some properties return values based upon these functions.
Would it be best if I set a variable in my class and Initialize it in the constructor, then return this variable, like so:
VB Code:
Public Class MyMovie Private strMovie As String Private strName As String Private strYear As String 'Constructor Public Sub New(ByVal strFullMovieName As String) strMovie = strFullMovieName strName = DoABunchOfStuff() 'This is a private function in my class End Sub Public ReadOnly Property Name() As String Get Return strName End Get End Property (...)
Or by calling the function directly from the property, like so:
VB Code:
Public Class MyMovie Private strMovie As String Private strName As String Private strYear As String 'Constructor Public Sub New(ByVal strFullMovieName As String) strMovie = strFullMovieName End Sub Public ReadOnly Property Name() As String Get Return DoABunchOfStuff() End Get End Property (...)
I reckon that in the latter example each time I 'm getting the value of that property, the private function has to be executed where as in the first example this only happen when creating an instance my class. The first would be more efficient, better. Am I right or ?




Reply With Quote