DateTime Declaration

  • Thread starter Thread starter Bob Day
  • Start date Start date
B

Bob Day

using VS 2003, VB.net...

Why is this valid:
dim x as datetime = nothing

but this is invalid:

public sub DoSomething (Optional byval x as datetime = nothing)
....code to do something
end sub

Nothing shows are error message that you cannot set a system object to
nothing.

Is there a work around for the sub problem? I must set it equal to
something since it is optional.

Thanks!
Bob Day
 
Hi Bob,

Just to expand on your statement of the problem (which I consider
to be a bug*).

There is a problem setting an optional DateTime parameter to its
default value as illustrated with the following:

Sub Foo (ByRef Optional dtParam As DateTime = Nothing) 'Error
Dim dtLocal As DateTime = Nothing 'This is fine.
End Sub

The pre-compiler reports an error by underlining the Nothin in the
parameter list. The error given is:
Conversion from 'System.Object' to 'Date' cannot occur in a
constant expression.

The Visual Basic Language Reference says a lot about Nothing: :-)
The Nothing keyword represents the default value of any data
type. Assigning Nothing to a variable sets it to the default value for
its declared type. If that type contains variable members, they are
all set to their default values.

===============================
The workaround is to use a set date. The earliest DateTime is
12:00:00 midnight, January 1, 0001 C.E. (Common Era). Given that
you've found a bug, you could use #1/4/2003#.

Regards,
Fergus

=========================
*You wait months for a bug and nothing and all of a sudden - two in
one day!!
 
Hello,

Bob Day said:
using VS 2003, VB.net...

Why is this valid:
dim x as datetime = nothing

but this is invalid:

public sub DoSomething (Optional byval x as datetime = nothing)
....code to do something
end sub

Nothing shows are error message that you cannot set a system object to
nothing.

DateTime is a value type, so assigning Nothing doesn't make sense. The
default value will be assigned.

\\\
Dim d As Date = Nothing
MsgBox(IsNothing(d)) ' Returns False.
MsgBox(d.ToLongDateString())
///

HTH,
Herfried K. Wagner
 
Hi Herfried,

The pre-compiler gives an error.
If I may refer you to my response to the same query ? ...

Regards,
Fergus
 
Hello,

Fergus Cooney said:
Just to expand on your statement of the problem (which I consider
to be a bug*).

There is a problem setting an optional DateTime parameter to its
default value as illustrated with the following:

Sub Foo (ByRef Optional dtParam As DateTime = Nothing) 'Error
Dim dtLocal As DateTime = Nothing 'This is fine.
End Sub

The pre-compiler reports an error by underlining the Nothin in the
parameter list. The error given is:
Conversion from 'System.Object' to 'Date' cannot occur in a
constant expression.

The Visual Basic Language Reference says a lot about Nothing: :-)
The Nothing keyword represents the default value of any data
type. Assigning Nothing to a variable sets it to the default value for
its declared type. If that type contains variable members, they are
all set to their default values.

Have a look at the code below:

\\\
Const d As DateTime = Nothing
///

This code will cause the same error as assigning Nothing as the default
value of an optional parameter. I think the reason is that the compiler
won't do the conversion for you when compiling the application.
*You wait months for a bug and nothing and all of a sudden - two in
one day!!

I am not sure if this is a bug.

HTH,
Herfried K. Wagner
 
I have read all your replies, and just to make things less clear, consider

Dim x As DateTime = DateTime.MinValue
Dim z As String = x.ToString

These should produce the same value, they do not. Hold your cursor over X
and you get only the time (and Locals in Debug shows the same). Hold your
cursor over X and you get the full date and time (and Locals window in debug
shows the same).

Why?

Bob Day
 
Bob Day said:
I have read all your replies, and just to make things less clear,
consider

Dim x As DateTime = DateTime.MinValue
Dim z As String = x.ToString

These should produce the same value, they do not. Hold your cursor
over X and you get only the time (and Locals in Debug shows the
same). Hold your cursor over X and you get the full date and time
(and Locals window in debug
shows the same).

Why?

Because the tooltip in the IDE performs a different date-to-string
conversion from that you do.
 
Back
Top