VB.NET help with dates

Soldato
Joined
1 Feb 2006
Posts
8,188
Hi,

I have the following code but am having problems using null dates as optional parameters in a function. See below...

Code:
Public Shared Function SendMail(Optional ByVal o As User = Nothing, Optional ByVal configid As Integer = Nothing, Optional ByVal dob As Nullable(Of DateTime) = Null, Optional ByVal email As String = Nothing) As User
   Return UserData.SendMail(o, configid, dob, email)
End Function

The error is with this part:

Code:
Optional ByVal dob As Nullable(Of DateTime) = Null

The error message displayed says 'Optional parameters cannot have structure types'. Any ideas what I need to do here? If no date parameter is passed I want to pass null or nothing.
 
Using .NET 4?

C# equivalent of this is:
Code:
DateTime? dateTime = null
and it works fine, and having not used VB.NET I can't help any more than this I'm afraid :)
 
Ah, that might be it then.. default values are a .NET 4 feature (according to my ide that complains when I use it in 3.5!)
 
Ah, that might be it then.. default values are a .NET 4 feature (according to my ide that complains when I use it in 3.5!)

Default & Optional parameters are new in C# 4 (it's a language thing rather than a framework thing), but VB.NET has had them pretty much forever AFAIK.

And to the OP, searching for the error message brings up a few threads talking about this. Looks like using overloading should fix it as you suggest though.

http://www.vbforums.com/showthread.php?t=420153
 
Got sorted with this. VB.NET doesn't seem to get on with null dates at all so instead I had to use SqlTypes.SqlDateTime.MinValue. I could then ignore this value in my Stored Proc.
 
Back
Top Bottom