473,385 Members | 1,356 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,385 software developers and data experts.

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(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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 1436
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(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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**********@discussions.microsoft.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(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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**********@discussions.microsoft.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(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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**********@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.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********@nofreakinspam.gcctv.com> wrote in message
news:uw**************@TK2MSFTNGP09.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**********@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.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********@nofreakinspam.gcctv.com> wrote in message
news:%2********************@TK2MSFTNGP12.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********@nofreakinspam.gcctv.com> wrote in message
news:uw**************@TK2MSFTNGP09.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**********@discussions.microsoft.com> wrote in message news:32**********************************@microsof t.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********@nofreakinspam.gcctv.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********@nofreakinspam.gcctv.com> wrote in message
news:%2********************@TK2MSFTNGP12.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********@nofreakinspam.gcctv.com> wrote in message
news:uw**************@TK2MSFTNGP09.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**********@discussions.microsoft.com> wrote in message > news:32**********************************@microsof t.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********@nofreakinspam.gcctv.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.SqlTypes' instead of 'System.DateTime'. In .NET 2.0 (2005)
there will be a 'System.Nullable' 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

"Herfried K. Wagner [MVP]"
'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.SqlTypes' instead of 'System.DateTime'. In .NET 2.0 (2005)
there will be a 'System.Nullable' 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'.

You have in my opinion not understood the question from the OP and the
answers from the other posters and the OP himself in this thread, do you
have any things you want to know about this when you read the thread (which
is already a day old), be free to do that?

Cor
Nov 21 '05 #11
"Cor Ligthert" <no************@planet.nl> schrieb:
You have in my opinion not understood the question from
the OP and the answers from the other posters and the OP
himself in this thread


It seems that you didn't understand the purpose of my reply...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #12
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I think that it is a flaw (usable bug) in the implementation. I saw that
other values do it as well.
That's ultimately what I was getting at, but since I'm rather new to the
..NET environment I didn't feel absolutely confident in stating this. I
think through our discussions, though, it seems like a logical conclusion.
However much need is not for it as I said before

I agree.
Nov 21 '05 #13
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ug**************@tk2msftngp13.phx.gbl...
'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.
Yes, I was aware of this. Actually, that's what I was expecting.
If you need to deal with real nullable dates, you can use
'System.Data.SqlTypes' instead of 'System.DateTime'. In .NET 2.0 (2005)
there will be a 'System.Nullable' generic type that can be used to make
other types nullable ('Nullable(Of Date)').


I was just getting ready to dive into the SqlTypes. Also, I did not know
about the System.Nullable generic type in 2005. That's very cool!

Thanks for the info, Herfried!
Nov 21 '05 #14

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

Similar topics

10
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: ...
7
by: Fendi Baba | last post by:
The function is called from opencalendar(targetfield). Thanks for any hints on what could be the problem. .............................................................. var...
22
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)...
52
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...
2
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...
18
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
5
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...
7
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....
9
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...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.