Dim
state ment for the
variable with the other
Dim
statements at the beginning of
the module.
The most important part about the
Dim
state ment is where to put it. You
have two choices: You can put it at the be ginning of your procedure (right after
the
Sub
statement) or at the beginning of your module (before any
Sub
state -
ments). If you put it any place else, it will not work. While pro grammers have
lots of good reasons to put
Dim
statements in procedures, we are scripters, so for
the pur poses of this book, we will put most of our
Dim
state ments at the be gin-
ning of the mod ule. A
Dim
statement at the be ginning of a module means that
every procedure in the mod ule can ac cess that variable. That is, the scope of the
variable is the en tire mod ule.
Alternatively, if you put the
Dim
statement at the be ginning of the pro ce-
dure, only that procedure can use the variable; that is, the scope of the vari able is
the pro cedure. For the
YourName
procedure, it would be pretty silly to create
the
userName
variable so that only
YourName
could use it. If we did that,
when we add a second pro cedure (such as the
DoingWell
procedure to tell the
user how well he or she is do ing), we won’t be able to use the name typed by the
user. That is, we would be stuck saying “Good job” in stead of “Good job, Ada.”
Therefore, we want to add a
Dim
statement at the be ginning of the mod ule:
52 Let’s Get Scripting
WARNING!
Dim userName
Sub YourName()
userName = InputBox(Prompt:="Type your name", _
Do'stlaringiz bilan baham: |