1
我確實創建了一個能夠處理不準確的日期和時間的XDateTime類。ByRef參數與類型轉換
該類具有轉換字符串和字符串所需的所有CType運算符,並且它在過去的幾個月中已經過全面測試。
Public Shared Widening Operator CType(ByVal xDateTime As FrameworkBL.XDateTime) As String
Dim retrunValue As String = Nothing
If xDateTime Is Nothing Then
retrunValue = Nothing
Else
retrunValue = xDateTime.StringValue
End If
Return retrunValue
End Operator
Public Shared Narrowing Operator CType(ByVal value As String) As FrameworkBL.XDateTime
Dim returnValue As FrameworkBL.XDateTime = Nothing
If String.IsNullOrEmpty(value) Then
returnValue = Nothing
Else
returnValue = New FrameworkBL.XDateTime(value)
End If
Return returnValue
End Operator
然而,當一個對象的ByRef參數中返回一個字符串,我CTYPE運營商似乎被忽略和cast異常。
Private Sub Test()
Dim myXDateTime As FrameworkBL.XDateTime
myXDateTime = "200101010000007" 'Ok
Me.Temp1(myXDateTime) 'Ok
Me.Temp2(myXDateTime) 'Ok
Me.Temp3(myXDateTime) 'Unable to cast object of type 'System.String' to type 'FrameworkBL.XDateTime'
End Sub
Private Sub Temp1(ByRef myObject As String)
myObject = "200201010000007"
End Sub
Private Sub Temp2(ByRef myObject As XDateTime)
myObject = "200301010000007"
End Sub
Private Sub Temp3(ByRef myObject As Object)
myObject = "200401010000007"
End Sub
這種問題是documented by Microsoft但我無法找到一個有效的解決方案來解決這個問題。我是死路一條還是有一個選項可以讓我保留我的ByRef Object參數?