Вы находитесь на странице: 1из 41

Variables & Data Types

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

Naming a Variable No more than 255 characters


They may include only letters, numbers, and underscore (_)

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

Cont.. Data Types


Data Type Boolean Integer Long (Integer) Single (Floating) Double (Floating) Currency Date Object String Variant Suffix None % & ! # @ None None $ None

IT Department - National Institute of Education

End Show

Cont..

Data type

Description

Range

Byte
Integer

1-byte binary data


2-byte integer

0 to 255
32,768 to 32,767

Long

4-byte integer

2,147,483,648 to 2,147,483,647

IT Department - National Institute of Education

End Show

Cont..
Single

Single 4-byte floating-point number Range

Negative values
3.402823E38 to 1.401298E 45 Positive values

1.401298E 45 to 3.402823E38

IT Department - National Institute of Education

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

8-byte number with fixed decimal point


Range 922,337,203,685,477.5808 to 22,337,203,685,477.5807

IT Department - National Institute of Education

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.

IT Department - National Institute of Education

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

IT Department - National Institute of Education

End Show

Cont..
Data Types

Constant a fixed value Eg PI as constant declaration

IT Department - National Institute of Education

End Show

Variable declaration

There are three ways for a variable to be declared Default Implicit Explicit

IT Department - National Institute of Education

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:

IT Department - National Institute of Education

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

IT Department - National Institute of Education

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

IT Department - National Institute of Education

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

IT Department - National Institute of Education

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.

IT Department - National Institute of Education

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

Local Level Global Level

IT Department - National Institute of Education

End Show

Within a procedure, variables are declared using the Dim statement:


Dim NoOfStudent as Integer Dim Salary as Double Dim StName, StCity as String

Note :- StName is taken as Variant type and StCity is taken as String


IT Department - National Institute of Education

End Show

Within a procedure, variables are declared using the Dim statement: Using Suffix Characters
Dim MyInt%
Dim MyDouble#

Dim MyString$, YourString$

IT Department - National Institute of Education

End Show

Private Sub Command1_Click(); Dim Quantity as Integer Dim Name as String

0
Quantity

?
Name

IT Department - National Institute of Education

End Show

Private Sub Command1_Click(); Dim Quantity as Integer Dim Name as String


Name =Saman

Quantity

Saman Name

IT Department - National Institute of Education

End Show

Private Sub Command1_Click(); Dim Quantity as Integer Dim Name as String


Name =Saman

Quantity

23

Saman Name

Quantity =23

IT Department - National Institute of Education

End Show

Private Sub Command1_Click(); Dim Quantity as Integer Dim Name as String


Name =Saman

Quantity

23

Saman Name

Quantity =23 End sub

IT Department - National Institute of Education

End Show

Private Sub Command1_click(); Dim X as Integer


Dim Y as Integer
X=17 Y=5 X=X+2
END

Memory

17

Sub

Y
End Show

IT Department - National Institute of Education

Private Sub Command1_click(); Dim X as Integer


Dim Y as Integer
X=17 Y=5 X=X+2
END

Memory

19

Sub

Y
End Show

IT Department - National Institute of Education

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

IT Department - National Institute of Education

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:

Dim MyInt as Integer Dim MyDate as Date


IT Department - National Institute of Education

End Show

Variable Scope Global level variables


Global level variables retain their value and are available to all procedures within an application. Module level variables are declared in the declarations part of the general object of a module's code window. (It is advisable to keep all global variables in one module.) Use the Global keyword or Public Keyword:

Global MyInt as Integer Public MyDate as Date


IT Department - National Institute of Education

End Show

Operators
1. Arithmetic. Operators 2. String-Concatenate Operators

3. Comparison Operators
4. Logical Operators

IT Department - National Institute of Education

End Show

Arithmetic. Operators

Operator ^ */ \

Operation Exponentiation Multiplication and division Integer division (truncates)

Mod
+-

Modulus
Addition and subtraction

Parentheses around expressions can change precedence.

IT Department - National Institute of Education

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

Evaluated First y=6*4 /3*2 y = 24 / 3 * 2 Evaluated Second

y=8*2

Evaluated Third

y=16

IT Department - National Institute of Education

End Show

Print 8 ^ (2 / 3) Private Sub Command1_Click()

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

Write following expression in VB

1. x2+2x-5 2. 2x2-3x/(y-7) 3. 5x-2/(3x27)

IT Department - National Institute of Education

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

False False True True True True

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)

txtSample.Text = "Hook this + to this

IT Department - National Institute of Education

End Show

Logical Operators Cont. Three logical operators


Operator Not And Or Operation Logical not Logical and Logical or

IT Department - National Institute of Education

End Show

Logical Operators

Three logical operators


The Not operator simply negates an operand.
The And operator returns a True if both operands are True. Else, it returns a False. The Or operator returns a True if either of its operands is True, else it returns a False. Logical operators follow arithmetic operators in precedence
IT Department - National Institute of Education

End Show

Private Sub Command1_Click() Print 3 = 4 Or "A" = "B" False False

Print 3 = 4 And "A" = "B"


Print Not (3 = 4) End Sub

True

IT Department - National Institute of Education

End Show

Variable Scope Module1

*Example of Variable Scope:

Global X as Integer Form1 Form2


Dim Y as Integer Dim Z as Single

Sub Routine1()
Dim A as Double End Sub Sub Routine2() Static B as Double . . End Sub

Sub Routine3()
Dim C as Double

Procedure Routine1 has access to X, Y, and A (loses value upon termination)

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

IT Department - National Institute of Education

Вам также может понравиться