By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,812 Members | 1,288 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,812 IT Pros & Developers. It's quick & easy.

"Optional ByVal SomeDate As Date = Nothing" in VB2005

P: n/a
In our applications, we use the special value of DateTime.MinValue to
represent "null dates" throughout all our code. We recently ran into an
issue where we wanted an optional date parameter for a procedure. We weren't
able to declare it with DateTime.MinValue as its default value, as MinValue
is a read-only property rather than a constant. To work around, we had to
use a "magic date" that we checked for later on. I was never very happy with
that solution.

Today however I noticed that VB2005 is quite happy with the following:

\\\
Public Sub DoSomething(Optional ByVal SomeDate As Date = Nothing)
[...]
End Sub
///

(I believe this fails to compile in VB2003).

The value that is present in SomeDate if no date is provided by the calling
code is DateTime.MinValue (i.e., the default value for an uninitialised date
variable).

Is this the intended documented behaviour of declaring the optional
parameter in this way? Can I use this safe in the knowledge that the
behaviour won't break or change in the next release of VB?

Thanks,

--

(O)enone
Jun 15 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Oenone" <oe****@nowhere.com> schrieb:
In our applications, we use the special value of DateTime.MinValue to
represent "null dates" throughout all our code.
I don't think this is an optimal solution. Some alternative approaches are
demonstrated at <URL:http://dotnet.mvps.org/dotnet/articles/nullabledates/>
(just ignore the text and check out the code snippets).
Today however I noticed that VB2005 is quite happy with the following:

\\\
Public Sub DoSomething(Optional ByVal SomeDate As Date = Nothing)
[...]
End Sub
///

(I believe this fails to compile in VB2003).

The value that is present in SomeDate if no date is provided by the
calling code is DateTime.MinValue (i.e., the default value for an
uninitialised date variable).
The value of 'DateTime.MinValue' is specified (see documentation).
Is this the intended documented behaviour of declaring the optional
parameter in this way? Can I use this safe in the knowledge that the
behaviour won't break or change in the next release of VB?


Yes, it is.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jun 15 '06 #2

P: n/a
Oenone,

As Herfried suggests I would use a Nullable(Of DateTime) in .NET 2.0 to
represent a "null date" (or one of the other methods in .NET 1.x) rather
then a "special value".

| Public Sub DoSomething(Optional ByVal SomeDate As Date = Nothing)
| [...]
| End Sub
|
| (I believe this fails to compile in VB2003).
It fails to compile in VB 2003, which feels like a bug as this is allowed:

Dim empty As DateTime = Nothing

| Is this the intended documented behaviour of declaring the optional
| parameter in this way?
Maybe ;-)

Rather then use an optional parameter I would use an overloaded method.
Considering that Nullable(Of T) cannot be optional...

Public Sub DoSomething()
DoSomething(Nothing)
End Sub

Public Sub DoSomething(ByVal SomeDate As Nullable(Of Date))

End Sub

| Can I use this safe in the knowledge that the
| behaviour won't break or change in the next release of VB?
I would think you would be safe, As I stated I consider the error in .NET
1.x above to be a bug.

Nothing is defined (has always been defined) as the "default value" for a
type.
http://msdn2.microsoft.com/en-us/0x9tb07z(VS.80).aspx

For reference types this means a null reference. For value types it means
"all zeros" or False in the case of Boolean, For structures all its members
are set to their defaults. In the case of DateTime this happens to be
DateTime.MinValue.

For example try:

Dim empty As DateTime = Nothing
Dim minValue As DateTime = DateTime.MinValue

Debug.Assert(empty = minValue)
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Oenone" <oe****@nowhere.com> wrote in message
news:ub**************@TK2MSFTNGP04.phx.gbl...
| In our applications, we use the special value of DateTime.MinValue to
| represent "null dates" throughout all our code. We recently ran into an
| issue where we wanted an optional date parameter for a procedure. We
weren't
| able to declare it with DateTime.MinValue as its default value, as
MinValue
| is a read-only property rather than a constant. To work around, we had to
| use a "magic date" that we checked for later on. I was never very happy
with
| that solution.
|
| Today however I noticed that VB2005 is quite happy with the following:
|
| \\\
| Public Sub DoSomething(Optional ByVal SomeDate As Date = Nothing)
| [...]
| End Sub
| ///
|
| (I believe this fails to compile in VB2003).
|
| The value that is present in SomeDate if no date is provided by the
calling
| code is DateTime.MinValue (i.e., the default value for an uninitialised
date
| variable).
|
| Is this the intended documented behaviour of declaring the optional
| parameter in this way? Can I use this safe in the knowledge that the
| behaviour won't break or change in the next release of VB?
|
| Thanks,
|
| --
|
| (O)enone
|
|
Jun 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.