PC Review


Reply
Thread Tools Rate Thread

DateTime Declaration

 
 
Bob Day
Guest
Posts: n/a
 
      30th Aug 2003
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


 
Reply With Quote
 
 
 
 
Fergus Cooney
Guest
Posts: n/a
 
      30th Aug 2003
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!!


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      30th Aug 2003
Hello,

"Bob Day" <(E-Mail Removed)> schrieb:
> 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
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet


 
Reply With Quote
 
Fergus Cooney
Guest
Posts: n/a
 
      30th Aug 2003
Hi Herfried,

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

Regards,
Fergus


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      30th Aug 2003
Hello,

"Fergus Cooney" <filter-(E-Mail Removed)> schrieb:
> The pre-compiler gives an error.


If Nothing is assigned in the parameter.

> If I may refer you to my response to the same query ? ...


I know.

Regards,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      30th Aug 2003
Hello,

"Fergus Cooney" <filter-(E-Mail Removed)> schrieb:
> 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
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet


 
Reply With Quote
 
Bob Day
Guest
Posts: n/a
 
      1st Sep 2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      1st Sep 2003
"Bob Day" <(E-Mail Removed)> schrieb
> 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.


--
Armin

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
build Dictionary< DateTime , decimal > from List < DateTime > John A Grandy Microsoft C# .NET 1 5th Feb 2009 03:08 AM
Millisecond values missing when inserting datetime into datetime column of sql Server Manikandan Microsoft C# .NET 4 18th Jul 2007 08:59 PM
How can I save a DateTime from my C# program into a SQL Server (datetime) database column. Steve Kershaw Microsoft ASP .NET 5 29th Aug 2006 03:10 AM
System.DateTime doesn't convert to SqlDbType.DateTime Narshe Microsoft C# .NET 3 2nd Nov 2005 05:42 PM
NASTY Bug in handling of DateTime.MaxValue and DateTime.MinValue =?Utf-8?B?YmlsbF83Nw==?= Microsoft Dot NET Framework 2 31st Mar 2004 06:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:56 AM.