简体   繁体   English

如何在子例程中将datetime的system.nullable设置为可选参数?

[英]How can i set system.nullable of datetime as an optional parameter in a subroutine?

I tried the following, but i get the error 我尝试了以下操作,但出现错误

constant expression is required 需要常量表达式

Public Sub ExampleSub(ByVal Test as string, 
  Optional ByVal fromDate As System.Nullable(Of DateTime) = Date.Today)
'A Great sub!
End sub

and here is the C# 这是C#

public void ExampleSub(string Test, 
  System.Nullable<DateTime> fromDate = System.DateTime.Today)
{
    //A Great sub!
}

Thanks in advance 提前致谢

You can't, the compiler tells you why :) 您不能,编译器会告诉您原因:)

in C#: 在C#中:

public void ExampleSub(string Test)
{
    //A Great overload!
    ExampleSub(Test, System.DateTime.Now);
}

public void ExampleSub(string Test, System.Nullable<DateTime> fromDate)
{
    //A Great sub!
}

Now, IFF you know that null will not be legitimately by passed in by a caller, you could do: 现在,IFF您知道调用者不会合法传递null ,您可以这样做:

public void ExampleSub(string Test, System.Nullable<DateTime> fromDate = null)
{
    fromDate = fromDate?? System.DateTime.Now;
    //An Even Greater sub!
}

You can't use a non-constant expression for the default parameter. 您不能对默认参数使用非常数表达式。 System.DateTime.Today will depend on when you run the program so it is not constant. System.DateTime.Today将取决于您运行程序的时间,因此它不是恒定的。

Use a constant expression for default and then check for that and set fromDate to System.DateTime.Now in the routine. 将常量表达式用作默认值,然后进行检查,然后在例程中将fromDate设置为System.DateTime.Now Normally null would be used as the special value as in @sehes answer. 通常,像@sehes答案一样,将null用作特殊值。 If null has another special meaning to your code you could use a value which will never be used as the defaul parameter, eg System.DateTime.MinValue : 如果null对您的代码有另一个特殊含义,则可以使用一个永远不会用作默认参数的值,例如System.DateTime.MinValue

public void ExampleSub(string Test, 
  System.Nullable<DateTime> fromDate = DateTime.MinValue)
{
    fromDate = fromDate == DateTime.MinValue ? System.DateTime.Now : fromDate;
    //A Great sub!
}

If someone is doing this in VB.Net this is a way I solved my issue, I didn't find the exact thing on this thread, if it helps someone: 如果有人在VB.Net中这样做,这是我解决问题的一种方式,但在此线程上我没有找到确切的方法,如果它可以帮助某人:

  /*I set below line as parameter in method*/
  Optional ByVal SlotDate As DateTime = Nothing

  If Not SlotDate = Nothing Then
       /* code to execute when date passed */
  Else
       /* code to execute when there is no date passed */
  End If

VB VB

Public Sub ExampleSub(Test As String, _
                      Optional fromDate As System.Nullable(Of DateTime) = Nothing)
    'A Great sub!
    If fromDate Is Nothing Then
        'code here for no fromDate
        'i.e. Now
        fromDate = DateTime.Now
    End If
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “没有为类型‘System.Nullable`1[System.DateTime]’(参数‘property’)定义属性‘System.DateTime Date’”异常 - “Property 'System.DateTime Date' is not defined for type 'System.Nullable`1[System.DateTime]' (Parameter 'property')” exception 使用Guid作为可选参数给出“参数需要类型为&#39;System.Nullable&#39;的值” - Using Guid as an optional parameter gives 'parameter requires a value of type 'System.Nullable' 序列化复杂类型System.Nullable <System.DateTime> - Serialize Complex Type System.Nullable<System.DateTime> 如何创建可选的DateTime参数? - How can I create an optional DateTime parameter? 错误System.Nullable&#39;1自动映射器 - Error System.Nullable'1 automapper System.Nullable <> struct的“Covariance” - “Covariance” of the System.Nullable<> struct 如何正确处理 System.Nullable<t> LinqToSql 类的字段?</t> - How to correctly deal with System.Nullable<T> fields of LinqToSql classes? &#39;System.String&#39; 和 &#39;System.Nullable`1[System.DateTime]&#39; 类型之间没有定义强制运算符 - No coercion operator is defined between types 'System.String' and 'System.Nullable`1[System.DateTime]' ExecuteMethodCall:类型&#39;System.Nullable`1 [System.DateTime]&#39;必须声明一个默认(无参数)构造函数 - ExecuteMethodCall: The type 'System.Nullable`1[System.DateTime]' must declare a default (parameterless) constructor &#39;TruncateTime(System.Nullable`1 [System.DateTime])&#39;不支持对SQL的转换。 - TruncateTime(System.Nullable`1[System.DateTime])' has no supported translation to SQL.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM