473,378 Members | 1,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

DateTime Declaration

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
Nov 20 '05 #1
14 18045
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!!
Nov 20 '05 #2
Hello,

"Bob Day" <Bo****@TouchTalk.net> 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
Nov 20 '05 #3
Hi Herfried,

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

Regards,
Fergus
Nov 20 '05 #4
Hello,

"Fergus Cooney" <fi******@tesco.net> 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
Nov 20 '05 #5
Hello,

"Fergus Cooney" <fi******@tesco.net> 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
Nov 20 '05 #6
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" <Bo****@TouchTalk.net> wrote in message
news:OC**************@TK2MSFTNGP11.phx.gbl...
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

Nov 20 '05 #7
"Bob Day" <Bo****@TouchTalk.net> 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

Nov 20 '05 #8
It should not. As a developer, I cannot question when and where the tooltip
is giving me corrent results and when it is not. That is a bug, period.

Bob Day

"Armin Zingler" <az*******@freenet.de> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...
"Bob Day" <Bo****@TouchTalk.net> 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

Nov 20 '05 #9
"Bob Day" <Bo****@TouchTalk.net> schrieb
It should not. As a developer, I cannot question when and where the
tooltip is giving me corrent results and when it is not. That is a
bug, period.


Yes, Sir!

The tooltip does give the right result.
#12:00:00 AM# is the format that is used in the IDE for datetime literals.
You can write, for example:

dim d as datetime
d = #12:00:00 AM#

The format is fixed because the code should be compliable in different
countries without changing the code (e.g. in Germany, we don't have "AM").
As the tooltip is shown by the IDE it also uses this format.
Then, we also have the "human readable" format depending on the
language/culture of the system (or the current thread). This format is used
because applicatoins should be "user friendly". _You_ are determining the
format used for datetime->string conversions. If you don't use the format
string to get the result you need, you don't have to wonder why the formats
are different.
--
Armin

Nov 20 '05 #10
"Armin Zingler" <az*******@freenet.de> schrieb
Yes, Sir!
I forgot the -> ;-) <-
The format is fixed because the code should be compliable in


....should be compilable...

In other words: The tooltip is for programmers, the result of the other
conversion for end users. => different formats.
--
Armin

Nov 20 '05 #11
Cor
Armin,

Yes, Sir!

That sounds better in the German language, can you speak that?
:-)
Cor
Nov 20 '05 #12
"Cor" <no*@non.com> schrieb
Armin,

Yes, Sir!

That sounds better in the German language, can you speak that?
:-)
Cor


?? what do you mean?

;-)

--
Armin

Nov 20 '05 #13
Hello,

"Cor" <no*@non.com> schrieb:
Yes, Sir!

That sounds better in the German language, can you speak
that?


What do you expect to hear?

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #14
Cor
Armin,
When "Jawoll Mijnherr" oder etwas is written I see in mind somebody clicking
his shoes together standing straight,
In the English language I see "yes sir" just as an polite acknowledge.
But maybe that is only in my mind.
:-)))
Cor
Nov 20 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Phil Powell | last post by:
I have a mySQL database table with a column field datatype of datetime that I set up in the original create table statement like this: create table nnet_usermetadata ( .... nnet_record_entered...
2
by: Pet Matrix. | last post by:
Hi, I am having the schema and one element in that schema is , <xs:element name="EndT" type="TstampType" minOccurs="1" maxOccurs="1"/> How do I represent the NULL value for the datatype dateTime...
3
by: ad | last post by:
I have a DataView, say myDV, it contain a DataTime field, "Birth". I want to get the Birth Value from the DataView: DateTiem dBirth= (DateTime)myDV; But the complier throw an error. How can...
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
9
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
0
by: vijayalakshmi.venkataraman | last post by:
Hello, I have a .NET client that accesses a Java webservice. Let me first say that the client stub I got generated from the wsdl was incomplete and had to make changes to it manually to make it...
1
by: huks | last post by:
Hi, I have a PL1 program to load a DB2 table with datetime zone (null allowed) Usually when i have to load null values, I used nullindicator with -1 value but here, with a datetime variable i...
10
by: Jeff | last post by:
Hey ..NET 2.0 I'm about to create 2 input parameters for a method. These are 2 DateTime parameters - one named "from" and another named "to"... - from and to DateTime values... Okay the...
1
by: chaitanya02 | last post by:
Hello All, Can anyone help me in passing a Datetime parameter to a webmethod, please? *declaration datetime datetime1; webmethod(out string datetime1); is it the right way of declaring a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.