Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Detecting if a parameter has been passed
Message
 
To
10/12/1997 11:47:10
General information
Forum:
Visual Basic
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00065022
Message ID:
00065873
Views:
37
>I thought I had the correct syntax to detect if a parameter has been passed.
>
>I have the following method which accept a parameter and I would like to know if the parameter was passed.
>
>Sub Cleanup(Optional tnError As Integer)
>
>' If we pass a parameter
>If VarType(tnError) = 2 Then
> lnLastError = tnError
>End If
>
>But, this is not working because even if the tnError is not passed, the type is still integer.

I see that you figured out the Variant solution. This was the common
solution in VB4. However, VB5 provides a better alternative. You can
use typed parameters and provide a default argument:

Sub Cleanup(Optional tnError As Integer = 0)
If tnError = 0 Then
' Do default case for no argument
Else
' Handle specific argument
End If
End Sub

This technique depends on being able to find a value that wouldn't
normally be passed and use it as the signal that no argument was passed.
This number might be 0 or -1 for numeric types. For strings it might be
"" or vbNullString. If all possible values are valid, you won't be able
to use this technique and will have to use the Variant version with
IsMissing. Behind the scenes the Variant version works the same way.
Variants can store error values in addition to other types, and one of
the error values represents a missing value. VB automatically generates
this value if no argument is passed to an optional Variant parameter,
and IsMissing simply checks for it.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform