By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 775 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a

"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

P: n/a
"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

P: n/a
"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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.