I want to specify an List(Of String) as an Optional function argument. But i am unable to initialize it in the function declaration. Can You plz show me how is it done.
Thanks
Printable View
I want to specify an List(Of String) as an Optional function argument. But i am unable to initialize it in the function declaration. Can You plz show me how is it done.
Thanks
The only option is to supply Nothing as the default value. Then, in the function, you would check if the list is Nothing, and if it is you assume it has not been passed.
So you can't really distinguish between someone not passing anything and someone passing 'Nothing', such as thisCode:Private Sub SomeMethod(ByVal x As Integer, Optional ByVal list As List(Of String) = Nothing)
If list Is Nothing Then
' list has not been passed
End If
End Sub
If you absolutely need to then you can simply define the same method twice, with different parameters. In that case you can supply whatever value you want for the list:Code:Me.SomeMethod(32, Nothing)
Code:Private Sub SomeMethod(ByVal x As Integer)
Dim newList As New List(Of String)
newList.AddRange({"A", "B", "C"})
Me.SomeMethod(x, newList)
End Sub
Private Sub SomeMethod(ByVal x As Integer, ByVal list As List(Of String)
...
End Sub