How do you use default parameters in VB functions?
-
I couldn't seem to find the answer from whatever documents I have right now... In C/C++ we have "default parameters" for functions,
int Func(int nPara1, int nPara2 = 0);
, but how do we achieve this in VB? Thanks a lot.Use the
Optional
keyword eg:Function Func(nPara1 as Integer, Optional nPara2 as Integer = 0) as Integer
-- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
-
Use the
Optional
keyword eg:Function Func(nPara1 as Integer, Optional nPara2 as Integer = 0) as Integer
-- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
-
Thanks for your reply, I forgot to mention that I was using VBScript(ASP application) so the "Optional" keyword is not supported :( Is there anyway I can do that in VBScript? Thank you.
Option parameters are not supported at all. I haven't tried it, but since all variables in VBScript are of type Variant, you might look into passing an array of values to your function. This could be used to simulate optional arguments, but your function will have to be written to parse up the array and you can't pass parameters by reference at all. RageInTheMachine9532
-
Use the
Optional
keyword eg:Function Func(nPara1 as Integer, Optional nPara2 as Integer = 0) as Integer
-- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky