Functions with Optional Inputs

Results 1 to 2 of 2

Thread: Functions with Optional Inputs

  1. #1
    Join Date
    Dec 1969

    Default Functions with Optional Inputs

    I’ve never written a function where some of the inputs are optional. For example,<BR><BR>Function MergeName(strFirst, strMiddle, strLast)<BR>MergeName = strFirst & “ “ & strMiddle & “ “ & strLast<BR>Function End<BR><BR>How would I call this function in the event that I don’t have a middle name to input?<BR><BR>Like this (?) :<BR>response.write MergeName(“Lonnie”, “”, “Best”)<BR><BR>What is the best way to handle functions where some of the inputs are optional?<BR>

  2. #2
    Join Date
    Dec 1969

    Default Can't be done with VBScript

    Sorry, but the language simply doesn&#039;t allow it.<BR><BR>You could write an ActiveX component with a method that has optional arguments (as, for example, ADODB.RecordSet.Open uses), but not in just VBScript.<BR><BR>JScript, on the other hand, *can* allow optional arguments, so long as they are the trailing arguments. (That is, you can&#039;t leave out a middle argument and supply one after it.)<BR><BR>In the specific example you gave, probably the best thing to do is to supply "" or NULL as the missing argument and then check for "" or NULL in the function and do the appropriate thing.<BR><BR>For example:<BR><BR>Function MergeName(strFirst, strMiddle, strLast)<BR>&nbsp; &nbsp; MergeName = strFirst <BR>&nbsp; &nbsp; If strMiddle &#060;&#062; "" Then MergeName = MergeName & " " & strMiddle <BR>&nbsp; &nbsp; MergeName = MergeName & " " & strLast<BR>Function End<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts