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

G

Guest

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.
 
M

Mattias Sjögren

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
 
G

Guest

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.
 
G

Guest

In fact, the overload you suggested, flags the 2 parameters, since as you
pointed out, nullable(of t) does not implement IComparable.
 
M

Mattias Sjögren

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
 
L

Linda Liu [MSFT]

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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

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

Linda Liu [MSFT]

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
 

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