For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > February 2005 > Optional parameters









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Optional parameters
Dan

2005-02-24, 3:55 pm

Is there a way to test inside a subroutine if an optional parameter in that
subroutine's Sub statement was actually included in the call to that
subroutine or not?

Public Sub Sub1(Param1 As String, Optional Param2 As Long)

<<some kind of test whether Param2 was included in the call>>

End Sub

Call Sub1("Testing")

vs.

Call Sub1("Testing", 123)

-Dan
Frank Lehmann

2005-02-24, 3:55 pm

> Is there a way to test inside a subroutine if an optional parameter in that
> subroutine's Sub statement was actually included in the call to that
> subroutine or not?
>
> Public Sub Sub1(Param1 As String, Optional Param2 As Long)
>
> <<some kind of test whether Param2 was included in the call>>
>
> End Sub
>
> Call Sub1("Testing")
>
> vs.
>
> Call Sub1("Testing", 123)
>
> -Dan


Hi Dan,

If IsMissing(Param2) Then
...
End If

But!, Param2 must be of type Variant for this to work.

-----
Frank




Saga

2005-02-24, 3:55 pm


In the decleration, assign a default value to the parameter,
preferably one that won't happen in execution, then test for
that condition.

Private Sub xyz(Optional ByVal xx As Long = -1)

If xx = -1 Then
MsgBox "No param"
Else
'Do whatever.
End If

End Sub

Good luck!
Saga

"Dan" <Dan@discussions.microsoft.com> wrote in message
news:372245CD-E275-4D6C-93C7-C3AB8B829682@microsoft.com...
> Is there a way to test inside a subroutine if an optional parameter in
> that
> subroutine's Sub statement was actually included in the call to that
> subroutine or not?
>
> Public Sub Sub1(Param1 As String, Optional Param2 As Long)
>
> <<some kind of test whether Param2 was included in the call>>
>
> End Sub
>
> Call Sub1("Testing")
>
> vs.
>
> Call Sub1("Testing", 123)
>
> -Dan



Ken Halter

2005-02-24, 3:55 pm

"Dan" <Dan@discussions.microsoft.com> wrote in message
news:372245CD-E275-4D6C-93C7-C3AB8B829682@microsoft.com...
> Is there a way to test inside a subroutine if an optional parameter in
> that
> subroutine's Sub statement was actually included in the call to that
> subroutine or not?
>
> Public Sub Sub1(Param1 As String, Optional Param2 As Long)
>
> <<some kind of test whether Param2 was included in the call>>
>
> End Sub
>
> Call Sub1("Testing")
>
> vs.
>
> Call Sub1("Testing", 123)
>
> -Dan


If you set the default to an out of range value, you can test that...

Public Sub Sub1(Param1 As String, Optional Param2 As Long = -1)

If Param2 = -1 Then 'optional param skipped or passed as -1

If they were variants, you could use the IsMissing keyword but since they
aren't, you can't.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..


MikeD

2005-02-24, 3:55 pm


"Dan" <Dan@discussions.microsoft.com> wrote in message
news:372245CD-E275-4D6C-93C7-C3AB8B829682@microsoft.com...
> Is there a way to test inside a subroutine if an optional parameter in

that
> subroutine's Sub statement was actually included in the call to that
> subroutine or not?
>
> Public Sub Sub1(Param1 As String, Optional Param2 As Long)
>
> <<some kind of test whether Param2 was included in the call>>
>
> End Sub
>
> Call Sub1("Testing")
>
> vs.
>
> Call Sub1("Testing", 123)



Yes. The parameter MUST be a Variant and then you can use the IsMissing
function.

Public Sub Sub1(Param1 As String, Optional Param2 As Variant)

If IsMissing(Param2) Then
'Param2 was not passed
End If

End Sub

Look up the IsMissing function in Help for more information on it.

However, it's frequently better to just specify a default value for an
optional parameter and use a specific data type. This avoids problems
associated with Variants.

Public Sub Sub1(Param1 As String, Optional Param2 As Long = -1)

If Param2 = -1 Then
'Param2 was not passed or -1 was specifically passed
End If

End Sub

--
Mike
Microsoft MVP Visual Basic



MikeD

2005-02-24, 3:55 pm


"Saga" <antiSpam@somewhere.com> wrote in message
news:OeQHM$oGFHA.1948@TK2MSFTNGP10.phx.gbl...
>
> In the decleration, assign a default value to the parameter,
> preferably one that won't happen in execution, then test for
> that condition.
>
> Private Sub xyz(Optional ByVal xx As Long = -1)
>
> If xx = -1 Then
> MsgBox "No param"
> Else
> 'Do whatever.
> End If
>
> End Sub



I agree that using a specific data type and providing a default value is
usually preferable to a Variant and the IsMissing function, but you can't
(or shouldn't) assume no parameter was passed if the parameter has the
default value. It's a dangerous assumption to make even if the default
value is "one that won't happen in execution". You might not intend for
that value to ever get explicitly passed, but that doesn't mean it won't.

--
Mike
Microsoft MVP Visual Basic


Rick Rothstein

2005-02-24, 3:55 pm

> Is there a way to test inside a subroutine if an optional parameter in
that
> subroutine's Sub statement was actually included in the call to that
> subroutine or not?
>
> Public Sub Sub1(Param1 As String, Optional Param2 As Long)
>
> <<some kind of test whether Param2 was included in the call>>
>
> End Sub
>
> Call Sub1("Testing")
>
> vs.
>
> Call Sub1("Testing", 123)


If you declare the optional parameter as a Variant data type, you can
use the IsMissing function to see if it is there or not. However, VB6
allows you to set a default value for optional parameters no matter what
the data type. For example,

Public Sub Sub1(Param1 As String, Optional Param2 As Long = -1)

Inside your Sub, Param2 will be whatever the user passes in and -1 if
the user doesn't pass that parameter in. This usually does away with a
need to know if the user passed the optional parameter in or not.
However, depending on data requirements, you might be able to set a
default value that lets you know if the user passed in a value or not.
For example, if you did this

Public Sub Sub1(Param1 As String, Optional Param2 As Long = -2147483648)

then checking for Param2 = -2147483648 could be your test... if Param2
= -2147483648, then you could assume the user didn't pass in a value,
otherwise he did.

Rick - MVP

Jeff Johnson [MVP: VB]

2005-02-24, 3:55 pm


"Dan" <Dan@discussions.microsoft.com> wrote in message
news:372245CD-E275-4D6C-93C7-C3AB8B829682@microsoft.com...

> Is there a way to test inside a subroutine if an optional parameter in
> that
> subroutine's Sub statement was actually included in the call to that
> subroutine or not?
>
> Public Sub Sub1(Param1 As String, Optional Param2 As Long)
>
> <<some kind of test whether Param2 was included in the call>>


If you declare the parameter As Variant you can use the IsMissing() function
as Frank mentioned. With any other data type, you must specify a default
value which will be considered the "missing" value. This means you must pick
a value that can NEVER be a valid value (as far as your businiess rules go)
for the data. For many numeric parameters a good choice is -1 as long as
negative numbers never make sense in that case (for example, age). For
strings the empty string is often a good choice. For dates, something far in
the past or future.


Dan

2005-02-24, 3:55 pm

Jeff,

A big thank you to you and everyone else who responded to my question. I've
never had 7 people respond right away to a question!

I should have been more careful with my example, because my optional
parameter is a variant, so the IsMissing function is exactly what I was
looking for. Otherwise, I'm familiar with the default value that you can add
to an optional parameter and use to test whether the user passed in a
different value.

Thanks again for the help!

-Dan

"Jeff Johnson [MVP: VB]" wrote:

>
> "Dan" <Dan@discussions.microsoft.com> wrote in message
> news:372245CD-E275-4D6C-93C7-C3AB8B829682@microsoft.com...
>
>
> If you declare the parameter As Variant you can use the IsMissing() function
> as Frank mentioned. With any other data type, you must specify a default
> value which will be considered the "missing" value. This means you must pick
> a value that can NEVER be a valid value (as far as your businiess rules go)
> for the data. For many numeric parameters a good choice is -1 as long as
> negative numbers never make sense in that case (for example, age). For
> strings the empty string is often a good choice. For dates, something far in
> the past or future.
>
>
>

Ralph

2005-02-24, 8:55 pm


>-----Original Message-----
>
>"Saga" <antiSpam@somewhere.com> wrote in message
>news:OeQHM$oGFHA.1948@TK2MSFTNGP10.phx.gbl...
parameter,[color=darkred]
test for[color=darkred]
>
>
>I agree that using a specific data type and providing a

default value is
>usually preferable to a Variant and the IsMissing

function, but you can't
>(or shouldn't) assume no parameter was passed if the

parameter has the
>default value. It's a dangerous assumption to make even

if the default
>value is "one that won't happen in execution". You

might not intend for
>that value to ever get explicitly passed, but that

doesn't mean it won't.
>
>--
>Mike
>Microsoft MVP Visual Basic
>


Mike brings up a good point,

While there is a common adversion to using Variants,
IMHO, this avoidance is often misplaced. Variants are at
the heart of VBness. They are useful tools. When a
Variant is needed, use it.

Coming up with a 'magic value' for all situations can be
just as error prone as any inherent problems induced by
using a Variant.

-ralph

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com