Property Let: assign return value of Double when passing String

T

Tetsuya Oguma

Hi all,

I have a class called clsTrade and in it:

+++
Dim m_dtSettleDate As Date

Property Let SettleDate(szSettleDate As String)
m_dtSettleDate = ParseStringToDate(szSettleDate)
End Property

Property Get SettleDate() As Date
SettleDate = m_dtSettleDate
End Property

Private Function ParseStringToDate(szDate As String) As Date
Const HYPHEN As String = "-"
Dim lYearPos As Long
Dim lMonthPos As Long

On Error GoTo ErrorHandler
With VBA
lYearPos = .InStr(1, szDate, HYPHEN, vbTextCompare)
lMonthPos = .InStr(lYearPos + 1, szDate, HYPHEN, vbTextCompare)

ParseStringToDate = .DateSerial(.Left$(szSettleDate, lYearPos - 1), _
.Mid$(szSettleDate, lYearPos + 1, lMonthPos
- lYearPos - 1), _
.Right$(szSettleDate, .Len(szSettleDate) -
lMonthPos))

End Function

ErrorHandler:
'Put today's day as default first
ParseStringToDate = .DateSerial(.Year(Now), .Month(Now), .Day(Now))
End With
End Function
+++

When compiled an error says by highlilghting the above Property Let,
"Definitions of property procedures for the same property are inconsistent,
or property procedure has an optional parameter, a ParamArray. Or an invalid
Set final parameter"

My intention:
1) Taking a string of Date (e.g., "2005-11-23")
2) Parse and convert it to Date format through ParseStringToDate function
and store it in m_dtSettleDate

Now, I am trying to set a return value of Property Let statement as Date and
cannot!

My convictions:
1) I think it is reasonbale to have Private Function ParseStringToDate in
this clsTrade
2) The Property Let takes 1 parameter of String, converts it to Date and
store it, which I think is again reasonable.

My questions:
1) Am I coding in a good OO principle (to some extent)?
2) How can I get around the error I am getting?

Thanks for your time.
 
C

Chris Marlow

Hi,

Your OO is fine, as far as VBA Excel OO goes.

Your property let & property get must be of the same type. They are more for
accessing the underlying variable, rather than manipulating it (although you
can do some manipulation - that is not really how it is intended to be used).
You should use a Public Sub (say 'Public Sub SetSettleDate') to which you can
pass the text & then use the function to return the correctly cast value to
set the property.

Regards,

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top