• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » VB » Variables

Variable’s Scope and Lifetime of a Variable

By Dinesh Thakur

A Variable’s Scope : The scope of a variable is the section of the application that can see and manipulate the variable. If a variable is declared within a procedure, only the code in the specific procedure has access to that variable. When the variable’s scope is limited to a procedure it’s called local.

 

e.g.

Private Sub Command1_Click()

Dim i as Integer

Dim Sum as Integer

For i=0 to 100 Step 2

Sum = Sum +i

Next

MsgBox “ The Sum is “& Sum

End Sub

A variable whose value is available to all procedures within the same Form or Module are called Form-wide or Module-wide and can be accessed from within all procedures in a component.  In some situations the entire application must access a certain variable. Such variable must be declared as Public.

Lifetime of a Variable : It is the period for which they retain their value. Variables declared as Public exist for the lifetime of the application. Local variables, declared within procedures with the Dim or Private statement, live as long as the procedure.

You can force a local variable to preserve its value between procedure calls with the Static keyword. The advantage of using static variables is that they help you minimize the number of total variables in the application.

Variables declared in a Form outside any procedure take effect when the Form is loaded and cease to exist when the Form is unloaded. If the Form is loaded again, its variables are initialized, as if it’s being loaded for the first time.

What are Constants

By Dinesh Thakur

Some variables don’t change value during the execution of program. These are constants that appear many times in your code. The constants are preferred for two reasons :

 

• Constants don’t change value

• Constants are processed faster than variables

The general form to declare a constant is as follows :

Const constantname [As type] =value

The As type part of the declaration is optional. If you omit it, the constant’s type is determined by the value you assign to it. Constants also have a scope and can be Public or Private.

e.g. Public Const pi as Double =3.14159265358979

Visual Basic uses constants extensively to define the various arguments of its methods and the settings of the various control properties.

How variables are declared in Visual Basic

By Dinesh Thakur

Variables are used to store values during a program’s execution. So, variables are place values in which you can leave values and recall them at will.

 

Declaring Variables : In most programming languages, variables must be declared in advance for the compiler.

Explicit Declarations : To declare a variable, use the Dim statement followed by the variable’s   name and type as follows :

 

o Dim Meters as Integer

 

o Dim Names as String Implicit Declarations : When Visual Basic meets an undeclared variable name, it creates a new variable on the spot and uses it. The new variable’s type is variant, the generic data type that can accommodate all other data types.

The reserved word Dim is really short for dimension, which means ‘Size’. When you declare a variable, the amount of memory reserved depends on its data type. In the Dim statement [as data type] is optional. If you omit that statement the default data type is applied to the variable.

 

Dim var1, var2

var1=”Thank you”

var2=43.23

 

The var1 variable is a string variable, and var2 is a numeric one.

 

Types of Variables 

Integer:

Integer data type can store any whole numbers with in the range -32,768 to 32,767.

 

String:

String is also the most commonly used data type which can store Alphabetical data like letters, digits, other characters.

 

Variant:

Variant data type is default data type which can store the any kind of data like Integer, String, Object etc.

 

Currency:

Currency stores Decimal fractions, such as dollars and cents.

 

Date:

The date is also a data type which stores eight-character date.

 

Double:

Double-precision floating-point numbers with 14 digits of accuracy.

 

Long:

Larger whole number.

 

Object : An object variable refers to one of Visual Basic’s many objects, and you can use an object variable to access the actual object.

 

Variant : This is the most flexible data type because it can accommodate all other types.

 

 

What effect does the location of a Dim statement has on the variables is declares

By Dinesh Thakur

1. There are several ways of declaring the variable, the most commonly used statement is Dim statement to declare the variable. The syntax for declaring the variable using Dim is as follows:

 

Dim Identifier [As Data type]

 

2. The reserved word Dim is really short for dimension, which means ‘Size’. When you declare a variable, the amount of memory reserved depends on its data type.

 

3. In the Dim statement [as data type] is optional. If you omit that statement the default data type is applied to the variable.

 

4. Example for using Dim statement:

 

Dim strName as String                       ‘Declared the String variable

Dim intCounter as Integer                   ‘Declared an Integer variable

Dim curDiscount as Currency            ‘Declared a currency variable

Dim vntChanging                                ‘Default variant data type

 

What is Purpose of the Val Function

By Dinesh Thakur

1. Visual Basic provides number of built in functions. The Val function is one of it. Val function converts the text data into a numeric value.

 

The general form of Val function is as follows:

Val(Expression to Convert)

 

2. The expression you wish to convert can be the property of control, a variable, or a constant.

 

3. A function can not stand by itself. It returns (produces) a value that can be used a part of a statement, such as the assignment statements in the following examples.

 

4. When the Val function converts as argument to numeric, it begins at argument’s left most character. If that character is a number digit, decimal point, or sign, Val converts the character to numeric and moves to the next character. As soon as a nonnumeric character is found, the operation stops.

 

5. Example: Val(123.45) will return = 123.45 and Val(123A5) will return 123 only.

 

Explain Order of Precedence of Operators for Calculations

By Dinesh Thakur

1. The order in which operator are performed determines the result. Consider the expressions 3 + 4 * 2. Over here if addition is done first, the result is 14. However, if multiplication is done first then result is 11.

 2. The hierarchy of operations, or order of operations, or order of precedence, in arithmetic expressions from highest to lowest is:

 

a. Exponentiation

b. Multiplication and Division

c. Addition and Subtraction

 

3. In the previous example, the multiplication is done before addition, and the result is 11. to change the order of evolution, use parenthesis:

(3 + 4) * 2 will yield 14 as the result. One set of parenthesis can be used inside another set. In the case, parenthesis are said to be nested.

Explain Boolean variable test for True and False. Give example

By Dinesh Thakur

In Visual Basic there are some controls properties that return you a Boolean value which can be as condition in the testing. For example, option buttons returns true when they are selected and returns you false when they are not.

 

These Boolean variables can be tested as follows:

 

If optFreshman.Value = True Then

mintFreshmanCount = mintFreshmanCount + 1

ElseIf optSophomore.Value = True Then

mintSophoreCount = mintSophoreCount + 1

End If

 

Explain purpose of relational operators and logical operator

By Dinesh Thakur

Relational operators: Relational operators are used for comparison of to variables of same data type. These operators are used in the conditions where two values are compared and true or false is returned according to test result.

Example: 3 > 5 returns you false and 3 < 5 returns you true, which can be used in if statement.

 

Logical operators: Logical operators are used write a compound condition. You can use compound conditions to test more than one condition. Create compound condition by joining two conditions with Logical Operators. The Logical Operators are Or, And and Not.

 

Example:

 

If optMale.Value = True And Val(txtAge.Text) < 21 Then

minMinorMaleCount = minMinorMaleCount + 1

End If

What is the general format of the statement used decisions in an application

By Dinesh Thakur

1. A power full asset of the computer is its ability to make decisions and to take alternate course of action based on the out come.

 

2. A decision made by the computer is formed as a question: is a given condition is true of false.

 

3. Decision can be taken using If statement which is used as follows:

 

If the sun is shining Then                    (Condition)

Go to beach                                        (Action to take)

Else

Go to Class                                         (Action if Condition is false)

End If

 

4. In If statement, when the condition is true, only Then clause is executed. When condition is false, only the Else clause, if present is executed.

 

If… Then statement general form is

 

If (Condition) Then

Statement(s)

Else

Statement(s)

End If

 

5. A block If … Then … Else must always include with End If. The word Then must appear on the same line as the If with nothing following Then. End If and Else clauses are indented for readability and clarity.

What is the Continuation operator ? When is it used

By Dinesh Thakur

When a Basic statement become too long for one line , use a line-Continuation

character.You can type a space and an underscore , press Enter, and continue the statement on the next line . It is OK to indent the continued lines.The only restriction is that the line-continuation character must appear between elements; you cannot place a continuation in the middle of a literal or split the name of an object or property.

 

Example:

 

lblGreetings.Caption = ” Greetings ” & txtName.Text & ” : ” & ” You have been selected to win a free prize.”

 

Primary Sidebar

Visual Basic Tutorial

Visual Basic Tutorial

  • VB - File Types Purpose
  • VB - Events
  • VB - Ole
  • VB - Controls Properties
  • VB - Project Steps
  • VB - Pretest Vs Posttest
  • VB - IDE
  • VB - Record Set
  • VB - Common Dialog Box
  • VB - Val Function
  • VB - Sub Vs Function Procedure
  • VB - Terms
  • VB - User Interface Elements
  • VB - Objects and Modules
  • VB - Variable’s Scope
  • VB - Message Box
  • VB - Data Vs Data-bound Control
  • VB - Branching Statements
  • VB - Do/Loop and For-Next Loop
  • VB - Grid Control
  • VB - Error Types
  • VB - Looping Constructs
  • VB - Relational Vs Logical Operator
  • VB - Control Purpose

Other Links

  • Visual Basic - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW