0% found this document useful (0 votes)
51 views42 pages

VARIABLES IN VISUAL BASIC New

notes

Uploaded by

gladys temwo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views42 pages

VARIABLES IN VISUAL BASIC New

notes

Uploaded by

gladys temwo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

VARIABLES IN VISUAL BASIC

A variable is a location in memory where a value can be stored


during the execution of a Visual Basic application. Visual Basic
variables are assigned names by the programmer when they are
declared so that they can easily be referenced in other places in
the application code.

 datacan be stored as variables or constants. Variables are like


mailboxes in the post office. The content of the variable changes
every now and then, just like the mailboxes.
 VisualBasic is a Strongly Typed programming language. Before
we perform any operation on a variable, it’s mandatory to define
a variable with a required data type to indicate what type of data
VARIABLES

A variable has a name and a value. In addition to


a name and a value, variables have a data type,
which determines what kind of values we can
store to a variable.
RULES WHEN NAMING VARIABLES

A variable name must begin with an alphabet.


 It cannot be more than 255 characters.
 The variable name must not contain any special
character like %,&,!,#,@ or $.
 And finally, it has to be unique within the same
scope.
 Dim [Variable Name] As [Data Type]


Dim [Variable Name] As [Data Type] = [Value]
 Dim It is useful to declare and allocate the storage space for one or more variables.
 [Variable Name] It’s the variable's name to hold the values in our application.
 As The As clause in the declaration statement allows you to define the data type.

 [Data Type] t’s a type of data the variable can hold, such as integer, string,
decimal, etc.

 [Value] Assigning a required value to the variable.


DECLARING VARIABLES IN VB 6.0
 You declare a variable by using the Dim keyword:

 Dim RandomNumber As Integer

 If you do not specify a variable type the variable will default to


Variant:

 Dim Foo

 is equivalent to

 Dim Foo As Variant


DECLARING VARIABLES IN VB
 You can declare a variable with the 'dim' keyword.

 Syntax:
 Dim variable As [Type]

 Example:
 Private Sub cmdSum_Click()
 Dim m As Integer
 Dim n As Integer
 Dim sum As Integer

 m = 10 'm is a variable, 10 is a constant


 n = 30
CONT’

 sum = m + n

 Print "The sum is " & sum

 End Sub

 Output: The sum is 40


 Declaring a variable tells Visual Basic to reserve space in memory. It is not must
that a variable should be declared before using it.
 Automatically whenever Visual Basic encounters a new variable, it assigns the
default variable type and value. This is called implicit declaration
 Though this type of declaration is easier for the user, to have more control over
the variables, it is advisable to declare them explicitly.
Option Explicit

 This forces the user to declare all the variables. The Option Explicit
statement checks in the module for usage of any undeclared
variables and reports an error to the user. The user can thus rectify
the error on seeing this error message.
 The Option Explicit statement can be explicitly placed in the general
declaration section of each module using the following steps.
 Click Options item in the Tools menu
 Click the Editor tab in the Options dialog box
 Check Require Variable Declaration option and then click the OK
button
Categories of Variables

 Static variables
 They are those variables that don’t change when the program
executes.

 Dynamic variables

 They are those variables that change when the program executes.
Scope of variables

 A variable is scoped to a procedure-level (local) or module-level


variable depending on how it is declared.
 The scope of a variable, procedure or object determines which part of
the code in our application are aware of the variable's existence.
 A variable is declared in general declaration section of a Form, and
hence is available to all the procedures.
 Local variables are recognized only in the procedure in which they
are declared. They can be declared with Dim and Static keywords
Local Variables

 A local variable is one that is declared inside a procedure. This


variable is only available to the code inside the procedure and can be
declared using the Dim statements as follows:
 Dim sum As Integer
 The local variables exist as long as the procedure in which they are
declared, is executing.

 Once a procedure is executed, the values of its local variables are lost
and the memory used by these variables is freed and can be
reclaimed. Variables that are declared with keyword Dim exist only as
long as the procedure is being executed.
Static Variables

 Static variables are not reinitialized each time Visual basic Invokes a
procedure and therefore retains or preserves value even when a
procedure ends.

 In case we need to keep track of the number of times a command


button in an application is clicked, a static counter variable has to be
declared.

 These static variables are also ideal for making controls alternately
visible or invisible. A static variable is declared as given below.
 Static intPermanent As Integer
example of an event procedure for a
Command Button that counts and
displays the number of clicks made.

 Private Sub Command1_Click()


 Static Counter As Integer
 Counter = Counter + 1
 Print Counter
 End Sub
Module Level Variables

 A module level variable is available to all the procedures in the


module.

 They are declared using the Public or the Private keyword.


 Public CustomerName As String ' A Public property
CONSTANTS
 Constant is a fixed value that does not change during
the program execution.

 Creating your own constants



You can create your own constant to use it in your
program.
 The value of the constant remains unchanged
throughout the program.
DECLARING CONSTANTS
 Private Sub Command1_Click()
 Const pi As Double = 3.142
 Dim r As Double
 Dim area As Double
r =7
 area = pi * r * r
 Label1.Caption = area
 End Sub
DATATYPES
 A data type shows the kind information or data a certain variable
stores.
 When a variable is declared, a data type is supplied for it that
determines the kind of data they can store. The fundamental data
types in Visual Basic including variant are integer, long, single,
double, string, currency, byte and Boolean.

 Each data type has limits to the kind of information and the
minimum and maximum values it can hold.

 Visual Basic is a Strongly Typed programming language. Before


we perform any operation on a variable, it’s mandatory to define
a variable with a required data type to indicate what type of data
Variable type declaration

 Dim [Variable Name] As [Data Type]


 Dim [Variable Name] As [Data Type] = [Value]
Numeric datatypes
Store integer values in the range of 0 – 255
Byte used to access binary files, image and sound
files,

Stores whole numbers ie non-floating numbers .


Integer Store integer values in the range of (-32,768) - (+
32,767)
stores whole numbers e.g counts, quantities, and
so on.
 Long
 Store integer values in the range of (- 2,147,483,468) - (+ 2,147,483,468) used
when you need to hold an integer larger than the Integer data type can
hold
 Single
 Store floating point value in the range of (-3.4x10-38) - (+ 3.4x1038) , used to
contain floating-point values that do not require the full data width of
Double
 Double
 Store large floating value which exceeding the single data type value. stores
numeric values that have a decimal place
 Currency
 store monetary values. It supports 4 digits to the right of decimal point and 15
digits to the left . useful for calculations involving money and for fixed-
point calculations in which accuracy is particularly important.
Datatypes
 2. String
 Use to store alphanumeric values. A variable length string can store
approximately 4 billion characters. Used to represent a sequential
collection of characters that is called a text.

 3. Date
 Used to store date and time values. A variable declared as date type can
store both date and time values and it can store date values 01/01/0100 up
to 12/31/9999. used to contain date values, time values, or date and
time values
Datatypes cont’
 4. Boolean
 Boolean data types hold either a true or false value.
These are not stored as numeric values and cannot
be used as such. Values are internally stored as -1
(True) and 0 (False) and any non-zero value is
considered as true.

 5. Variant
 Stores any type of data and is the default Visual
Basic data type. In Visual Basic if we declare a
variable without any data type by default the data
type is assigned as default.
Operators in Visual Basic

 Arithmetic operators
Operator Description Example (a = 6, b = 3)

It will add two


+ a+b=9
operands.
It will subtract two
- a-b=3
operands.
It will multiply two
* a * b = 18
operands.
It divides two
numbers and
/ a/b=2
returns a floating-
point result.
It divides two
numbers and
\ a\b=2
returns an integer
result.
It divides two
numbers and
Mod a Mod b = 0
returns only the
remainder.
Assignment operators
Operator Description Example
It will assign a value to a
= a = 10
variable or property.
It will add left and right
a += 10 equals to a = a +
+= operands and assign a
10
result to the left operand.

It will subtract left and right


-= operands and assign a a -= 10 equals to a = a - 10
result to the left operand.

It will multiply left and right


*= operands and assign a a *= 10 equals to a = a * 10
result to the left operand.
It will divide left and right
operands and assign the
/= a /= 10 equals to a = a / 10
floating-point result to the
left operand.
Relational operator

 Use of conditional operators


 They let the VB program compare data values
and then decide what action to take, whether to
execute a program or terminate the program/
 In Visual Basic, Comparison Operators are
useful to determine whether the defined two
operands are equal, greater than or less than,
etc., based on our requirements.
Example (a = 10, b
Operator Description
= 5)

It will return true if the


right operand is
< a < b = False
greater than the left
operand.
It will return true if the
right operand is
<= a <= b = False
greater than or equal
to the left operand.
It will return true if the
left operand is greater
> a > b = True
than the right
operand.
It will return true if the
left operand is greater
>= a >= b = True
than or equal to the
right operand.
Comparison operators cont’

It will return true if


= both operands are a = b = False
equal.
It will return true if
<> both operands are not a <> b = True
equal.
It will return true if
two object references
Is
refer to the same
object.
It will return true if
two object references
IsNot
 Private Sub Command3_Click()
 Dim a As Integer, b As Integer
 a = 10
 b=3
 If (a > b) Then

 Print "True"
 Else
 Print "False"
 End If
 Logical operators
LOGICAL OPERATORS
Example (a = True, b =
Operator Description
False)
It will return true if both
And a And b = False
operands are non zero.
It will return true if any one
Or operand becomes a non a Or b = True
zero.

It will return the reverse of


a logical state that means if
Not Not(a And b) = True
both operands are non
zero, it will return false.
It will return true if any one of
expression1 and expression2
evaluates to true. It returns True if
Xor both expressions are True or both a Xor b = True
expressions are False; otherwise it
returns False.
OR

 Private Sub Command1_Click()


 Dim a As Boolean, b As Boolean, y As Boolean
 a = True
 b = False
 y = a Or b
 Print y

 End Sub
AND

 Private Sub Command1_Click()


 Dim a As Boolean, b As Boolean, y As Boolean
 a = True
 b = False
 y = a And b
 Print y

 End Sub
Not

 Private Sub Command1_Click()


 Dim a As Boolean, b, y As Boolean

 a = True
 b = False
 y = Not a
 Print y
 End sub
XOR
 Private Sub Command1_Click()
 Dim a As Boolean, b As Boolean, y As Boolean
 a = True
 b = False
 y = a Xor b

 Print y

 End Sub
concatenation operators

The following table lists the different types of concatenation operators available in Visual Basic.

Example (a = Hello, b =
Operator Description
World)

It will concatenate given


& a & b = HelloWorld
two expressions.

It is useful to add two


+ numbers or concatenate a + b = HelloWorld
two string expressions.
Concatenation Operators
 useful to concatenate defined operands based on our requirements.
 & It will concatenate given two expressions. a & b = HelloWorld
 + It is useful to add two numbers or concatenate two string expressions. a + b =
HelloWorld
Vb comments
 In Visual Basic, Comments are the self-explanatory notes to provide
detailed information about the code which we wrote in our applications.

 By using comment symbol ('), we can comment on the code in our Visual
Basic programming. The comment symbol (') will tell the Visual Basic
compiler to ignore the text following it, or the comment.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy