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

S

Scott Hembrough

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
 
G

Guest

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.
 
G

Guest

Cor,

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

Chris.
 
S

Scott Hembrough

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?
 
C

Cor Ligthert

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
 
S

Scott Hembrough

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.
 
S

Scott Hembrough

I found this link which indicates that Date is indeed a value type:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vbconvaluereftypes.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).
 
C

Cor Ligthert

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
 
H

Herfried K. Wagner [MVP]

Scott Hembrough said:
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'.
 
C

Cor Ligthert

"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)').


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
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
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...
 
S

Scott Hembrough

Cor Ligthert said:
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.
 
S

Scott Hembrough

Herfried K. Wagner said:
'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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top