Anda di halaman 1dari 3

Chapter 3 Studyguide - Variables and Constants…………part 1

1. There are 2 types of identifiers used in programming (VB is no exception):


variables and named constants.

2. Identifiers:
• must begin with a letter and contain only letters, numbers, and some special
characters. Legal VB.NET identifiers for use as variable AND constant names
must begin with a letter and contain only letters, digits, and the underscore (_)
character. Periods, spaces and other special characters are NOT allowed.
• are NOT case sensitive, which means that an uppercase letter is the SAME as
a letter in lowercase (ex: Count/count/COUNT all refer to the same
variable).The IDE automatically changes the case of an identifier to MATCH
that of the first occurrence. Specific “conventions” for variable identifiers and
named constant identifiers will be addressed below.
• cannot be keywords themselves (see complete list p. 76)…because these
keywords have special meaning to the VB compiler. (EX: Integer would
NOT be an acceptable identifier, since it is a keyword)

3. For variable or named constant identifiers, we will need to let the computer know
what “type” of data we will be storing in these locations. The following table will
outline the types used: This data has been compiled from multiple textbooks. The
columns in bold reflect the information you MUST memorize: READ p. 4-6 and 4-7
Type Used to Layman’s Bytes of Default Prefix for
represent terms used to storage value identifiers of this
represent initialized to type, IF USED
Short Integers from Whole numbers 2 0 srt
-32,768 to 32,767 with the smallest
range
Integer Integers from Whole numbers 4 0 int
-2,147,483,648 to (used most
2,147,483,647 often…for those
who don’t want
to think about
it!!!)
Long Large integers Whole numbers 8 lng
(no decimals) with the biggest 0
range
Single Numbers possible Number with a 4 0 sng
containing a decimal point
decimal with the smallest
range
Double Positive or Number with a 8 0 dbl
negative numbers decimal point
that may contain with the largest
a decimal in range
range
-1.8E+308 to
1.8D+308

1
Type Used to Layman’s Bytes of Default Prefix for
represent terms used to storage value identifiers of this
represent initialized to type, IF USED

Decimal Numbers that Used to store 16 0 dec


may have many currency
significant digits

Date A date in Store a date 8 12:00:00 AM dtm


m/d/yyyy format
Char A single Store 1 2 “” or Nothing chr
character character only
String A set of Store many 2 per “” or Nothing str
characters…ie. 1 characters (0 or character
or more more…a string
characters can be empty)
(actually 0
characters too!)
Boolean True or False 2 False bln

4. A variable is a name for a value stored in memory. Variables are used in programs
so that values can be represented with meaningful names.
• Think of a variable as the “ADDRESS” to a mailbox, think of the “stuff” you put
into it (either with an assignment statement or reading data into it) as the “MAIL”;
ie. The variable is the address of the location in memory. NOTE: in machine
language, the address would be represented as 0 and 1s, versus a “NICE,
DESCRIPTIVE NAME” like we use in a high level language, like Visual Basic.
• A variable must be declared before it is used. A declaration statement must take
this form: dim variablename as type
• .In particular,. The convention on naming variables varies from book to book or
company to company, but in agreement: All variables should be descriptive.
Typically variable identifiers begin with a lowercase letter and each word after the
first should begin with an uppercase letter.(ex:rectangleLength) Your book states
that not only should variable identifiers be descriptive, but that they also should
begin with an appropriate prefix, as outlined in the table above(ex:sngCircleArea).
I will NOT require this convention in MY labs…but you may do so if you like!
• Multiple variables with the same data type can be declared in a single statement,
according to: dim var1,var2 as type
However, grouping variables together in a single statement is good programming
style WHEN the variables represent related items. Declarations should NOT be
grouped together in the same statement JUST BECAUSE the variables are all the
same type.
ACCEPTABLE EXAMPLE: dim grade1,grade2 as integer
UNACCEPTABLE EXAMPLE: dim grade1, count as integer

2
• Variable declarations SHOULD be grouped at the beginning of a procedure. A
blank line after the declarations makes it easy to determine where the declarations
end.

5. A constant is a name for a memory location that stores a value that CANNOT be
changed from its initial assignment. TRYING to change the value of a constant after
the initial assignment generates an error.
• A named constant must be declared (just like a variable) before it is used. A
declaration statement must take this form: const constantname as type = value
• .In particular,. The convention on naming constant identifiers varies from book
to book or company to company, but in agreement: All constant identifiers
should be descriptive. Typically constant identifiers are ALL uppercase and may
include underscore (_) characters to separate words (ex:MAX_PRICE) Your book
states that not only should variable identifiers be descriptive, but that they also
should begin with an appropriate prefix, as outlined in the table
above(ex:sngCircleArea). I will NOT require this convention in MY labs…but
you may do so if you like!
• The placement of constant declarations determines their scope. Because the
value of a constant does not change throughout a program run, it is usually safe to
allow a broader scope. Therefore, constant declarations are OFTEN placed in the
Form class, outside any procedures. Wherever constant declarations are placed,
they SHOULD be grouped and declared before any variable declarations.
This will be required for your labs!
Example:
const PI as double = 3.14

dim radius as integer


dim area as single

Written by Ms. Davis


Please pass on any comments for changes

Anda mungkin juga menyukai