5

在此示例中,我不知道如何将可选参数设置c为空List(Of thing)

Sub abcd(a as something, b as something, optional c as List(Of thing) = ?? )
    ' *stuff*
End Sub

我考虑设置cnull,但这似乎是一件坏事。

4

2 回答 2

9

你不能。可选值必须是编译时常量。您可以分配给的唯一编译时常List(Of T)量是Nothing.

可以做的是用省略List(Of T)参数的方法重载该方法。然后,此重载可以将空传递List(Of T)给原始方法:

Sub abcd(a as something, b as something)
    abcd(a, b, New List(Of T)())
End Sub

Sub abcd(a as something, b as something, c as list(of thing))
    doStuff()
End Sub
于 2014-12-03T04:15:38.147 回答
2

我很欣赏这是一个老问题(我为违反礼仪而感到羞耻)但是......

我今天遇到了完全相同的问题。它是通过传递一个对象来解决的......

Sub abcd(a as something, b as something, optional c as Object = Nothing )
    Dim lstC as new List(Of thing)
    If Not IsNothing(c) then
        lstC = c
    End IF
    ' Then in your code you just have to see if lstC.Count > 0
    ' *stuff*
End Sub
于 2018-08-13T12:45:42.467 回答