End Sub End Sub 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, setsdate to 1/1/0001 12:00:00 AM.Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.ClickDim x As Datex = NothingDebug.WriteLine(x)End SubSnippet 2: Optional date parameter contains default value of nothing. .NET2003 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 SubWhy am I getting this error? It doesn''t make sense to me. If x is adifferent type (such as Integer), .NET doesn''t complain at all. So why isDate treated differently?Thanks in advance for any feedback!Scott 解决方案 Scott,I don''t know about other people but I never use optional anymore. Tryoverloading the method.Private Sub ThisDoesntWork()ThisDoesntWork(Nothing)End SubPrivate Sub ThisDoesntWork(ByVal x As Date)Debug.WriteLine(x)End SubHope 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 methodnot 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 isSomethingWhy it gives the error is as well described on this page. http://msdn.microsoft.com/library/de...tionalargs.aspCor"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 codewhich 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 这篇关于日期/没有 - 为什么这不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!