Академический Документы
Профессиональный Документы
Культура Документы
Use the following rules when you name procedures, constants, variables, and arguments in a Visual Basic module: You must use a letter as the first character. You cant use a space, period ( . , e!clamation mar" ( ! , or the characters @, &, $, # in the name. #ame cant e!ceed $%% characters in length. You cant repeat names within the same level of scope. &or e!ample, you cant declare two variables named age within the same procedure. 'owever, you can declare a private variable named age and a procedure(level variable named age within the same module.
Note: Visual Basic isnt case-sensitive, but it preserves the capitalization in the statement where the name is declared.
Adding Comments
)omments provide a convenient means to document a program. Visual Basic ignores comments when it compiles your program. )omment lines begin with a single apostrophe ( ' , followed by a te!tual message, and can be added anywhere in a procedure. By default, comments are displayed as green te!t. Example:
Assignment Statements
*ssignment statements assign a value or e!pression to a variable or constant. *ssignment statements always include an e+ual sign (= . ,ome e!amples of assignment statements are:
youName = InputBox( !hat "s your name#$% age = current&ate ' b"rth&ate message = Show me the money$
Numeric Constants
Information and Communication Technology Department Palompon Institute of Technology
14
#umbers are referred to as numeric constants in Visual Basic. -ost numeric constants are e!pressed as integers (whole numbers that do not contain a decimal point or an e!ponent , long integers (similar to integers with an e!tended range , single-precision real quantities (numbers that include a decimal point, an e!ponent, or both , or double-precision real quantities (similar to single(precision real +uantities with an e!tended range and greater precision . .he following rules apply to numeric constants: /. $. 2. 7. %. )ommas cannot appear anywhere in a numeric constant. * numeric constant may be preceded by a 0 or a 1 sign. .he constant is understood to be positive if a sign does not appear. *n integer constant occupies two bytes. 3t must fall within the range (2$,456 to 2$,454. 3t cannot contain either a decimal point or an e!ponent. * long integer constant occupies four bytes. 3t must fall within the range ($,/74,762,576 to $,/74,762,574. 3t cannot contain either a decimal point or an e!ponent. * single(precision real constant occupies four bytes. 3t can include a decimal point and as many as seven significant figures. * single(precision real constant can include an e!ponent if desired. 8!ponential notation is similar to scientific notation, e!cept that the base /9 is replaced by the letter E. .hus, the +uantity /.$ ! /9 (2 could be written as /.$8(2. .he e!ponent itself can be either positive or negative, but it must be a whole number, i.e., it cannot contain a decimal point. * double(precision real constant occupies eight bytes. 3t can include a decimal point and as many as fifteen significant figures. * double(precision real constant can include an e!ponent if desired. :ouble(precision e!ponential notation is similar to scientific notation, e!cept that the base /9 is replaced by the letter D. .hus, the +uantity /.5554 ! /9(2 could be written as /.5554:(2. .he e!ponent itself can be either positive or negative, but it must be a whole number, i.e., it cannot contain a decimal point.
5.
( +,2-(
1 +,(2-*+.
+1 1/02
()1*+1 .((((((
1(*+1 .&+1
String Constants
* string constant is a se+uence of characters (i.e., letters, numbers and certain special characters, such as 0, (, ;, <, =, >, etc. , enclosed in +uotation mar"s. Blan" spaces can be included within a string. * +uotation mar" can also be placed within a string, but it must be written as two adjacent +uotation mar"s. ,tring constants are used to represent nonnumeric information, such as names, addresses, etc. .here is no practical restriction on the ma!imum number of characters that can be included within a string constant. .hus, the ma!imum length of a string constant can be considered infinite. ,everal string constants are shown below:
Please type a 7alue for 48$ !elcome to the 21st 4entury$ .;.(/2,$ &o you w"sh to try aga"n#$ $Please <on>t snore "n class$$
Data Types
Information and Communication Technology Department Palompon Institute of Technology
15
Visual Basic supports all common data types, including Integer, Long (i.e., long integer , ingle, !ouble, and tring. .he language also supports other data types, such as Boolean, B"te, #urrenc", and !ate data, as well as Variant(type and user(defined data types. Below is a specific description of each data type in Visual Basic. Boolean ( indicates the presence of logical data that can contain either of two values, 3rue or ?alse. 8!ample:
&"m frm 2s ?orm &"m txt&"splay as 3extBox Pr"7ate Sub ?ormA2ct"7ate(% Set frm = ?orm1 Set txt&"splay = 3ext1 frm)4apt"on = 4reate< from BbCects$ txt&"splay)text = I am also create< from BbCects$ *n< Sub
Single ( a single precision number that represents fractional numbers, numbers with decimal places, or e!ponential numbers. #egative values are ranging from (2.79$6$2826 to (/.79/$A68(7%, with Bositive values ranging from /.79/$A68(7% to 2.79$6$2826. String ( can contain up to appro!imately $ billion of characters. User-Define !"ype 1 a user(defined type is a compound structure that holds different variables of simpler data types. Before you can use a User(:efined .ype variable, you must first define its structure, using a Type statement in the declaration section of a module. &or e!ample:
16
&"m *mp as *mpolyee6&3 *mp)Name = Dohn Sm"th$ *mp)&epartmentI& = 10-#ariant 1 variant is the default data type for Visual Basic. 3n other words, if you use a variable without specifying its type, as in the following line of code:
&"m my@ar"able
this will become a Variant variable. Decimal 1 a floating(point data type with a higher precision than :ouble, but it has a smaller range value. :ecimal is a singular case among the data types supported by Visual Basic in that you cant e!plicitly declare a variable using 2s &ec"mal. 3nstead, you assign a value to a Variant variable using the 4&ec conversion function, for e!ample:
Type Conversion
Visual Basic also supports conversion from one data type to another. ,ome conversion functions supported by Visual Basic are: $unction Description )onverts a variable to a Boolean data type. )onverts a variable to a Byte data type. )onverts a variable to a :ecimal data subtype. .his conversion function provides the only method of creating a :ecimal data subtype. )onverts a variable to a :ate;.ime data type. )onverts a variable to a )urrency data type. )onverts a variable to a :ouble precision data type. .he function accepts any number within the limits of the :ouble data type or any string data that can be converted to a number within the range of the double data type. )onverts a variable to an 3nteger data type. 4Int accepts any number within the limits of the integer data type or any string data that can be converted to a number and is within the limits of the integer data type. )onverts a variable to a Dong data type. .he function accepts any number
4Int(7ar"ableAname%
45ng(7ar"ableAname%
17
within the limits of the long integer data type or any string data that can be converted to a number whose value lies within the range of a long integer.
4Sng(7ar"ableAname%
)onverts a variable to a ,ingle data type. .he function accepts any number within the limits of the single data type or any string data that can be converted to a number within the range of the ,ingle data type. )onverts a variable to a ,tring data type. 4Str accepts any "ind of data. )onverts a variable to a Variant data type. 4@ar accepts any "ind of data.
4Str(7ar"ableAname% 4@ar(7ar"ableAname%
18
Variables
* variable is a name that represents a numerical +uantity, a string, or some other basic data item (e.g., a date, true;false condition, etc. . .he following rules apply to the naming of variables: /. $. 2. 7. %. * variable name must begin with a letter. *dditional characters may be letters or digits. )ertain other characters may also be included, though the period and special data-t"ping characters (e.g., E, F, G, H, and > are not permitted. 3n general, it is good programming practice to avoid the use of characters other than letters and digits. * variable name cannot e!ceed $%% characters. *s a practical matter, however, variable names rarely approach this siIe. Visual Basic does not distinguish between uppercase and lowercase letters. -any programmers use uppercase letters as word separators within a single variable name (e.g., &reeIingBoint, .a!Jate, etc. . Visual Basic includes a number of reserved words (e.g., :im, 3f, 8lse, ,elect, )ase, :o, etc. . .hese reserved words represent commands, function names, etc. .hey cannot be used as variable names.
2rea 4ounter
Fa<"us 4ustomerName
Variable Declaration
3f you have a number of variables to declare, you can do this on a single line by separating them with commas, as in the following &"m statement: Information and Communication Technology Department Palompon Institute of Technology
19
20
Constant Declaration
.o declare a constant in Visual Basic, you have to use the Const statement and assign or set its value. *fter a constant is declared, it cannot be modified or assigned a new value. You can declare a constant within a procedure or at the top of a module, in the declarations section. -odule( level constants are private by default. .o declare a public module(level constant, precede the Const statement with the Public "eyword. You can e!plicitly declare a private constant by preceding the Const statement with the Private "eyword to ma"e it easier to read and interpret your code. .he following e!ample declares the Public constant con2ge as an Integer and assigns it the value 27.
3n Visual Basic, you can e!plicitly declare a variable by simply using it in an assignment statement, as in the following e!ample:
Pr"7ate Sub ?ormA2ct"7ate(% <"splay3h"sS3FINH = I*55B !BF5&$ Pr"nt <"splay3h"sS3FINH *n< Sub
*ll variables that are implicitly declared are of type #ariant. Variables of type #ariant re+uire more memory resources than most other variables. Your application will be more efficient if you declare variables e!plicitly and with a specific data type. 8!plicitly declaring all variables reduces the incidence of name(conflict and spelling mista"es. Using the Bpt"on *xpl"c"t statement is good programming practice. 3t forces us to declare all variables and constants. You can automatically have Visual Basic add this to new modules as they are created by chec"ing the $equire Variable !eclaration option, which can be found on the 8ditor tab of the ?ptions dialog (Select!"ools!%%!Options . You can also used e!plicit declaration by placing the statement Bpt"on *xpl"c"t in a module before any procedure. Information and Communication Technology Department Palompon Institute of Technology
21
Khen the Bpt"on *xpl"c"t statement is used, Visual Basic can determine variables you used in your program that has not been declared.
22
Su""i es
Jather than declaring a data type e!plicitly (using !im or #onst statement , a variable or named constant can be associated with a data type by adding a single(character su%%i& to the end of the variable;constant name. several of the more commonly used suffi!es are listed below. u%%i& E F G H > !ata '"pe
,hown below are several variables whose data types are defined by suffi!es.
Variable
!ata '"pe
.he use of suffi!es is derived from earlier versions of the Basic language, and is included in Visual Basic largely for purposes of consistency and bac"ward compatibility. -odern programming practice encourages the use of e!plicit data type declarations rather than suffi!es.
,ometimes you need a variable to be seen by all the procedures within your proCect, while other variables should only be available within a particular procedure. .he visibility of a variable is "nown as its scope. )losely related to a variables scope is its lifetime, or the period of program e!ecution when the variable is live and available for use. Brecisely where you declare a variable or constant in a program determines its scope and its lifetime. &lobal!#ariables 3n Visual Basic Cargon, global variables are those variables declared using the Publ"c "eyword in the declaration section of Visual Basic modules. )onceptually, these variables are the simplest of the group because they survive for the life of the application and their scope is the entire application. 'o ule-Le(el!#ariables * variable has module-level scope when it can be accessed by all the subroutines and functions contained in a particular module. -odule(level variables are visible only from within the module they belong to and cant be accessed from the outside. .o create a variable with module(level scope, you must declare it in the Information and Communication Technology Department Palompon Institute of Technology
23
modules declaration section (that is to say, outside of any subroutine or function by using either the &"m or Pr"7ate ,tatement. )roce ure-Le(el!Scope * variable that is declared within an individual procedure (that is, within a subroutine or a function can only be used within that procedure, and is therefore said to have procedure-level scope. .he lifetime of a procedure(level variable ends when the *n< Sub or *n< ?unct"on statement is e!ecuted. .here is also a special type of variable that has procedure(level scope, called a static variable. * static variable is defined within a procedure, and although it has procedure(level scope, it has module(level lifetime. 3n practice, this means that you can only use the variable within the procedure in which its defined, but its value is maintained between calls to the procedure. .o declare a static variable, you use the Stat"c "eyword in a procedure, for e!ample:
,pecial symbols, called arithmetic operators, are used to indicate arithmetic operations such as addition, subtraction, multiplication, division, and e!ponentiation. .hese operators are used to connect numeric constants and numeric variables, thus forming arithmetic e&pressions. #isual!Basic!*rit+metic!Operators #isual!Basic Operator Description *ddition ,ubtraction -ultiplication :ivision (float :ivision (integer 8!ponentiation #egation -odulus #isual!Basic!Expression ,!x-./!y-0/!1-.23!4
+ + G N P Q + Eo<
Or er!of!)rece ence Operator,s4 Operation,s4
Or er!of!E(aluation!,prece ence4
24
( %
Barentheses
8valuated first. 3f the parentheses are nested, the e!pression in the innermost part is evaluated first. 3f there is one or more pairs of parentheses on the same level (i.e. not nested , they are evaluated left to right. 8valuated second. 3f there is one or more of this operator on the same level, they are evaluated left to right. 8valuated third. 3f there is one or more of this operator on the same level, they are evaluated left to right. 8valuated fourth. 3f there is one or more of this operator on the same level, they are evaluated left to right. 8valuated fifth. 3f there is one or more of this operator on the same level, they are evaluated left to right. 8valuated si!th. 3f there is one or more of this operator on the same level, they are evaluated left to right. 8valuated last. 3f there is one or more of this operator on the same level, they are evaluated left to right.
8!ponentiation
#egation
G or N
Eo<
-odulus
+ or +
.he following e!ample shows how operators are evaluated in Visual Basic using the order of precedence table given above. Example!5:
G
1
Eo<
3
+
4
N
2
'
5
Example!6:
( a
G
1
+
2
b %
Eo<
5
( b
N
3
, %
G
4
+
6
25