Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class_Initialize or Property Get

Status
Not open for further replies.

scottsanpedro

Programmer
Apr 26, 2002
97
GB
Can someone tell me the advantage or reason for running code in the class_initialize against putting into a Get Property, if maybe they gave the same answer?
Hope that makes sense.
Many thanks
Scott
 
One possible difference is that using the Initialize method guarantees that the value is set while the Get provides a setting only if you use it. For example
Code:
Private mvarTheDate As Date

Public Property Let TheDate (Vdata As Date)
   mvatTheDate = vData
End Property

Public Property Get TheDate() As Date
   If mvarTheDate = 0 Then
      TheDate = Date()
   Else 
      TheDate = mvarTheDate
   End If
End Property

Public Function DaysElapsed (SomeDate As Date) As Long
   DaysElapsed = DateDiff("d", SomeDate, mvarTheDate)
End Function
This code returns a problematic result for "DaysElapsed" because the value of mvarTheDate has not been set (i.e. we didn't go through the "Get" to determine it.)

However
Code:
Private mvarTheDate As Date

Public Property Let TheDate (Vdata As Date)
   mvatTheDate = vData
End Property

Public Property Get TheDate() As Date
   TheDate = mvarTheDate
End Property

Public Function DaysElapsed (SomeDate As Date) As Long
   DaysElapsed = DateDiff("d", SomeDate, mvarTheDate)
End Function

Private Sub Class_Initialize()
   mvarTheDate = Date()
End Sub
Works correctly because the value of mvarTheDate is guaranteed to be initialized when the class is instantiated.
 
ok. I understand.
That is a good reason to me.
So really depending on the circumstances of the class.
Thanks for your answer.
Slowly getting my head around all this.
Scott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top