Passing null to an optional parameter with default value
I think this is a pretty basic question, but I just want to clarify. If I have a variable with a null value, and pass it as a parameter that is optional, will the par开发者_JAVA百科ameter get the null value, or the default value?
dim str As String = "foo"
dim obj As Object
//call 1
Request(str, str)
//call 2
Request(str)
//call 3
Request(str, obj)
public Function Request(byVal someVal As String, Optional ByVal someVal2 As String = "bar")
...
I know that call 1 will make someval == someval2 == "foo"
inside the function, and call 2 will make someval == "foo"
and someval2 == "bar"
and call 3 will make someval == foo
but what is someval2
equal to in call 3? nullable or bar?
Also - I'm relatively new to vb.net and I don't think I fully understand the null/nullable/nothing concept differences from C#
"what is someval2 equal to in call 3? nullable or bar?" It will be null.
Well, actually, you can't do call 3 ... it will not compile because you can't pass an object as a string parameter. However, if you had dim obj as string = null, then it would be null.
If no parameter value is supplied to an optional parameter, the default value for that parameter will be used.
If Nothing
is passed to an optional parameter, the parameter value will be Nothing
and the default value will be ignored.
精彩评论