Set the default value for the optional "List (Of t)" to an empty list?

In this example, I cannot figure out how to set the optional parameter c

to empty List(Of thing)

:

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

      

I considered setting c

on null

, but that seems like a bad thing.

+3


source to share


1 answer


You can not. Optional values ​​must be compile-time constants. The only compile-time constant you can assign List(Of T)

is Nothing

.

What you can do is overload this method for those who don't have a parameter List(Of T)

. This overload can then pass empty to the List(Of T)

original method:



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

      

+5


source







All Articles