close

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 = quot;-quot;
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,
quot;Definitions of property procedures for the same property are inconsistent,
or property procedure has an optional parameter, a ParamArray. Or an invalid
Set final parameterquot;

My intention:
1) Taking a string of Date (e.g., quot;2005-11-23quot;)
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.
---
Tetsuya Oguma, SingaporeHi,

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

Your property let amp; 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 amp; then use the function to return the correctly cast value to
set the property.

Regards,

Chris.

--
Chris Marlow
MCSD.NET, Microsoft Office XP Masterquot;Tetsuya Ogumaquot; wrote:

gt; Hi all,
gt;
gt; I have a class called clsTrade and in it:
gt;
gt;
gt; Dim m_dtSettleDate As Date
gt;
gt; Property Let SettleDate(szSettleDate As String)
gt; m_dtSettleDate = ParseStringToDate(szSettleDate)
gt; End Property
gt;
gt; Property Get SettleDate() As Date
gt; SettleDate = m_dtSettleDate
gt; End Property
gt;
gt; Private Function ParseStringToDate(szDate As String) As Date
gt; Const HYPHEN As String = quot;-quot;
gt; Dim lYearPos As Long
gt; Dim lMonthPos As Long
gt;
gt; On Error GoTo ErrorHandler
gt; With VBA
gt; lYearPos = .InStr(1, szDate, HYPHEN, vbTextCompare)
gt; lMonthPos = .InStr(lYearPos 1, szDate, HYPHEN, vbTextCompare)
gt;
gt; ParseStringToDate = .DateSerial(.Left$(szSettleDate, lYearPos - 1), _
gt; .Mid$(szSettleDate, lYearPos 1, lMonthPos
gt; - lYearPos - 1), _
gt; .Right$(szSettleDate, .Len(szSettleDate) -
gt; lMonthPos))
gt;
gt; End Function
gt;
gt; ErrorHandler:
gt; 'Put today's day as default first
gt; ParseStringToDate = .DateSerial(.Year(Now), .Month(Now), .Day(Now))
gt; End With
gt; End Function
gt;
gt;
gt; When compiled an error says by highlilghting the above Property Let,
gt; quot;Definitions of property procedures for the same property are inconsistent,
gt; or property procedure has an optional parameter, a ParamArray. Or an invalid
gt; Set final parameterquot;
gt;
gt; My intention:
gt; 1) Taking a string of Date (e.g., quot;2005-11-23quot;)
gt; 2) Parse and convert it to Date format through ParseStringToDate function
gt; and store it in m_dtSettleDate
gt;
gt; Now, I am trying to set a return value of Property Let statement as Date and
gt; cannot!
gt;
gt; My convictions:
gt; 1) I think it is reasonbale to have Private Function ParseStringToDate in
gt; this clsTrade
gt; 2) The Property Let takes 1 parameter of String, converts it to Date and
gt; store it, which I think is again reasonable.
gt;
gt; My questions:
gt; 1) Am I coding in a good OO principle (to some extent)?
gt; 2) How can I get around the error I am getting?
gt;
gt; Thanks for your time.
gt; ---
gt; Tetsuya Oguma, Singapore
gt;

全站熱搜
創作者介紹
創作者 software 的頭像
software

software

software 發表在 痞客邦 留言(0) 人氣()