once a parameter is defined as optional within the method, then all the parameters following it must also be set as optional. (see definition of optional, above.) an optional parameter value can be set to a default value that will be used if a value is not sent to the method. if the calling code doesn't send a value for the optional parameter variable, you can determine the value that will be used in your vb component code without the need to set it in the method's body. the default value will not be the value used when the calling code sends a value for the parameter variable. the value sent will be used instead. so, in our example we'll set the optional blnageemphasis boolean variable to false. as a result, if the calling code doesn't send a value for it, we won't offend anyone by emphasizing the fact that they are age 45 or older, by default at least. now we need to designate each parameter variable as being passed to the method code as byval. this indicates that a copy of the variable's value will be sent from the calling code to the vb method code rather than a reference (byref) to the variable's value (which remains resident in the memory of the code that called the method). use byval for calling methods from asp files.
strname as string
intage as integer
optional blnageemphasis as boolean = false
next, we need to add a few comas, spaces, and underline characters (, _) to the end of some of the code lines so we can break up our lines for appearance sake without vb protesting that the syntax is wrong. when we place our parameter list in the method's parenthesis, we get the following error-free method definition:
byval strname as string
byval intage as integer
optional byval blnageemphasison as boolean = false
typing our method definition into the vb code window will generate an end function. everything in between the method's definition we typed in and the end function line is called the body of the method. this is where the code for revealing the day count of a person's age in years will be placed.
public function examplemethod(byval strname as string, _
byval intage as integer, _
optional byval blnageemphasison as boolean = false) as string
the first code we'll add to our method's body is a declaration of a string variable that will be used to store the string value this method will return. we could manage to send back our reply text without using a string variable to gather this information, but using a string for this purpose makes for cleaner code most of the time. now we can begin to construct our return string. we'll first use the strname variable value sent by the asp code via the method's parameter list. we'll concatenate the strname parameter variable value with the string literal (" is over "). added to this will be our calculated "age in days," using the intage parameter variable, also from the incoming parameter list. the intage will be used to calculate the number of days a person has lived, depending on their age in years. notice that since we're using the product of two integers (intage * 365) we'll change the resulting integer of our calculation into a string before we concatenate it to the strreturnstring string. the vb cstr() method is used to do this.
dim strreturnstring as string
given that the name value sent to us from the asp code was "eric clapton" and the age sent was 56, our strreturnstring should now contain the following value:
strreturnstring = strname & " is over " & cstr(intage * 365)
eric clapton is over 20440
to finish off our string we'll either add " days old." or " days old.", depending whether the value of intage is 45 or greater and whether the blnageemphasison variable is set to true. if a value for blnageemphasison was not sent as a method parameter from the asp code, its value will default to "false" as determined by our method definition. if it's set to "true" by the calling asp code and the intage variable value is 45 or greater, we'll get the following string to return:
if blnageemphasison and intage > 44 then
strreturnstring = strreturnstring & " days old."
else
strreturnstring = strreturnstring & " days old."
end if
eric clapton is over 20440 days old.
otherwise we'll get:
eric clapton is over 20440 days old.
to return the string to the calling asp code we assign the value of our string to the name of our method:
examplemethod = strreturnstring
the complete method code follows:
public fun