Click to See Complete Forum and Search --> : Need help with optional function parameters!


Aaron Croasmun
March 15th, 2001, 11:03 AM
I have a function that takes a list of 4 parameters. Only 1 of these are needed and the function executes differently depending on the other 3 parameters. ie:
If parameter2<>"" then
do this
end if
if parameter3<>"" then
do this
end if
and so on...but when i call the function:
MyFunction(1,,"hello")
I get an error saying Parameter2 doesn't exist. How can i check to see if parameter2 has a value? I tried isNull and that didn't work either. Thanks for your help!
-Aaron

Iouri
March 15th, 2001, 11:08 AM
In your function type the word 'Optional' in fromt of your optional parameter

Iouri Boutchkine
iouri@hotsheet.com

Aaron Croasmun
March 15th, 2001, 11:14 AM
I do that but I still get the error (sorry, i should have mentioned that)
-Aaron

shree
March 15th, 2001, 11:15 AM
There are two methods to it. If it doesn't matter if the arguments are variant, then you can use the IsMissing() function to check if the parameter was passed.


private Sub Func1(Param1, optional Param2, optional Param3, optional Param4)
If Not IsMissing(Param2) then
...
...
End If
... and so on
End Sub




If instead, your variable cannot be variant, you can set it in the declaration to a value that the variable doesn't typically contain as follows. Then in the function, check if the variable has that atypical value.


private Sub Func2(Param1 as Integer, optional Param2 as Integer = -999, optional Param3 as string = "", optional Param4 as Boolean = true)
If Not (Param2 = -999) then
...
...
End If
... and so on
End Sub