sending empty value to optional parameter

  • Thread starter Thread starter mick0987b
  • Start date Start date
M

mick0987b

If you have a function that accepts an optional parameter:

Function select_live_nodes(ByVal sf_node_parent As Integer, Optional
ByVal node_orderby As String = "my_default_value”)

I know you can call the function with
X = select_live_nodes(2)

Or

X = select_live_nodes(2,”another order”)

But how do I send a varialbe as the optional parameter that might or
might not contain a value?

X = select_live_nodes(2,myvariable)

Even if ‘myvarialbe’ is empty the function does not resort to its
default value, "my_default_value”

I’ve tried sending dbnull, string.empty, but I just cant get the
parameter to resort to its default value unless I completely miss it
out from the call.

Anyone help?
 
I've tried sending dbnull, string.empty, but I just cant get the
parameter to resort to its default value unless I completely miss it
out from the call.

Try calling the routine with nothing supplied for the optional argument.
Then it will fill in the default value. As long as you send in anything,
you will get what you send in. That is how optional parameters work.

The other pattern that can be employed is testing for the bad value.

If (node_orderby = Nothing) Then
node_orderby = "my_default_value"
End If

Not as pretty as the optional parameter, but it works. It is called "input
checking".

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Surely
If (node_orderby Is Nothing) Then...

?

Function select_live_nodes(ByVal sf_node_parent As Integer, Optional
ByVal node_orderby As String)

If (node_orderby = Nothing) Then
node_orderby = "my_default_value"
End If

'Rest of function here

End Function

This ensures you supply a "default" value when you pass Nothing to the
parameter.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Gregory said:
Function select_live_nodes(ByVal sf_node_parent As Integer, Optional
ByVal node_orderby As String)

If (node_orderby = Nothing) Then
node_orderby = "my_default_value"
End If

'Rest of function here

End Function

This ensures you supply a "default" value when you pass Nothing to the
parameter.

I was referring to using "is" vs. "=" for the comparison, as in ("" =
Nothing) is true whereas ("" Is Nothing) is false. Did I miss something?

Andrew
 
Yeah this was my next tactic but I thought I might be missing
something.
'Input checking' it is.

Thanks for your help :)
 
I was referring to using "is" vs. "=" for the comparison, as in ("" =
Nothing) is true whereas ("" Is Nothing) is false. Did I miss something?

I may have that incorrect, as I do not do Visual Basic that often any more.
But it is standard for older VB. If the = Nothing works, then I would use
that. Here is how I normally write things:

if(x == null)
{
}

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Back
Top