270 Part II Programming Fundamentals
By declaring Cost using ByVal, you can safely modify Cost in the CostPlusInterest procedure
without sending the changes back to the calling procedure. By keeping Total declared using
ByRef, you can modify the variable that’s being passed, and only those changes will be
passed back to the calling procedure. In general, if you use ByRef only when it’s needed, your
programs will be freer of defects.
Here are some guidelines on when to use ByVal and when to use ByRef:
n Use ByVal when you don’t want a procedure to modify a variable that’s passed to the
procedure through an argument.
n Use ByRef when you want to allow a procedure to modify a variable that’s passed to
the procedure through an argument.
n When in doubt, use the ByVal keyword.
Chapter 10 Quick Reference
To Do This
Create a new
module
Click the Add New Item button on the Standard toolbar, and then select
the Module template;
or
Click the Add New Item command on the Project menu, and then select
the Module template.
Rename a module Select the module in Solution Explorer. In the Properties window, specify
a new name in the File Name property;
or
Right-click the module in Solution Explorer, select Rename, and then
specify a new name.
Remove a module
from a program
Select the module in Solution Explorer, and then click the Exclude From
Project command on the Project menu.
Add an existing
module to a project
On the Project menu, click the Add Existing Item command.
Create a public
variable
Declare the variable by using the Public keyword between the Module
and End Module keywords in a module. For example:
Public TotalSales As Integer
Create a public
function
Place the function statements between the Function and End Function
keywords in a module. Functions are public by default. For example:
Function HitRate(ByVal Hits As Short, ByVal _
Tries As Short) As String
Dim Percent As Single
Percent = Hits / Tries
Return Format(Percent, "0.0%")
End Function