ASP 101 - Active Server Pages 101 - Web01
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash





Script Output:

Values Before Calling the Sub:
intDefault = 1
intByRef = 1
intByVal = 1

Now Calling:
SampleSub(intDefault, intByRef, intByVal)

Values After Calling the Sub:
intDefault = 2
intByRef = 2
intByVal = 1


ASP HTML Download            FAQ

ASP.NET Version

This main purpose of this sample is to illustrate one of the side effects that can occur when you pass parameters to a function or subroutine.

There are two ways that you can specify for VB to pass arguments to a procedure: by reference (ByRef) or by value (ByVal).

If you specify ByRef, then VB simply passes a pointer to the existing value in memory. In many cases this is faster then passing the value, but if you change the value of the parameter, the value of the underlying variable that you passed in is also changed. This is usually not desirable as it can make troubleshooting extremely difficult. Modifying values in this manner is therefore almost universally frowned upon and is generally considered poor programming practice. If you need to return a value it's usually best to return it as the return value of a function.

The other option is to specify ByVal. This causes VB to make a copy of the value of the variable and pass this new copy to the procedure. The result is that any changes made to the parameter are discarded when the routine completes execution. It's generally slightly slower then ByRef, but in some specific situations where execution is split between multiple computers it can provide better performance because it eliminates the need to pass changes to parameters back to the original machine.

As you can tell from the results of the sample above, if you don't specify which method to use, then by default VB will pass parameters ByRef.


Samples

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers