Beruflich Dokumente
Kultur Dokumente
Variable- are placeholders used to store values in Main memory The data types of variables determine how the bit representing these values are stored in the computer memory
IT Department - National Institute of Education
End Show
The first character must be a letter You cannot use a reserved word (word needed by Visual Basic)
Must be unique within the same scope, which is the range from which the variable can be referenced a procedure, a form, and so on.
IT Department - National Institute of Education
End Show
End Show
Cont..
Data type
Description
Range
Byte
Integer
0 to 255
32,768 to 32,767
Long
4-byte integer
2,147,483,648 to 2,147,483,647
End Show
Cont..
Single
Negative values
3.402823E38 to 1.401298E 45 Positive values
1.401298E 45 to 3.402823E38
End Show
Cont..
Double
8-byte floating-point number Range Negative values 1.79769313486231E308 to 4.94065645841247E 324 Positive values 4.94065645841247E 324 to 1.79769313486231E308
IT Department - National Institute of Education
End Show
Cont..
Currency
End Show
Cont..
String String of characters characters Zero to approximately two billion
Variant , Date/time, floating-point number, integer, string, or object. 16 bytes, plus 1 byte for each character if a string value.
End Show
Cont..
Boolean Date
2 bytes 8-byte
True or False date/time value January 1, 100 to December 31, 9999 Any Object reference
Object
4 bytes
End Show
Cont..
Data Types
End Show
Variable declaration
There are three ways for a variable to be declared Default Implicit Explicit
End Show
Implicit Declaration
Visual Basic automatically creates a variable with that name, which you can use as if you had explicitly declared it. While this is convenient, it can lead to subtle errors in your code if you misspell a variable name. For example, suppose this was the function you wrote:
End Show
Cont..
You don't have to declare a variable before using it.
Private Sub Command1_Click() Quantity = 20 SalesPrice = 200.00 Tax = 50 Total = Quantity * SalesPrice + Txa Print "Quantity", "SalesPrice", "Total Price" Print Quantity, SalesPrice, Total End Sub
End Show
Explicit Declaration
There are many advantages to explicitly typing variables
we ensure all computations are properly done, mistyped variable names are easily spotted Visual Basic will take care of ensuring consistency in upper and lower case letters used in variable names. Because of these advantages,it is a good programming practice that declares all variables as explicit
End Show
Cont..
To avoid the problem of misnaming variables, declared explicitly as a variable To explicitly declare variables Place this statement in the Declarations section of a class, form, or standard module: Option Explicit
End Show
Cont..
From the Tools menu, choose Options, click the Editor tab and check the Require Variable Declaration option. This automatically inserts the Option Explicit statement in any new modules,
but not in modules already created; therefore, you must manually add Option Explicit to any existing modules within a project.
End Show
Variable scope
To explicitly type a variable, you must first determine its scope. There are four levels of scope:
Procedure level Procedure level, static Form level module level
End Show
End Show
Within a procedure, variables are declared using the Dim statement: Using Suffix Characters
Dim MyInt%
Dim MyDouble#
End Show
0
Quantity
?
Name
End Show
Quantity
Saman Name
End Show
Quantity
23
Saman Name
Quantity =23
End Show
Quantity
23
Saman Name
End Show
Memory
17
Sub
Y
End Show
Memory
19
Sub
Y
End Show
Variable Scope To make a procedure level variable retain its value upon exiting the procedure, replace the Dim keyword with Static: Static MyInt as Integer Static MyDouble as Double
End Show
Variable Scope
Form level variables retain their value and are available to all procedures within that form. Form level variables are declared in the declarations part of the general object in the form's code window. The Dim keyword is used:
End Show
End Show
Operators
1. Arithmetic. Operators 2. String-Concatenate Operators
3. Comparison Operators
4. Logical Operators
End Show
Arithmetic. Operators
Operator ^ */ \
Mod
+-
Modulus
Addition and subtraction
End Show
Operator precedence
If an expression is written 16+20*4-18/3 how would it be evaluated? There is a set rule to be followed
1.All operations are carried out left to right. 2.All operators have associated hierarchies that determine the order of precedence in evaluating the expression.
Unary
High
*, /, MOD,
+,IT Department - National Institute of Education
lowest
End Show
y=6*4 /3*2
y=8*2
Evaluated Third
y=16
End Show
Print 21 + 2
Print 21 - 2 Print 21 / 2 Print 21 \ 2 Print 21 Mod 2
Print 2 ^ 2
Print 8 ^ 2 / 3
Print 8 ^ (2 / 3)
End Sub
IT Department - National Institute of Education
End Show
End Show
Comparison Operators
There are six comparison operators in Visual Basic: Operator Comparison > Greater than < Less than >= Greater than or equal to <= Less than or equal to = Equal to <> Not equal to The result of a comparison operation is a Boolean value (True or False).
IT Department - National Institute of Education
End Show
Private Sub Command1_Click() Print 3 = 4 Print 3 > 4 Print 3 < 4 Print 3 >= 3
Print 3 <= 4
Print 3 <> 4 End Sub
IT Department - National Institute of Education
End Show
String-Concatenate Operators
String -Concatenate Operators To concatenate two strings, use the & symbol or the + symbol: lblTime.Caption = "The current time is" & Format(Now, hh:mm)
End Show
End Show
Logical Operators
End Show
True
End Show
Sub Routine1()
Dim A as Double End Sub Sub Routine2() Static B as Double . . End Sub
Sub Routine3()
Dim C as Double
Procedure Routine2 has access to X, Y, and B (retains value) Procedure Routine3 has access to X, Z, and C (loses value upon termination) End Show