sending empty value to optional parameter

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?
 
G

Gregory A. Beamer

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! |
*******************************************
 
G

Gregory A. Beamer

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! |
*******************************************
 
A

Andrew Morton

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
 
M

mick0987b

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

Thanks for your help :)
 
G

Gregory A. Beamer

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! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top