Unit 3
Data Types and Array
Variables in VB.NET
Variables are used to store values in memory during program execution. Each variable has a name,
type, and scope.
A variable is nothing but a name given to a storage area that our programs can manipulate.
Each variable in VB.Net has a specific type, which determines the size and layout of the variable's
memory; the range of values that can be stored within that memory; and the set of operations
that can be applied to the variable.
Data Types
• Data types refer to an extensive system used for declaring variables or functions of
different types. The type of a variable determines how much space it occupies in storage
and how the bit pattern stored is interpreted.
Data Types Available in VB.Net
• VB.Net provides a wide range of data types. The following table shows all the data types
available −
Data Type Storage Allocation Value Range
Boolean Depends on True or False
implementing
platform
Byte 1 byte 0 through 255 (unsigned)
Char 2 bytes 0 through 65535 (unsigned)
Date 8 bytes 0:00:00 (midnight) on January 1, 0001 through 11:59:59 PM
on December 31, 9999
Decimal 16 bytes 0 through +/-79,228,162,514,264,337,593,543,950,335 (+/-
7.9...E+28) with no decimal point; 0 through +/-
7.9228162514264337593543950335 with 28 places to the
right of the decimal
Double 8 bytes -1.79769313486231570E+308 through -
4.94065645841246544E-324, for negative values
4.94065645841246544E-324 through
1.79769313486231570E+308, for positive values
Integer 4 bytes -2,147,483,648 through 2,147,483,647 (signed)
Long 8 bytes -9,223,372,036,854,775,808 through
9,223,372,036,854,775,807(signed)
Object 4 bytes on 32-bit Any type can be stored in a variable of type Object
platform
8 bytes on 64-bit
platform
SByte 1 byte -128 through 127 (signed)
Short 2 bytes -32,768 through 32,767 (signed)
Single 4 bytes -3.4028235E+38 through -1.401298E-45 for negative values;
1.401298E-45 through 3.4028235E+38 for positive values
String Depends on 0 to approximately 2 billion Unicode characters
implementing
platform
UInteger 4 bytes 0 through 4,294,967,295 (unsigned)
ULong 8 bytes 0 through 18,446,744,073,709,551,615 (unsigned)
User- Depends on Each member of the structure has a range determined by its
Defined implementing data type and independent of the ranges of the other
platform members
UShort 2 bytes 0 through 65,535 (unsigned)
Project scope
It indicates how much visibility it has throughout the project; whether it is visible
to procedure where it is declared, throughout a form or module or globally visible.
there are two types of scopes:
• Variable scope
• Procedure scope
Variable scope
variable in VB can be declared in no. of ways. Most commonly ‘Dim’(Dimenssion)
statement is used to declare the variable.
• Dim: using Dim can alone create a variants. Keyword ‘as’ is used to specify
variable type.
• Private: Makes variable available only in the current form or module
• Public: creates global variable- variable is available to the entire program.
• Redim: Reallocates storage space for dynamic array variables.
• Static: Variable preserves its value between procedure calls.
• Type: creates user defined variable type.
The syntax of DIM statement is as follows:
Dim variable name as data type
Where variable name is user defined.
The naming rules for variables in VB are as follows:
• the name must begin with a letter, followed by 0 or more letters, numbers,
and/or underscore characters.
• the name may not contain spaces.
• the name cannot be a keyword.
• E.g. num1, Num_2, I, dkte_123
Examples of Dim statement:
• dim a as integer
Above statement creates an integer variable named as ‘a’
• dim num1, num2 as single
Above statement creates two single variables named as num1 and num2.
• If you do not specify the data type in dim statement then it creates a
variant.
dim a
Above statement creates a variant ‘a’ which can accept any type of data.
Besides Dim other statements like Redim, Private, Public, Static and Type can
also be used.
Procedure Scope:
• As with variable we can also restrict the scope of procedure; it can be done
using private, public, static, friend keywords
➢ Private: it specifies that the subroutine or function is private to the module
or form.
➢ Public: it specifies that the sub or function is global/public to all module or
forms.
➢ Friend: friend procedures are usually used in class modules . They are not
available in standard modules. It allows procedures to be called outside
class but not outside project.
➢ Static: procedures declared using static keyword restricts variables in the
procedure from changing between procedure calls.
Lifetime of variable:
• Period for which variable retains its value is called as lifetime of variable.
➢ Variable declared with public keyword exists for lifetime till the application
is running
➢ Local variables declared within procedure using Dim or private keywords
live as long as procedure , when procedure finishes local variables cease to
exist(die) and allocated memory is returned to the system.
➢ Variables declared using static keyword preserve their values during
procedure calls.
Operator in VB.Net
VB.Net has three types of operators
• Arithmetic operators
• Comparison operators
• Logical Operators
Arithmetic Operators:
Comparison Operators:
Logical Operators:
Constants in VB.NET
Constants store fixed values that do not change during program execution.
e.g.
Const PI As Double = 3.14159
Const CompanyName As String = "ABC Corp"
Array in Vb.Net:
Imagine that you want to store some information about multiple items, e.g. you
want to keep roll numbers of 10 students in memory, or names of 50 users. Perhaps
you realize that there must be an easier way than to start typing variables like
user1,user2,…until user 50 Despite the fact that there may be 1000 of them. How
would go about searching for something in there? Definitely not like that!
• If we need to store a larger amount of variables of the same type, we can
solve this problem using an array.
Definition of an array:
• An array stores a sequential collection of elements of the same type. An array
is used to store a collection of data, an array can be seen as a collection of
variables of the same data type.
• All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
• By using an array, you can refer to these related values by the same name,
and use a number that’s called an index or subscript to identify an individual
element based on its position in the array. The indices of an array range from
0 to one less than the total number of elements in the array. When you use
Visual Basic syntax to define the size of an array, you specify its highest index,
not the total number of elements in the array.
• Each data item is called an element of the array. These elements are
sequentially arranged in computer memory with the first element being
at index 0 and the last element at index n - 1, where n represents the total
number of elements in the array.
• The following is an illustrated representation of similar data type elements
defined in the VB.NET array data structure.
Types of Array:
• Arrays can be defined as fixed and dynamic or one dimensional and
multidimensional
Dimension of an Array
• An array can be one-dimensional or multidimensional. A one-
dimensional array is like a list of items or a table that consists of one
row of items.
• A two-dimensional array is a table of items that make up of rows
and columns. The format for a one-dimensional array is
ArrayName(x), the format for a two dimensional array is
ArrayName(x,y) and a three-dimensional array is ArrayName(x,y,z)
. Normally it is sufficient to use a one-dimensional and two-
dimensional array, you only need to use higher dimensional arrays
if you need to deal with more complex problems.
Declaring Array
• We can use Public or Dim statement to declare an array just as the
way we declare a single variable. The Public statement declares an
array that can be used throughout an application while the Dim
statement declares an array that could be used only in a local
procedure.
• Declaring one dimensional Array
The general syntax to declare a one dimensional array is as follow:
Dim arrayName(subscript) as atatype
where subscript indicates the last index in the array.
Examples of one dimensional arrays:
Dim Name(3) as string
above statement declares an array that will hold 4 string values as
below
Name(0)=“Pooja”
Name(1)=“Swati”
Name(2)=“Sachin”
Name(3)=“Amol”
Dim rollno(4) as integer
above statement creates an array named as rollno that will hold 5
integer values:
rollno(0)=“1”
rollno(1)=“2”
rollno(2)=“3”
rollno(3)=“4”
rollno(4)=“5”
These are examples of fixed arrays where size of array is fixed and
specified at the time of declaration itself.
Furthermore, we can also initialize and declare an array using the
following ways, as shown below.
• Dim intData() As Integer = {1, 2, 3, 4, 5}
• Dim intData(5) As Integer
• Dim array_name() As String = {“Peter”, “John”, “Brock”, “James”, “
Maria”}
• Dim misc() as Object = {“Hello friends”, 16c, 12ui, “A”c}
• Dim Emp(0 to 2) As String
• Emp{0} = “Mathew”
• Emp(1) = “ Anthony”
• Emp(2) = “Prince”
Dynamic array can be created using empty parenthesis i.e. without
specifying the size of an array which can be further re-dimensioned
using “Redim” keyword. Redim keyword is used to redimension or
change the size of an array whenever it is required.
Syntax for ReDim statement −
Redim [preserve] arrayname(subscripts)
Where,
➢ The Preserve keyword helps to preserve the data in an existing
array, when you resize it.
➢ arrayname is the name of the array to re-dimension.
➢ subscripts specifies the new dimension.
Illustartion of dynamic array:
e.g. dim name() as string
redim name(2) as string
name(0)=“Swati”
name(1)=“Pooja”
name(2)=“Ravi”
Above array stores 3 names, suppose you want to add few more names
into an existing array that can be done as follows:
Redim preserve name(4) as string
name(3)=“Rohit”
name(4)=“Varsha”
Redim keyword in above statement will redimension the array to hold 2
more names and preserve keyword will preserve the earlier data in an
existing array when you resize it. If preserve keyword is not used then
previous data will be lost.
• When you declare an array, you need to be aware of the number of
elements created by the Dim keyword. In the Dim
arrayName(subscript) statement, subscript actually is a constant
that defines the maximum number of elements allowed. More
importantly, subs start with 0 instead of 1. Therefore, the Dim
arrangeName(10) statement creates 11 elements indexed as 0 to
11.
• There are two ways to overcome this problem, the first way is by
using the keyword Option Base 1, as shown in below:
Option Base 1
dim CusName(10) as String
Will declare an array that consists of 10 elements if the statement option
base 1 appears in the declaration area starting from CusName(1) to
CusName(10). Otherwise there will be 11 elements in the array starting
from CusName(0) to CusName(10).
CusN CusN CusN CusN CusN CusN CusN CusN CusN CusN
ame( ame( ame( ame( ame( ame( ame( ame( ame( ame(
1) 2) 3) 4) 5) 6) 7) 8) 9) 10)
• The second way is to specify the lower bound and the upper bound
of the subscript using To keyword. The syntax is
Dim arrayName(lowerbound To upperbound) As dataType
Dim Count(1 to 50) as Integer
declares an array that consists of the first element starting from Count(1)
and ends at Count(50)
1. Program to calculate sum and average of array elements:
Dim sum, avg, marks(4) As Single
Dim i As Integer
marks(0) = 45
marks(1) = 65
marks(2) = 40
marks(3) = 60
marks(4) = 78
For i = 0 To 4
sum = sum + marks(i)
Next
avg = sum / 5
TextBox1.Text = sum
TextBox2.Text = avg
2. Program to calculate sum and average of array elements:
Dim sum, avg, marks(4) As Single
Dim i As Integer
marks(0) = Val(textbox1.text)
marks(1) = Val(textbox2.text)
marks(2) = Val(textbox3.text)
marks(3) = Val(textbox4.text)
marks(4) = Val(textbox5.text)
For i = 0 To 4
sum = sum + marks(i)
Next
avg = sum / 5
TextBox6.Text = sum
TextBox7.Text = avg