在此示例中,我无法弄清楚如何将可选参数c
设置为空List(Of thing)
:
Sub abcd(a as something, b as something, optional c as List(Of thing) = ?? )
' *stuff*
End Sub
我考虑过将c
设置为null
,但这似乎是件坏事。
答案 0 :(得分:7)
你不能。可选值必须是编译时常量。您可以分配给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
答案 1 :(得分:1)
我感谢这是一个古老的问题(由于在回复中违反礼节而感到羞耻),但是...
我今天有完全一样的问题。通过传递一个对象来解决...
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