Você está na página 1de 27

A variable is simply a temporary location in memory that is used to store a piece of data or a reference.

Every variable has three properties:


Name - reference to the location - cannot be changed Value - the information that is stored - can be changed during program execution, hence the name variable Data Type - the type of information that can be stored cannot be changed

Data type - Specifies type of data variable can store Integer variables: Long, Integer, Short, Byte Floating-point variables: Single, Double Fixed decimal point variable: Decimal Boolean variables: True, False Character variable: Char Text variable: String The Object variable
Default data type assigned by Visual Basic Can store many different types of data Less efficient than other data types

Data type

Prefix

Size

Values

Byte Short Integer Long Single Double Decimal


Char Boolean String

byt sht int lng sng dbl dec


chr bln str

1 2 4 8

byte byte byte byte

positive integer value from 0 to 255 integer from 32,768 to +32,767 integer from +/- 2,147,483,647 integer from +/- 9,223,372,036,854,775,807

4 byte single-precision, floating-point number 8 byte double-precision, floating-point number 16 byte number with up to 28 significant digits
2 byte 2 byte Any single character True or False

(4 byte) Text - Any number/combination of characters

Variable are declared with one of four possible level scope:

(Declaration section)

Dim ModuleLevelVariable Const NamedConstant Private Sub calculateButton_Click Dim LocalVariables .. End Sub Private Sub summarybutton_Click Dim LocalVariables .. End Sub

A variable declaration is a statement that creates a variable in memory Syntax: Dim VariableName As DataType
Dim (Dimension) - keyword VariableName - name used to refer to variable As - keyword DataType - one of many possible keywords to indicate the type of value the variable will contain

Example:

Dim intLength as Integer

A starting or initialization value may be specified with the Dim statement Good practice to set an initial value unless assigning a value prior to using the variable Syntax: Dim VariableName As DataType = Value
Example: Dim intLength as Integer = 5

Just append " = value to the Dim statement = 5 assigning a beginning value to the variable

Variable MUST be declared prior to the code where they are used Variable should be declared first in the procedure (style convention)
Declaring an initial value of the variable in the declaration statement is optional
Refer to default values (next slide)

Data type

Default (Initial) value

All numeric types Boolean Char String or Object Date

Zero (0) False Binary 0 Empty 12:00 a.m. on January 1, 0001

Actual value/data/information Similar to a variable, but can NOT change during the execution of a program. Examples of Literals:
Numeric: 5 ; 157 ; 195.38256 String: Paul ; Hello!!! ; Jackson, AL 36545 Char: a ; 1 ; ? ; @ Boolean: True ; False

Dim is the same as Private and is uses at module-level variable. However, when declaring a variable inside a procedure, the use of the modifier Private is not allowed. The use of Dim is required if the variables are declared inside a procedure (Sub Main). Many developers use the scope modifier Dim when declaring all variables (except variables that are required to be public).

A constant is declared by the keyword Const. Example:


Private Const STRTITLE As String = My Application

The name portion of the constant is uppercase. After a constant is declared, its value cannot be changed anywhere in your code.

Syntax: Const CONST_NAME As DataType =

Value

Looks like a normal declaration except: Const used instead of Dim An initialization value is required By convention, entire name capitalized with underscore characters to separate words

constants: Like variables, constant names must start with a letter or the underscore character. Constant names cannot exceed 255 characters. When declaring a constant inside a procedure, do not include a modifier (Public/Private). When declaring a module-level constant, use the Private modifier. The use of Dim is not allowed.

Following are the rules for declaring

Variable cannot be used before it is declared Variable declared within a code block is only visible to statements within that code block Called Local Variable Can be declared at the beginning of the class code window (General Declarations section) and be available to all blocks Called Form Level Variable Variables that share the same scope cannot have the same name (same name ok if different scope)

Arithmetic Operators
^ * / \ MOD + & = <> < > >= <= Exponential Multiplication Floating Point Division Integer Division Modulus (remainder from division) Addition Subtraction String Concatenation (putting them together) Equal to Not Equal to Less than Greater than Less than or equal to Greater than or equal to

Examples of use:
decTotal = decPrice + decTax decNetPrice = decPrice - decDiscount dblArea = dblLength * dblWidth sngAverage = sngTotal / intItems dblCube = dblSide ^ 3

The backslash (\) is used as an integer division operator The result is always an integer, created by discarding any remainder from the division Example
intResult = 7 \ 2 result is 3 shrHundreds = 157 \ 100 result is 1 shrTens = (157 - 157 \ 100 * 100) \ 10 result is ?

This operator can be used in place of the backslash operator to give the remainder of a division operation
intRemainder = 17 MOD 3 dblRemainder = 17.5 MOD 3
result is 2 result is 2.5

Any attempt to use of the \ or MOD operator to perform integer division by zero causes a DivideByZeroException runtime error

Concatenate: connect strings together Concatenation operator: the ampersand (&) Include a space before and after the & operator Numbers after & operator are converted to strings How to concatenate character strings
strFName = "Bob" strLName = "Smith" strName = srFName & " Bob strName = strName & strLName Bob Smith intX = 1 intY = 2 intResult = intX + intY strOutput = intX & + & intY & = & intResult

1 + 2 = 3

Often need to change the value in a variable and assign the result back to that variable For example: var = var 5 Subtracts 5 from the value stored in var
Operator Usage
+= -= *= /= \= &= x x x x x x += 2 -= 5 *= 10 /= y \= y &= .

Equivalent to
x x x x x x = = = = = = x x x x x x +2 5 * 10 /y \y & .

Effect
Add to Subtract from Multiply by Divide by Int Divide by Concatenate

Operator precedence tells us the order in which operations are performed From highest to lowest precedence: Exponentiation (^) Multiplicative (* and /) Integer Division (\) Modulus (Mod) Additive (+, -) Concatenation (&) Comparison (=,<>,<,>,<=,>=) Parentheses override the order of precedence Where precedence is the same, operations occur from left to right

Parenthesis Exponential Multiplication / Division Integer Division MOD Addition / Subtraction String Concatenation Relational Operators (< , > , >= , <= , <>) Logical Operators (AND, OR, NOT)

1.
2. 3. 4. 5. 6.

4 + 30 \ 2 * 5 = 2+4*2/4^2= 3 + ((4 * 2) /(4 ^ 2)) = 6*2^3+4/2= 7*4/26= 5 * (4 + 3) 15 Mod 2 =

intX = 10 intY = 5 7. intResultA = intX + intY * 5 8. iResultB = (intX + intY) * 5 9. dResultA = intX - intY * 5 10. dResultB = (intX - intY) * 5

'iResultA is 'iResultB is 'dResultA is 'dResultB is

1.
2. 3. 4. 5. 6.

4 + 30 \ 2 * 5 = 79 2 + 4 * 2 / 4 ^ 2 = 2.02 3 + ((4 * 2) /(4 ^ 2)) = 3.02 6 * 2 ^ 3 + 4 / 2 = 12002 7*4/26=8 5 * (4 + 3) 15 Mod 2 = 34

intX = 10 intY = 5 7. intResultA = intX + intY * 5 8. iResultB = (intX + intY) * 5 9. dResultA = intX - intY * 5 10. dResultB = (intX - intY) * 5

'iResultA is 35 'iResultB is 75 'dResultA is -15 'dResultB is 25

Você também pode gostar