ction examplemethod(byval strname as string, _ byval intage as integer, _ optional byval blnageemphasison as boolean = false) as string '///// set local string variable dim strreturnstring as string '///// set the value of the returning string strreturnstring = strname & " is over " & cstr(intage * 365) '///// add to the strreturnstring if blnageemphasison and intage > 44 then strreturnstring = strreturnstring & " days old." else strreturnstring = strreturnstring & " days old." end if '///// send back composed string examplemethod = strreturnstring end function
much of the asp code we now need was previously introduced in the 'a conceptual overview' section of this article (see above). the basic sequence of steps follows:
- to instantiate our vb component by using the createobject() method of the asp server object.
- call our component's method using the proper method parameter variables.
- assign the string value returned from our vb method to a variable in the asp code.
- then use this variable in a response.write() method to send it on its way to the visiting browser.
set objreference = server.createobject("exampleproject.exampleclass")
the asp createobject() method returns a reference to an object of our vb code so that we can call any of public methods for our class from the asp file. notice that it's our vb project and class names that are used as method parameters of the asp createobject() method. the objreference variable is used to hold the reference to the object instance of our component's class (e.g., a reference to the memory location of an individual instance our vb class code).
we can now use our component's class method, examplemethod, and receive a string indicating the minimum number of days a person has lived. the line of code below uses literals as parameter values and assigns the string value returned from the method to a variable named strmethodreturn. note that the objreference represents the exampleproject.exampleclass used in the createobject() method when our component was instantiated. recall that you can think of the objreference.examplemethod part of this code as equivalent to exampleproject.exampleclass.examplemethod(), our component's hierarchy, although it can't be literally used like this.
strmethodreturn = objreference.examplemethod("eric clapton", 56, true)
alternatively, you can use variables instead of literals for the method's parameters. the parameter variable names you choose in the asp file don't have to be the same variable names used in the vb method parameter list. they just need to match as far as the number of nonoptional parameters, the order in which they are used, and the variable types used in the vb parameter list. using variables in place of literals tends to be cleaner and more manageable, especially when your method's parameter list becomes lengthy.
aspname = "eric clapton"
aspage = 56
aspemphasis = true
strmethodreturn = objreference.examplemethod(aspname, aspage, aspemphasis)
now we just need to use the strmethodreturn variable within an asp response.write() method to send our returning method value back to the browser that visited our example asp file. here's the entire asp code. we'll add a line of code to de-reference our component's object at the end of the code for the purpose of cleaning up. putting the above asp code within an asp file will result in the following string being placed
<%
'///// instantiate the component object
set objreference = server.createobject("exampleproject.exampleclass")
'///// set local variables to send as method parameter values
aspname = "eric clapton"
aspage = 56
aspemphasis = true
'///// call the component's method and store the returned string
strmethodreturn = objreference.examplemethod(aspname, aspage, aspemphasis)
'///// send the returned string to the visiting browser
response.write(strmethodreturn)
'///// clean up
set objreference = nothing
%>