PC Review


Reply
Thread Tools Rate Thread

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

 
 
Scott Hembrough
Guest
Posts: n/a
 
      19th Nov 2004
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


 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q2hyaXMgUG9kbW9yZQ==?=
Guest
Posts: n/a
 
      19th Nov 2004
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
>
>
>

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      19th Nov 2004
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" <(E-Mail Removed)>


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



 
Reply With Quote
 
=?Utf-8?B?Q2hyaXMgUG9kbW9yZQ==?=
Guest
Posts: n/a
 
      19th Nov 2004
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" <(E-Mail Removed)>
>
>
> > 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
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
Scott Hembrough
Guest
Posts: n/a
 
      19th Nov 2004
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" <(E-Mail Removed)> wrote in message
news:3257E15C-6CE8-48D0-9303-(E-Mail Removed)...
> Cor,
>
> I assumed that Scott had posted some sample code rather than his actual

code
> which he was having problems with.
>
> Chris.



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      19th Nov 2004
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





 
Reply With Quote
 
Scott Hembrough
Guest
Posts: n/a
 
      19th Nov 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:3257E15C-6CE8-48D0-9303-(E-Mail Removed)...
> > Cor,
> >
> > I assumed that Scott had posted some sample code rather than his actual

> code
> > which he was having problems with.
> >
> > Chris.

>
>



 
Reply With Quote
 
Scott Hembrough
Guest
Posts: n/a
 
      19th Nov 2004
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" <(E-Mail Removed)> wrote in message
news:%236%23HF%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in

message
> > news:3257E15C-6CE8-48D0-9303-(E-Mail Removed)...
> > > Cor,
> > >
> > > I assumed that Scott had posted some sample code rather than his

actual
> > code
> > > which he was having problems with.
> > >
> > > Chris.

> >
> >

>
>




 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      20th Nov 2004
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" <(E-Mail Removed)>

>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" <(E-Mail Removed)> wrote in message
> news:%236%23HF%(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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" <(E-Mail Removed)> wrote in

> message
>> > news:3257E15C-6CE8-48D0-9303-(E-Mail Removed)...
>> > > Cor,
>> > >
>> > > I assumed that Scott had posted some sample code rather than his

> actual
>> > code
>> > > which he was having problems with.
>> > >
>> > > Chris.
>> >
>> >

>>
>>

>
>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      20th Nov 2004
"Scott Hembrough" <(E-Mail Removed)> 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/>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Querying between two dates doesn't work if Start and End are ident Richard Horne Microsoft Access Queries 10 23rd Nov 2009 05:17 PM
Defrag doesn't work because chkdsk doesn't work =?Utf-8?B?bWVnYW4=?= Windows XP Performance 2 3rd Jun 2007 03:21 PM
macro doesn't properly record AutoSum (and SendKeys doesn't work) =?Utf-8?B?Y3JpbXNvbmtuZw==?= Microsoft Excel Programming 2 21st Nov 2006 02:11 PM
Changing to eurodate format doesn't work on dates when merging ex. =?Utf-8?B?SmFuZXQgU2ltcHNvbg==?= Microsoft Excel Misc 0 27th Jan 2005 05:41 PM
data pertaining to dates, but date/time data type doesn't work Kendra Microsoft Access 2 6th Feb 2004 10:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:53 AM.