vb.net 具有现在(日期)默认值的可选参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23107103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:24:15  来源:igfitidea点击:

Optional Parameter with now (Date) default value

.netvb.net

提问by bgs

How to assign the default Date Time ( Now ) value for optional parameter.

如何为可选参数分配默认日期时间 ( Now ) 值。

When assigning the now object to optional parameter below error is raised

将 now 对象分配给以下错误的可选参数时

Constant expression is required

Code:

代码:

Public Sub ReminderMail(Optional ByVal ReminderMailDate As DateTime = Now)

// Code Block

End Sub 

回答by

Try this :

尝试这个 :

Public Sub ReminderMail(Optional ByVal ReminderMailDate As DateTime = Nothing)

If ReminderMailDate = Nothing Then ReminderMailDate = Now

// Code Block

End Sub 

回答by Damien_The_Unbeliever

Instead of using an optional parameter, you might want to consider offering two overloads of your Sub:

您可能需要考虑提供两个重载,而不是使用可选参数Sub

Public Sub ReminderMail()
    ReminderMail(DateTime.Now)
End Sub

Public Sub ReminderMail(ByVal ReminderMailDate As DateTime)

// Code Block

End Sub 

Which from a caller's perspective, operates quite similarly.

从调用者的角度来看,其操作非常相似。