473,806 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dates / Nothing -- Why doesn't this work?

Hello. I have two snippets of code here that are very similar. One works,
but the other doesn't. Can someone explain why?

Snippet 1: Local "date" variable is set to nothing. Compiles fine, sets
date to 1/1/0001 12:00:00 AM.

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim x As Date

x = Nothing

Debug.WriteLine (x)

End Sub

Snippet 2: Optional date parameter contains default value of nothing. .NET
2003 refuses to compile. Gives error of "Conversion from 'System.Object' to
'Date' cannot occur in a constant expression."

Private Sub ThisDoesntWork( Optional ByVal x As Date = Nothing)

Debug.WriteLine (x)

End Sub

Why am I getting this error? It doesn't make sense to me. If x is a
different type (such as Integer), .NET doesn't complain at all. So why is
Date treated differently?

Thanks in advance for any feedback!

Scott
Nov 21 '05 #1
13 1468
Scott,

I don't know about other people but I never use optional anymore. Try
overloading the method.

Private Sub ThisDoesntWork( )
ThisDoesntWork( Nothing)
End Sub
Private Sub ThisDoesntWork( ByVal x As Date)
Debug.WriteLine (x)
End Sub

Hope this helps.
Chris.
"Scott Hembrough" wrote:
Hello. I have two snippets of code here that are very similar. One works,
but the other doesn't. Can someone explain why?

Snippet 1: Local "date" variable is set to nothing. Compiles fine, sets
date to 1/1/0001 12:00:00 AM.

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim x As Date

x = Nothing

Debug.WriteLine (x)

End Sub

Snippet 2: Optional date parameter contains default value of nothing. .NET
2003 refuses to compile. Gives error of "Conversion from 'System.Object' to
'Date' cannot occur in a constant expression."

Private Sub ThisDoesntWork( Optional ByVal x As Date = Nothing)

Debug.WriteLine (x)

End Sub

Why am I getting this error? It doesn't make sense to me. If x is a
different type (such as Integer), .NET doesn't complain at all. So why is
Date treated differently?

Thanks in advance for any feedback!

Scott

Nov 21 '05 #2
Chris,

I assume it is a hypothetical question. As far as I can see has this method
not any sence.
Private Sub ThisDoesntWork( Optional ByVal x As Date = Nothing)
Because when the Date is not set it is Nothing and when it is set it is
Something

Why it gives the error is as well described on this page.

http://msdn.microsoft.com/library/de...tionalargs.asp

Cor

"Chris Podmore" <Ch**********@d iscussions.micr osoft.com>

Scott,

I don't know about other people but I never use optional anymore. Try
overloading the method.

Private Sub ThisDoesntWork( )
ThisDoesntWork( Nothing)
End Sub
Private Sub ThisDoesntWork( ByVal x As Date)
Debug.WriteLine (x)
End Sub

Hope this helps.
Chris.
"Scott Hembrough" wrote:
Hello. I have two snippets of code here that are very similar. One
works,
but the other doesn't. Can someone explain why?

Snippet 1: Local "date" variable is set to nothing. Compiles fine, sets
date to 1/1/0001 12:00:00 AM.

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim x As Date

x = Nothing

Debug.WriteLine (x)

End Sub

Snippet 2: Optional date parameter contains default value of nothing.
.NET
2003 refuses to compile. Gives error of "Conversion from 'System.Object'
to
'Date' cannot occur in a constant expression."

Private Sub ThisDoesntWork( Optional ByVal x As Date = Nothing)

Debug.WriteLine (x)

End Sub

Why am I getting this error? It doesn't make sense to me. If x is a
different type (such as Integer), .NET doesn't complain at all. So why
is
Date treated differently?

Thanks in advance for any feedback!

Scott

Nov 21 '05 #3
Cor,

I assumed that Scott had posted some sample code rather than his actual code
which he was having problems with.

Chris.

"Cor Ligthert" wrote:
Chris,

I assume it is a hypothetical question. As far as I can see has this method
not any sence.
Private Sub ThisDoesntWork( Optional ByVal x As Date = Nothing)


Because when the Date is not set it is Nothing and when it is set it is
Something

Why it gives the error is as well described on this page.

http://msdn.microsoft.com/library/de...tionalargs.asp

Cor

"Chris Podmore" <Ch**********@d iscussions.micr osoft.com>

Scott,

I don't know about other people but I never use optional anymore. Try
overloading the method.

Private Sub ThisDoesntWork( )
ThisDoesntWork( Nothing)
End Sub
Private Sub ThisDoesntWork( ByVal x As Date)
Debug.WriteLine (x)
End Sub

Hope this helps.
Chris.
"Scott Hembrough" wrote:
Hello. I have two snippets of code here that are very similar. One
works,
but the other doesn't. Can someone explain why?

Snippet 1: Local "date" variable is set to nothing. Compiles fine, sets
date to 1/1/0001 12:00:00 AM.

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim x As Date

x = Nothing

Debug.WriteLine (x)

End Sub

Snippet 2: Optional date parameter contains default value of nothing.
.NET
2003 refuses to compile. Gives error of "Conversion from 'System.Object'
to
'Date' cannot occur in a constant expression."

Private Sub ThisDoesntWork( Optional ByVal x As Date = Nothing)

Debug.WriteLine (x)

End Sub

Why am I getting this error? It doesn't make sense to me. If x is a
different type (such as Integer), .NET doesn't complain at all. So why
is
Date treated differently?

Thanks in advance for any feedback!

Scott


Nov 21 '05 #4
Yes, that is correct. It's just sample code. I was posting a very simple
example that didn't have any extra code that would detract from the actual
question.

As for ways to deal with this, I realize function overloading would be a
better solution (as suggested in another post). I just happened to run
across it as I was converting some old VB6 code. We have a very large
project, and right now we're trying to get it to run in .NET with minimal
changes. Rearchitecting the code will come later.

Our original function had an optional date parameter declared as a variant.
Within the function, we used IsMissing to determine if anything had been
passed in. Since IsMissing is no longer an option, I was defaulting the
parameter to nothing and checking for IsNothing within the function. This
would get us by until we can go back and take advantage of features such as
function overloading (which would make this a moot point anyway).

So, back to the original question: Does anyone know why this is happening?
Is this a bug? Or is there a more obvious explanation that I'm just
overlooking?

"Chris Podmore" <Ch**********@d iscussions.micr osoft.com> wrote in message
news:32******** *************** ***********@mic rosoft.com...
Cor,

I assumed that Scott had posted some sample code rather than his actual code which he was having problems with.

Chris.

Nov 21 '05 #5
Scott,

Did you see this text on the link I showed?

If the optional argument is a reference type such as a String, you can use
Nothing as the default value, provided this is not an expected value for the
argument.

Cor

Nov 21 '05 #6
OK. I'd overlooked one thing that Cor wrote when he responded to me. The
link that he provided said that "If the optional argument is a reference
type such as a String, you can use Nothing as the default value, provided
this is not an expected value for the argument."

Is date considered to be a value type or a reference type? I didn't see it
in the list of value types I was looking at in the help file. So, if it's
reference, then it seems like the MS article is saying that I *should* be
able to set this to nothing.

Also, integer *is* a value type and I can set an optional integer parameter
to nothing with no problems. So it seems like I should be able to do this
with a date.

Lots of questions, but I'm just trying to understand how all this works as
we delve into .NET. Again, thanks for all the help.

"Scott Hembrough" <sh********@nof reakinspam.gcct v.com> wrote in message
news:uw******** ******@TK2MSFTN GP09.phx.gbl...
Yes, that is correct. It's just sample code. I was posting a very simple
example that didn't have any extra code that would detract from the actual
question.

As for ways to deal with this, I realize function overloading would be a
better solution (as suggested in another post). I just happened to run
across it as I was converting some old VB6 code. We have a very large
project, and right now we're trying to get it to run in .NET with minimal
changes. Rearchitecting the code will come later.

Our original function had an optional date parameter declared as a variant. Within the function, we used IsMissing to determine if anything had been
passed in. Since IsMissing is no longer an option, I was defaulting the
parameter to nothing and checking for IsNothing within the function. This
would get us by until we can go back and take advantage of features such as function overloading (which would make this a moot point anyway).

So, back to the original question: Does anyone know why this is happening? Is this a bug? Or is there a more obvious explanation that I'm just
overlooking?

"Chris Podmore" <Ch**********@d iscussions.micr osoft.com> wrote in message
news:32******** *************** ***********@mic rosoft.com...
Cor,

I assumed that Scott had posted some sample code rather than his actual

code
which he was having problems with.

Chris.


Nov 21 '05 #7
I found this link which indicates that Date is indeed a value type:
http://msdn.microsoft.com/library/de...uereftypes.asp

So, as Cor pointed out earlier, if you have a reference type you can set it
to nothing without any problems in an optional parameter. Since Date is a
value type, I guess this is why I'm having an issue. However, if that's the
case, then why is this allowed?

Private Sub Test(Optional ByVal x As Integer = Nothing)

Integer is also a value type, so I could think it should be throwing an
error as well (assuming behavior should be consistent across all value
types).

"Scott Hembrough" <sh********@nof reakinspam.gcct v.com> wrote in message
news:%2******** ************@TK 2MSFTNGP12.phx. gbl...
OK. I'd overlooked one thing that Cor wrote when he responded to me. The
link that he provided said that "If the optional argument is a reference
type such as a String, you can use Nothing as the default value, provided
this is not an expected value for the argument."

Is date considered to be a value type or a reference type? I didn't see it in the list of value types I was looking at in the help file. So, if it's
reference, then it seems like the MS article is saying that I *should* be
able to set this to nothing.

Also, integer *is* a value type and I can set an optional integer parameter to nothing with no problems. So it seems like I should be able to do this
with a date.

Lots of questions, but I'm just trying to understand how all this works as
we delve into .NET. Again, thanks for all the help.

"Scott Hembrough" <sh********@nof reakinspam.gcct v.com> wrote in message
news:uw******** ******@TK2MSFTN GP09.phx.gbl...
Yes, that is correct. It's just sample code. I was posting a very simple example that didn't have any extra code that would detract from the actual question.

As for ways to deal with this, I realize function overloading would be a
better solution (as suggested in another post). I just happened to run
across it as I was converting some old VB6 code. We have a very large
project, and right now we're trying to get it to run in .NET with minimal changes. Rearchitecting the code will come later.

Our original function had an optional date parameter declared as a

variant.
Within the function, we used IsMissing to determine if anything had been
passed in. Since IsMissing is no longer an option, I was defaulting the
parameter to nothing and checking for IsNothing within the function. This would get us by until we can go back and take advantage of features such

as
function overloading (which would make this a moot point anyway).

So, back to the original question: Does anyone know why this is

happening?
Is this a bug? Or is there a more obvious explanation that I'm just
overlooking?

"Chris Podmore" <Ch**********@d iscussions.micr osoft.com> wrote in message news:32******** *************** ***********@mic rosoft.com...
Cor,

I assumed that Scott had posted some sample code rather than his
actual code
which he was having problems with.

Chris.




Nov 21 '05 #8
Scott,

I think that it is a flaw (usable bug) in the implementation. I saw that
other values do it as well.

However much need is not for it as I said before

Test(nothing) gives the same result withouth optional, and is in my opinion
nicer code.

I never use such a possible flaw because you will not be awared in future if
this is will be changed.

I have seen bad samples where people did that and because of that the
applications where not any more upgradable to newer compiller versions,
without almost completly writting them new.

Just my thought,

Cor

"Scott Hembrough" <sh********@nof reakinspam.gcct v.com>
I found this link which indicates that Date is indeed a value type:
http://msdn.microsoft.com/library/de...uereftypes.asp

So, as Cor pointed out earlier, if you have a reference type you can set
it
to nothing without any problems in an optional parameter. Since Date is a
value type, I guess this is why I'm having an issue. However, if that's
the
case, then why is this allowed?

Private Sub Test(Optional ByVal x As Integer = Nothing)

Integer is also a value type, so I could think it should be throwing an
error as well (assuming behavior should be consistent across all value
types).

"Scott Hembrough" <sh********@nof reakinspam.gcct v.com> wrote in message
news:%2******** ************@TK 2MSFTNGP12.phx. gbl...
OK. I'd overlooked one thing that Cor wrote when he responded to me.
The
link that he provided said that "If the optional argument is a reference
type such as a String, you can use Nothing as the default value, provided
this is not an expected value for the argument."

Is date considered to be a value type or a reference type? I didn't see

it
in the list of value types I was looking at in the help file. So, if
it's
reference, then it seems like the MS article is saying that I *should* be
able to set this to nothing.

Also, integer *is* a value type and I can set an optional integer

parameter
to nothing with no problems. So it seems like I should be able to do
this
with a date.

Lots of questions, but I'm just trying to understand how all this works
as
we delve into .NET. Again, thanks for all the help.

"Scott Hembrough" <sh********@nof reakinspam.gcct v.com> wrote in message
news:uw******** ******@TK2MSFTN GP09.phx.gbl...
> Yes, that is correct. It's just sample code. I was posting a very simple > example that didn't have any extra code that would detract from the actual > question.
>
> As for ways to deal with this, I realize function overloading would be
> a
> better solution (as suggested in another post). I just happened to run
> across it as I was converting some old VB6 code. We have a very large
> project, and right now we're trying to get it to run in .NET with minimal > changes. Rearchitecting the code will come later.
>
> Our original function had an optional date parameter declared as a

variant.
> Within the function, we used IsMissing to determine if anything had
> been
> passed in. Since IsMissing is no longer an option, I was defaulting
> the
> parameter to nothing and checking for IsNothing within the function. This > would get us by until we can go back and take advantage of features
> such

as
> function overloading (which would make this a moot point anyway).
>
> So, back to the original question: Does anyone know why this is

happening?
> Is this a bug? Or is there a more obvious explanation that I'm just
> overlooking?
>
> "Chris Podmore" <Ch**********@d iscussions.micr osoft.com> wrote in message > news:32******** *************** ***********@mic rosoft.com...
> > Cor,
> >
> > I assumed that Scott had posted some sample code rather than his actual > code
> > which he was having problems with.
> >
> > Chris.
>
>



Nov 21 '05 #9
"Scott Hembrough" <sh********@nof reakinspam.gcct v.com> schrieb:
Snippet 1: Local "date" variable is set to nothing. Compiles fine, sets
date to 1/1/0001 12:00:00 AM.
'DateTime' (= 'Date') is a value type. By setting a value type to
'Nothing', its value will be set to the type's default value. For numeric
data types, that's 0, for dates it's the date you mention above.

If you need to deal with real nullable dates, you can use
'System.Data.Sq lTypes' instead of 'System.DateTim e'. In .NET 2.0 (2005)
there will be a 'System.Nullabl e' generic type that can be used to make
other types nullable ('Nullable(Of Date)').
Dim x As Date

x = Nothing


The line above can be removed, because 'x' is already initialized with
'Nothing'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #10

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

Similar topics

10
3299
by: Colin Steadman | last post by:
I'm a stupid ASP programmer and I dont do Javascript (except for very simple tasks anyway), and I'm in a bit of a predicament. I've used a javascript table sorting script from here: http://www.ipwebdesign.net/kaelisSpace/useful_tableSort.html This works great except it doesn't sort my UK formatted dates properly, and I end up with something like this: Birth Date (dd/mm/yyyy)
7
1909
by: Fendi Baba | last post by:
The function is called from opencalendar(targetfield). Thanks for any hints on what could be the problem. .............................................................. var decimalPointDelimiter = ".";
22
4180
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b) { return -1; } else
52
4603
by: Andy Dingley | last post by:
I'm using this at present: <p title="Publication date" ></p> Works fine on screen, but Fangs/Jaws just reads it as "left bracket twenty-eight slash zero slash two thousand five fifteen colon zero right bracket" Really it needs something more to indicate that it _is_ a date. The brackets would be better done with CSS, :before and content: , but the boss wants it to work under IE too. The site is firmly UK
2
4516
by: Yog | last post by:
Is there a best way to handle various formats of dates for SQL server?. The data comes in various input files with different date formats. The ParseExact function looks like an "evil" and it doesn't seem to work for some cases. 'Date Time Stuff Dim dtarray As String() = _
18
38252
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
5
2639
by: AAJ | last post by:
Hi Does anyone know of any good publically available set of standards for managing dates when dealing with a database server (in my case SQL Server 2000 and c# VS2005). At the moment, if I create a record in the database for say a job, the invoicedOn date field will be left null within the data base, not being assigned untill its actually invoiced.
7
2422
by: evilcowstare via AccessMonster.com | last post by:
Hi, I have searched the forum for answers on this and to be honest as a novice I find it a bit confusing so apologies if it is simple. There are some searches that I want to apply to my database. 1. To search for all records between 2 dates and display them in a report 2. To be able to show all records which have a selection against them made from a combo box 3. To be able to combine the two, selecting the option from a combo and then...
9
21388
by: clintonb | last post by:
I'm looking for a way to calculate the number of days between two dates using standard C++ functions. Would it be as simple as just using the difftime() function and then dividing that result by the number of seconds in a day? - Clint
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10623
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10373
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5683
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3852
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.