468,134 Members | 1,229 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,134 developers. It's quick & easy.

Can someone explain this issue with nullable(of DateTime)...

I have a generic function I am using to check constraints, an example of
which is the following:

Public Function ValidateMinValue(Of t As IComparable)(ByVal PropertyName As
String, ByVal value As t, ByVal min As t) As Boolean
Dim newMessage As String = String.Empty

If value.CompareTo(min) < 0 Then
newMessage = String.Format("{0} must not be less then {1}, please
enter a valid value.", PropertyName, min)
' Add the message to the validation list
AddValidationError(PropertyName, newMessage)
Return False
Else
Return True
End If
End Function

I have 2 'Nullable(Of DateTime)' variables - DateReceived and DateDeposited.
When I attempt to call the function:

ValidateMinValue("Date Deposited", DateDeposited.Value, DateReceived.Value)
both Date arguments are flaged saying that:
"A value of type 'Date' cannot be converted to 'T'.

but of course, Date (DateTime) implements IComparable.

also, if I make the following change:
dim d1 as DateTime = DateDeposited.Value
dim d2 as DateTime = DateReceived.Value
ValidateMinValue("Date Deposited", d1, d2)
there are no errors. Not sure why this is necessary.
An explaination would be appreciated.

--
Terry
Aug 29 '07 #1
6 3335
>I have 2 'Nullable(Of DateTime)' variables - DateReceived and DateDeposited.
When I attempt to call the function:

ValidateMinValue("Date Deposited", DateDeposited.Value, DateReceived.Value)
both Date arguments are flaged saying that:
"A value of type 'Date' cannot be converted to 'T'.

but of course, Date (DateTime) implements IComparable.
Right, Date implements IComparable, but that doesn't imply that
Nullable(Of Date) also implements it.

I suppose you could add another overload to handle this case,
something like

Public Function ValidateMinValue(Of t As IComparable)(ByVal
PropertyName As String, ByVal value As Nullable(Of t), ByVal min As
Nullable(Of t)) As Boolean
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 29 '07 #2
Yes, but the value for the parameter is not a Nullable(of Date), it is the
value property of a Nullable(of Date), which is of type Date. Even the error
message says it is of type Date.
--
Terry
"Mattias Sjögren" wrote:
I have 2 'Nullable(Of DateTime)' variables - DateReceived and DateDeposited.
When I attempt to call the function:

ValidateMinValue("Date Deposited", DateDeposited.Value, DateReceived.Value)
both Date arguments are flaged saying that:
"A value of type 'Date' cannot be converted to 'T'.

but of course, Date (DateTime) implements IComparable.

Right, Date implements IComparable, but that doesn't imply that
Nullable(Of Date) also implements it.

I suppose you could add another overload to handle this case,
something like

Public Function ValidateMinValue(Of t As IComparable)(ByVal
PropertyName As String, ByVal value As Nullable(Of t), ByVal min As
Nullable(Of t)) As Boolean
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 29 '07 #3
In fact, the overload you suggested, flags the 2 parameters, since as you
pointed out, nullable(of t) does not implement IComparable.

--
Terry
"Mattias Sjögren" wrote:
I have 2 'Nullable(Of DateTime)' variables - DateReceived and DateDeposited.
When I attempt to call the function:

ValidateMinValue("Date Deposited", DateDeposited.Value, DateReceived.Value)
both Date arguments are flaged saying that:
"A value of type 'Date' cannot be converted to 'T'.

but of course, Date (DateTime) implements IComparable.

Right, Date implements IComparable, but that doesn't imply that
Nullable(Of Date) also implements it.

I suppose you could add another overload to handle this case,
something like

Public Function ValidateMinValue(Of t As IComparable)(ByVal
PropertyName As String, ByVal value As Nullable(Of t), ByVal min As
Nullable(Of t)) As Boolean
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 29 '07 #4
>Yes, but the value for the parameter is not a Nullable(of Date), it is the
value property of a Nullable(of Date), which is of type Date. Even the error
message says it is of type Date.
Oops your right, sorry I misread your code.

OK so that is a bit weird. Seems like the compiler fails to derive
that T is Date in this case. I'm not sure if it should be able to or
not. You can make the code compile by explicitly specifying T

ValidateMinValue(Of Date)("Date Deposited", DateDeposited.Value,
DateReceived.Value)
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 29 '07 #5
Hi Terry,

I agree with Mattias that you should explicitly specify the type parameter
to a concrete type when you call a function that has a type parameter.

If you don't do like that, the compiler doesn't know which type the
function will use, so compilation errors occur.

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 30 '07 #6
Hi Terry,

I am reviewing this post and would like to know the status of this issue.

If you have any concern, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support

Sep 3 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Maciej Nadolski | last post: by
5 posts views Thread by J Allen Horner | last post: by
7 posts views Thread by CMirandaman | last post: by
1 post views Thread by Tran Hong Quang | last post: by
27 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.