日期/没有 - 为什么这不起作用? [英] Dates / Nothing -- Why doesn't this work?

查看:59
本文介绍了日期/没有 - 为什么这不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我在这里有两个非常相似的代码片段。一个工作,

但另一个不行。有人可以解释原因吗?


片段1:本地日期变量设置为空。编译好,设置

日期到1/1/0001 12:00:00 AM。


Private Sub Button1_Click(ByVal sender As System.Object,ByVal e as

System.EventArgs)处理Button1.Click


Dim x As Date


x = Nothing


Debug.WriteLine(x)


结束子


片段2:可选日期参数包含默认值没有价值。 .NET

2003拒绝编译。给出从'System.Object''转换为

'的错误'''''不能出现在常量表达式中。


私有Sub ThisDoesntWork(可选ByVal x As Date = Nothing)


Debug.WriteLine(x)


End Sub

为什么我收到此错误?这对我来说没有意义。如果x是一个不同类型的

(例如Integer),.NET根本不会抱怨。那么为什么

日期处理方式不同?


提前感谢您的任何反馈!


Scott

解决方案

Scott,


我不知道其他人,但我再也不会使用可选项了。尝试

重载方法。


Private Sub ThisDoesntWork()

ThisDoesntWork(Nothing)

End Sub

Private Sub ThisDoesntWork(ByVal x As Date)

Debug.WriteLine(x)

End Sub

希望这会有所帮助。

Chris。

" Scott Hembrough"写道:

你好。我在这里有两个非常相似的代码片段。一个工作,
但另一个不起作用。有人可以解释原因吗?

片段1:本地日期变量设置为空。编译好,将日期设置为1/1/0001 12:00:00 AM。

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System。 EventArgs)处理Button1.Click

Dim x As Date

x = Nothing

Debug.WriteLine(x)
End Sub

Snippet 2:可选日期参数包含默认值no。 .NET
2003拒绝编译。给出从'System.Object''转换为
''''的错误不能出现在常量表达式中。

Private Sub ThisDoesntWork(可选ByVal x As Date) =没什么)

Debug.WriteLine(x)

结束子

为什么我会收到此错误?这对我来说没有意义。如果x是一个不同类型(如Integer),.NET根本就不会抱怨。那么为什么
日期的处理方式不同?

提前感谢您的任何反馈!

Scott



Chris,


我认为这是一个假设的问题。据我所知有这种方法

没有任何意义。

Private Sub ThisDoesntWork(可选ByVal x As Date = Nothing)


因为当没有设置日期时它是Nothing,当它被设置时它是

东西


为什么它给出了错误在这个页面上也有描述。

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


Cor


" Chris Podmore" < Ch ********** @ discussion.microsoft.com>


Scott,

我不知道其他人们,但我再也不会使用可选项了。尝试
重载方法。

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

希望这会有所帮助。
Chris。

" Scott Hembrough"写道:

你好。我在这里有两个非常相似的代码片段。一个
有效,
但另一个没有。有人可以解释原因吗?

片段1:本地日期变量设置为空。编译好,将日期设置为1/1/0001 12:00:00 AM。

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System。 EventArgs)处理Button1.Click

Dim x As Date

x = Nothing

Debug.WriteLine(x)
End Sub

Snippet 2:可选的日期参数包含默认值。
.NET
2003拒绝编译。给出从'System.Object'转换为'
'''''''''''''''''''''''''''''''''''''''''''''''''' ByVal x As Date = Nothing)

Debug.WriteLine(x)

End Sub

为什么我收到此错误?这对我来说没有意义。如果x是一个不同类型(如Integer),.NET根本就不会抱怨。那么为什么

日期处理方式不同?

提前感谢您的任何反馈!

Scott



Cor,


我认为Scott发布了一些示例代码而不是他的实际代码

他遇到了问题。


克里斯。


Cor Ligthert写道:

克里斯,

我认为这是一个假设的问题。据我所知,这种方法没有任何意义。

Private Sub ThisDoesntWork(可选ByVal x As Date = Nothing)



因为当没有设置日期时它是Nothing,当它被设置时它是



为什么它给出错误在本页面上有描述。

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

Chris Podmore < Ch ********** @ discussion.microsoft.com>

Scott,

我不是''知道其他人但我从不再使用可选项。尝试
重载方法。

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

希望这会有所帮助。
Chris。

" Scott Hembrough"写道:

你好。我在这里有两个非常相似的代码片段。一个
有效,
但另一个没有。有人可以解释原因吗?

片段1:本地日期变量设置为空。编译好,将日期设置为1/1/0001 12:00:00 AM。

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System。 EventArgs)处理Button1.Click

Dim x As Date

x = Nothing

Debug.WriteLine(x)
End Sub

Snippet 2:可选的日期参数包含默认值。
.NET
2003拒绝编译。给出从'System.Object'转换为'
'''''''''''''''''''''''''''''''''''''''''''''''''' ByVal x As Date = Nothing)

Debug.WriteLine(x)

End Sub

为什么我收到此错误?这对我来说没有意义。如果x是一个不同类型(如Integer),.NET根本就不会抱怨。那么为什么

日期处理方式不同?

提前感谢您的任何反馈!

Scott




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

解决方案

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



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



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




这篇关于日期/没有 - 为什么这不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆