In this example, I can't figure out how to set the optional parameter c to an 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 to null, but that seems like a bad thing to do.
Use the '=' operator only when you need to pass a default value to your variables hence, making them optional parameters. This is called a canonicalized constructor.
Why shouldn't you make the default arguments an empty list? Answer: d) is a bad idea because the default [] will accumulate data and the default [] will change with subsequent calls.
A parameter wrapped by { } is a named optional parameter. Also, it is necessary for you to use the name of the parameter if you want to pass your argument.
Using square brackets [] Lists in Python can be created by just placing the sequence inside the square brackets [] . To declare an empty list just assign a variable with square brackets.
You can't. Optional values have to be compile-time constants. The only compile-time constant you can assign to List(Of T) is Nothing.
What you can do is overload that method with one that omits the List(Of T)parameter. This overload can then pass an empty List(Of T) to the 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
I appreciate this is an old question (and shame on me for breaching etiquette in replying) but...
I had exactly the same issue today. It was resolved by passing an object...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With