473,499 Members | 1,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3520
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3133
by: Maciej Nadolski | last post by:
Hi! I can`t understand what php wants from me:( So: Cannot send session cache limiter - headers already sent (output started at /home/krecik/public_html/silnik.php:208) in...
6
16905
by: Ken Varn | last post by:
Sometimes when I try to close my managed C++ application, the following dialog displays in Win 2000 Pro: The title of the dialog is "Server Busy". The message is "This action cannot be completed...
5
2784
by: J Allen Horner | last post by:
I'm just starting to learn C, and the tutorial I'm reading uses this code as an example: ----------- #include <stdio.h> #define MAX 10 int a; int rand_seed=10;
7
1331
by: CMirandaman | last post by:
I have another newbie question I could use some help with. I ran across this code in one of the MS training guides: public class NullTokenVisitor {...} public class Application { public...
1
1239
by: Tran Hong Quang | last post by:
Hi, In header file, I see this declaration: #define DECLARE_VTBL(iname) iname vt##iname; then, in C file, there are below codes: typedef struct _Abc { .......... } abc;
0
1125
by: Rick | last post by:
VS2005 Pro I'm binding to a custom business object with type Nullable(of date) and trying to validate user input. I have an error provider connected to the binding object which is connected...
6
1334
by: blue | last post by:
The following is from http://developer.yahoo.com/yui/examples/dragdrop/dd-ontop.html What does the (function(){ })(); syntax actually do? I'm going to guess that is creates an unnamed function?!...
2
3331
by: BobRoyAce | last post by:
I am using VB.NET and I have a variable that is defined as Nullable(Of Single) as follows: Dim fMyNullableSingle as Nullable(Of Single) Then, I have another variable as follows: Dim...
2
1315
by: Peted | last post by:
Using vs2008 c# i have this code snipet note: SUBMITTED_DT = DateTime? type if ((DateTime.Now - billingEvent.SUBMITTED_DT).Days 180) { throw new...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.