SlideShare a Scribd company logo
1 of 25
Download to read offline
BSc. Sem V
Paper-5S-. Net Technology and Java Programming
UNIT II
The .NET Framework and the Common Language Runtime
VB .NET is only one component of a revolution in Windows-the .NET framework. This
framework provides the new support for software development and operating system support
in Windows, and it's more extensive than anything we've seen in Windows before. The .NET
framework wraps the operating system with its own code, and your VB .NET programs actually
deal with .NET code instead of dealing with the operating system itself. And it is specially
designed to make working with the Internet easy.
At the base of the .NET framework is the Common Language Runtime (CLR). The
CLR is the module that actually runs your VB .NET applications. When you create a VB .NET
application, what really happens is that your code is compiled into the CLR's Intermediate
Language (named MSIL, or IL for short), much like bytecodes in Java.
When you run the application, that IL code is translated into the binary code your computer
can understand by some special compilers built into the CLR. Compilers translate your code
into something that your machine's hardware, or other software, can deal with directly. In this
way, Microsoft can one day create a CLR for operating systems other than Windows, and your
VB .NET applications, compiled into IL, will run on them.
The .NET Framework class library is the second major part of the .NET framework. The class
library holds an immense amount of prewritten code that all the applications you create with
Visual Basic, Visual C++, C#, and other Visual Studio languages build on.
The class library gives your program the support it needs-for example, your program may
create several forms, and as there is a class for forms in the class library, your program doesn't
have to perform all the details of creating those forms from scratch. All your code has to do is
declare a new form, and the CLR compilers can get the actual code that supports forms from
the .NET Framework class library. In this way, your programs can be very small compared to
earlier Windows applications; because you can rely on the millions of lines of code already
written in the class library, not everything has to be in your application's executable (EXE) file.
All this assumes that you're working on a machine that has the .NET framework, and therefore
the CLR and the .NET Framework class library, installed. The code for all elements we use in
a VB .NET application-forms, buttons, menus, and all the rest-all comes from the class library.
And other Visual Studio applications use the same class library, making it easy to mix
languages in your programming, even in the same application. Also, distributing applications
is easier, because all the support you need is already on the machine you're installing your
application to.
As mentioned, the .NET framework organizes its classes into namespaces. For example, the
.NET framework includes the namespaces Microsoft.VisualBasic, Microsoft.JScript,
Microsoft.CSharp, and Microsoft.Win32. In fact, these namespaces contain relatively few
classes; the real way we'll interact with the .NET framework class library in this book is through
the System namespace.
The System Namespaces
When you want to use a Windows form, for example, you must use the
System.Windows.Forms. Form class. A button in a Windows form comes from the
System.Windows. Forms.Button class, and so on. There are many such classes, organized
into various namespaces like System.Windows.Forms.
Building VB .NET Applications
To build applications in VB .NET, we have to get some terminology under our belts, because
the .NET framework requires a new structure for applications. In particular, assemblies are
now the building blocks of the .NET Framework; they form the fundamental unit of
deployment, version control, reuse, security permissions, and more.
An assembly provides the CLR with the information and compiled code it needs to know how
to run your code, much as EXE files did for Windows in VB6.
Assemblies
You combine assemblies to form .NET applications, and although we won't deal with them
directly very often, we need to get the terminology down. An assembly holds the Intermediate
Language modules for your application. When you create an application in
VB .NET and run it, VB .NET creates one or more assemblies, which are run by the CLR. That
is, assemblies are how your applications interact with the .NET framework instead of the EXE
or DLL files of VB6.
The Visual Basic Integrated Development Environment
The IDE, shown in the following Figure, has become more complex than in previous versions
of Visual Basic, and being able to use it, or at least knowing what the various parts are called.
Part of the reasons it's become more complex is that the same IDE is now shared by all Visual
Studio languages, such as VB and C#.
Visual Studio is a powerful and customizable programming environment that contains all the
tools you need to build programs quickly and efficiently. It offers a set of tools that help you
write and modify the code for your programs, and also detect and correct errors in your
programs.
Before you start learning more about VB.NET programming, it is important to understand the
development environment and identify some of the frequently using programming tools in
Visual Studio IDE.
Fig:The Visual Basic Integrated Development Environment.
1. Menu Bar
2. Standard Toolbar
3. ToolBox
4. Forms Designer
5. Output Window
6. Solution Explorer
7. Properties Window
File Menu
You can use the file menu to save or open your vb.net project, edit, find and replace text. You
can also use the View Menu to display the toolbox or solutions explorer if they are not visible.
The project menu is used to add new forms, classes or modules.
Tool bar
The tool bar contains different shortcuts for opening/saving projects as well as running your
project once it is ready for debugging.
Status Bar
It appears on the bottom of the vb.net IDE. It displays the status of the current operation. By
default, it will display a “Ready” status but this will change once your run the application.
Solutions explorer
The solutions explorer appears at the right-most part of the vb.net IDE. It displays all the objects
that you are using on your project including your forms, datasets, reports, modules, references
and different resources that you have used on your project
Tool box
The vb.net tool box displays different visual objects/tools that you can use on your form. The
most common tools include:
● Label
● Textbox
● Command Button
● Radio button
● Checkbox
● Listbox
● Combo box
● Picture box
● Date & Time Picker and
● Progress Bar
Visual Basic.NET IDE is built out of a collection of different windows. Some windows are
used for writing code, some for designing interfaces, and others for getting a general overview
of files or classes in your application.
Visual Studio organizes your work in projects and solutions. A solution can contain more than
one project, such as a DLL and an executable that references that DLL. From the following
chapters you will learn how to use these Visual Studio features for your programming needs.
Console application
Console applications are light weight programs run inside the command prompt (DOS)
window. They are commonly used for test applications. Console Applications don't have
graphical user interfaces and are run in the Command Prompt. It is text based code editor
window. Windows Forms applications do have user interfaces.
In Visual Studio 2010 you can start Console Application project as follows:
File>New Project>Console Application
Windows Application
Windows *Forms* (WinForms) is the name given to the graphical application programming
interface (API) included as a part of Microsoft .NET Framework, providing access to native
Microsoft Windows interface elements by wrapping the extant Windows API in managed code.
Windows Applications are form based standard Windows desktop applications for common
day to day tasks. Microsoft word is an example of a Windows application.
A Windows form application is an application that has a graphical user interface(GUI) like the
Visual C# IDE. A console program on the other hand is a text application. There are not fancy
controls like buttons or textboxes in a console application and they are run from the command
prompt.
In Visual Studio 2010 you can start Console Application project as follows:
File>New Project>Windows Form Application
Visual Basic Statements
A Visual Basic statement is a complete instruction. It can contain:
keywords—Words reserved for Visual Basic's use.
operators—Symbols used to perform operations, like +, which performs addition and so on.
variables—Symbolic names given to values stored in memory and declared with the Dim
keyword.
constants—The same as variables, except that constants are assigned a value that cannot then
be altered.
expressions—Combinations of terms and/or keywords that yield a value. For example, if the
variable temperature holds the value 72, then the expression temperature + 3 yields the value
75.
Each statement is one of the following:
● A declaration statement, which can name and create a variable, constant, or procedure
and can also specify a data type.
● An executable statement, which can perform an action.
The Visual Basic Keywords
As with all programming languages, Visual Basic is built using keywords. These keywords are
reserved for use by Visual Basic, and you use them to build your programs.
Table: The Visual Basic keywords.
Keyword Description
AddHandler Visual Basic .NET Statement
AddressOf Visual Basic .NET Statement
Alias Used in the Declare statement
And Boolean operator
AndAlso Boolean operator
Ansi Used in the Declare statement
Append Used as a symbolic constant in the FileOpen function
As Used in variable declaration (Dim, Friend, etc.)
Assembly Assembly-level attribute specifier
Auto Used in the Declare statement
Binary Used in the Option Compare statement
Boolean Used in variable declaration (intrinsic data type)
ByRef Used in argument lists
Byte Used in variable declaration (intrinsic data type)
ByVal Used in argument lists
Call Visual Basic .NET statement
Case Used in the Select Case construct
Catch Visual Basic .NET statement
CBool Data-conversion function
CByte Data-conversion function
CChar Data-conversion function
CDate Data-conversion function
CDec Data-conversion function
CDbl Data-conversion function
Char Used in variable declaration (intrinsic data type)
CInt Data-conversion function
Class Visual Basic .NET statement
CLng Data-conversion function
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
implementing
platform
True or False
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
platform
8 bytes on 64-bit
platform
Any type can be stored in a variable of type
Object
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
implementing
platform
0 to approximately 2 billion Unicode characters
UInteger 4 bytes 0 through 4,294,967,295 (unsigned)
ULong 8 bytes 0 through 18,446,744,073,709,551,615
(unsigned)
User-Defined Depends on
implementing
platform
Each member of the structure has a range
determined by its data type and independent of
the ranges of the other members
UShort 2 bytes 0 through 65,535 (unsigned)
Declaring Variables
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.
Unlike VB6 and earlier versions of Visual Basic, you must declare all variables before
using them by default in VB .NET. Variable Declaration in VB.Net
The Dim statement is used for variable declaration and storage allocation for one or more
variables. The Dim statement is used at module, class, structure, procedure or block level.
Syntax for variable declaration in VB.Net is :
[ < attributelist > ] [ accessmodifier ] [[ Shared ] [ Shadows ] | [ Static ]] [ ReadOnly ] Dim [
WithEvents ] variablelist
Where,
● attributelist is a list of attributes that apply to the variable. Optional.
● accessmodifier defines the access levels of the variables, it has values as - Public,
Protected, Friend, Protected Friend and Private. Optional.
● Shared declares a shared variable, which is not associated with any specific instance of
a class or structure, rather available to all the instances of the class or structure.
Optional.
● Shadows indicate that the variable re-declares and hides an identically named element,
or set of overloaded elements, in a base class. Optional.
● Static indicates that the variable will retain its value, even when the after termination
of the procedure in which it is declared. Optional.
● ReadOnly means the variable can be read, but not written. Optional.
● WithEvents specifies that the variable is used to respond to events raised by the instance
assigned to the variable. Optional.
● Variablelist provides the list of variables declared.
accessmodifier are:
Public—Gives variables public access, which means there are no restrictions on their
accessibility. You can use Public only at module, namespace, or file level (which means you
can't use it inside a procedure). Note that if you specify Public, you can omit the Dim keyword
if you want to.
Protected—Gives variables protected access, which means they are accessible only from
within their own class or from a class derived from that class. You can use Protected only at
class level (which means you can't use it inside a procedure), because you use it to declare
members of a class. Note that if you specify Protected, you can omit the Dim keyword if you
want to.
Friend—Gives variables friend access, which means they are accessible from within the
program that contains their declaration, as well as from anywhere else in the same assembly.
You can use Friend only at module, namespace, or file level (which means you can't use it
inside a procedure). Note that if you specify Friend, you can omit the Dim keyword if you
want to.
Protected Friend—Gives variables both protected and friend access, which means they can
be used by code in the same assembly, as well as by code in derived classes.
Private—Gives variables private access, which means they are accessible only from within
their declaration context (usually a class), including any nested procedures. You can use
Private only at module, namespace, or file level (which means you can't use it inside a
procedure). Note that if you specify Private, you
can omit the Dim keyword if you want to.
Each variable in the variable list has the following syntax and parts −
variablename[([ boundslist])] [ As [ New ] datatype ] [ = initializer ]
Where,
● variablename − is the name of the variable
● boundslist − optional. It provides a list of bounds of each dimension of an array variable.
● New − optional. It creates a new instance of the class when the Dim statement runs.
● datatype − Required if Option Strict is On. It specifies the data type of the variable.
● initializer − Optional if New is not specified. Expression that is evaluated and assigned
to the variable when it is created.
Some valid variable declarations along with their definition are shown here −
Dim StudentID As Integer
Dim StudentName As String
Dim Salary As Double
Dim count1, count2 As Integer
Dim status As Boolean
Dim exitButton As New System.Windows.Forms.Button
Dim lastTime, nextTime As Date
Variable Initialization in VB.Net
Variables are initialized (assigned a value) with an equal sign followed by a constant
expression. The general form of initialization is −
variable_name = value;
for example,
Dim pi As Double
pi = 3.14159
You can initialize a variable at the time of declaration as follows −
Dim StudentID As Integer = 100
Dim StudentName As String = "Bill Smith"
Example
Try the following example which makes use of various types of variables −
Module variablesNdataypes
Sub Main()
Dim a As Short
Dim b As Integer
Dim c As Double
a = 10
b = 20
c = a + b
Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c)
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the following result −
a = 10, b = 20, c = 30
Accepting Values from User
The Console class in the System namespace provides a function ReadLine for accepting input
from the user and store it into a variable. For example,
Dim message As String
message = Console.ReadLine
The following example demonstrates it −
Module variablesNdataypes
Sub Main()
Dim message As String
Console.Write("Enter message: ")
message = Console.ReadLine
Console.WriteLine()
Console.WriteLine("Your Message: {0}", message)
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the following result (assume the
user inputs Hello World) −
Enter message: Hello World
Your Message: Hello World
Example
The following example demonstrates use of some of the types −
Module DataTypes
Sub Main()
Dim b As Byte
Dim n As Integer
Dim si As Single
Dim d As Double
Dim da As Date
Dim c As Char
Dim s As String
Dim bl As Boolean
b = 1
n = 1234567
si = 0.12345678901234566
d = 0.12345678901234566
da = Today
c = "U"c
s = "Me"
If ScriptEngine = "VB" Then
bl = True
Else
bl = False
End If
If bl Then
'the oath taking
Console.Write(c & " and," & s & vbCrLf)
Console.WriteLine("declaring on the day of: {0}", da)
Console.WriteLine("We will learn VB.Net seriously")
Console.WriteLine("Lets see what happens to the floating point variables:")
Console.WriteLine("The Single: {0}, The Double: {1}", si, d)
End If
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the following result −
U and, Me
declaring on the day of: 12/4/2012 12:00:00 PM
We will learn VB.Net seriously
Lets see what happens to the floating point variables:
The Single:0.1234568, The Double: 0.123456789012346
Scope of Variable in VB.NET
The scope of a variable is where its value is kept, and can be referred to in the code. The
scope of a variable determines the accessible range of a defined variable at the time of
declaration in any block, module, and class. You can access it if the variable is in a particular
region or scope in the same block. And if the variable goes beyond the region, its scope
expires.
The following are the methods to represent the scope of a variable in VB.NET.
1. Procedure Scope
2. Module Scope
3. Public Scope
Procedure (local) scope
A local variable is a type of variable defined within a procedure scope, block, or function. It
is available with a code inside the procedure, and it can be declared using the Dim or static
statement. These variables are not accessible from outside of the local method. However, the
local variable can be easily accessed by the nested programming function in the same method.
Ex: Dim X As Integer
Local variables exist until the procedure in which they are declared is executed. Once a
procedure is executed, the values of its local variables will be lost, and the resources used by
these variables will be released. And when the block is executed again, all the local variables
are rearranged.
Module Local_scope
Sub Main()
Console.WriteLine(" Scope of local varibale within a function")
local() ' call local() function without any object reference
Console.WriteLine("press any key to exit...")
Console.ReadKey()
End Sub
Sub local()
Dim X As Integer
X = 50
Console.WriteLine(" Value of Local value X is {0}", X)
End Sub
End Module
A local variable is a variable declared inside a Sub or Function. A local variables’ scope is its
enclosing block. It can be used from the Dim statement to the last statement of the enclosing
block.
Console.WriteLine(a) ' Compile error: "a" is declared after this statement.
Dim a As Integer = 10
And here is a program that cannot be compiled because the variable value is used outside of
its scope.
Do
Dim value As Integer = Console.ReadLine()
Loop While value <> -1 ' Compile error: "value" is not accessible
' here.
The program above can be corrected into one of the following:
Dim value As Integer
Do
value = Console.ReadLine()
Loop While value <> -1
Module Scope
A variable declared inside a module sheet is called a module-level variable. The defined
module variable is visible to all procedures within that module only, but it is not available for
other module's procedures.
The Dim or private statement at the top of the first procedure declaration can be declared the
module-level variables.
It means that these variables cannot be declared inside any procedure block. Further, these
variables are useful to share information between the procedures in the same module. And one
more thing about the module-level variable is that these variables can remains existence as long
as the module is executed.
Global (Public) Scope
As the name defines, a global variable is a variable that is used to access the variables globally
in a program.
These variables can be accessed by all the procedures or modules available in a program.
To access the variables globally in a program, you need to use the friend or public keyword
with a variable in a module or class at the top of the first procedure function.
Global scope is also known as the Namespace scope.
Module Global_scope1
Public str As String = "Hello, Programmer."
Public topic As String
Public exp As Integer
Sub Main()
Console.WriteLine(" You have passed {0}", str)
Console.WriteLine(" Enter the topic name")
topic = Console.ReadLine
Console.WriteLine(" Topic Name :{0}", topic)
Console.WriteLine("How many years of experienced in {0}?", topic)
exp = Console.ReadLine
Console.WriteLine(" Your Experienced is {0} ", exp)
Console.ReadKey()
End Sub
End Module
Operators and statements:
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. VB.Net is rich in built-in operators and provides following types of commonly
used operators −
● Arithmetic Operators
● Comparison Operators
● Logical/Bitwise Operators
● Bit Shift Operators
● Assignment Operators
● Miscellaneous Operators
Visual Basic comes with plenty of built-in operators, which let you manipulate your data. For
example, adding the values in intVariable1 and intVariable2 with the addition operator, +, and
storing the result in intVariable3 with the assignment operator, =:
Module Module1
Sub Main()
Dim intVariable1 As Integer = 1234
Dim intVariable2 As Integer = 2345
Dim intVariable3 As Integer
intVariable3 = intVariable1 + intVariable2
System.Console.WriteLine(intVariable3)
End Sub
End Module
This code prints out the result of adding 1234 and 2345, which is 3579. An operator works on
operands; for example, in the expression 5 + 4, 5 is operand1,+ is the operator, and 4 is
operand2. Some operators in Visual Basic take two operands, and some take one.
There are various types of operators in Visual Basic, and I'll go over them all here. Here are
the Arithmetic operators (for example, the expression 5 + 4 yields a value of 9):
^ Exponentiation
* Multiplication
/ Division
 Integer division
Mod Modulus
+ Addition
- Subtraction
Operator Description Example
^ Raises one operand to the power of another B^A will give 49
+ Adds two operands A + B will give 9
- Subtracts second operand from the first A - B will give -5
* Multiplies both operands A * B will give
14
/ Divides one operand by another and returns a floating
point result
B / A will give
3.5
 Divides one operand by another and returns an integer
result
B  A will give 3
MOD Modulus Operator and remainder of after an integer
division
B MOD A will
give 1
These are the Assignment operators (for example, temperature = 72 stores the value
72 in the variable temperature):
= Assignment
^= Exponentiation followed by assignment
*= Multiplication followed by assignment
/= Division followed by assignment
= Integer division followed by assignment
+= Addition followed by assignment
-= Subtraction followed by assignment
&= Concatenation followed by assignment
Conditional operators
< (Less than)—True if operand1 is less than operand2
<= (Less than or equal to)—True if operand1 is less than or equal to operand2
> (Greater than)—True if operand1 is greater than operand2
>= (Greater than or equal to)—True if operand1 is greater than or equal to
operand2
= (Equal to)—True if operand1 equals operand2
<> (Not equal to)—True if operand1 is not equal to operand2
Is—True if two object references refer to the same object
Like—Performs string pattern matching
Operator Description Example
= Checks if the values of two operands are equal or not;
if yes, then condition becomes true.
(A = B) is not
true.
<> Checks if the values of two operands are equal or not;
if values are not equal, then condition becomes true.
(A <> B) is true.
> Checks if the value of left operand is greater than the
value of right operand; if yes, then condition becomes
true.
(A > B) is not
true.
< Checks if the value of left operand is less than the
value of right operand; if yes, then condition becomes
true.
(A < B) is true.
>= Checks if the value of left operand is greater than or
equal to the value of right operand; if yes, then
condition becomes true.
(A >= B) is not
true.
<= Checks if the value of left operand is less than or
equal to the value of right operand; if yes, then
condition becomes true.
(A <= B) is true.
Module Module1
Sub Main()
Dim x As Integer = 11
Dim y As Integer = 5
If (x = y) Then
Console.WriteLine("11=5 is True")
Else
Console.WriteLine(" 11=5 is False")
End If
If (x < y) Then
Console.WriteLine(" 11<5 is True")
Else
Console.WriteLine(" 11<5 is False")
End If
If (x > y) Then
Console.WriteLine(" 11>5 is True")
Else
Console.WriteLine(" 11>5 is False")
End If
x = 3
y = 7
If (x <= y) Then
Console.WriteLine(" 3<=7 is True")
End If
If (y >= x) Then
Console.WriteLine(" 7>=3 is True")
End If
Console.ReadLine()
End Sub
End Module
Apart from the above, VB.Net provides three more comparison operators, which we will be
using in forthcoming chapters; however, we give a brief description here.
● Is Operator − It compares two object reference variables and determines if two object
references refer to the same object without performing value comparisons. If object1
and object2 both refer to the exact same object instance, result is True; otherwise, result
is False.
● IsNot Operator − It also compares two object reference variables and determines if two
object references refer to different objects. If object1 and object2 both refer to the exact
same object instance, result is False; otherwise, result is True.
● Like Operator − It compares a string against a pattern.
These are the String Concatenation operators (for example, "Hi "& " there " yields the
string "Hi there".):
& String concatenation
+ String concatenation
These are the Logical/Bitwise operators:
And— Performs an And operation (for logical operations: True if both operands
are True, False otherwise; the same for bit-by-bit operations where you treat 0
as False and 1 as True).
Not— Reverses the logical value of its operand, from True to False and False to True,
or bitwise operations, turns 0 into 1 and 1 into 0.
Or— Operator performs an Or operation (for logical operations: True if either
operand is True, False otherwise; the same for bit-by-bit operations where you treat 0
as False and 1 as True).
Xor— Operator performs an exclusive-Or operation (for logical operations:
True if either operand, but not both, is True, and False otherwise; the same for
bit-by-bit operations where you treat 0 as False and 1 as True).
AndAlso— Operator A "short circuited" And operator; if the first operand is
False, the second operand is not tested.
OrElse— Operator A "short circuited" Or operator, if the first operand is True,
the second is not tested.
Operator Description Example
And It is the logical as well as bitwise AND operator. If both the operands
are true, then condition becomes true. This operator does not perform
short-circuiting, i.e., it evaluates both the expressions.
(A And B) is
False.
Or It is the logical as well as bitwise OR operator. If any of the two
operands is true, then condition becomes true. This operator does not
perform short-circuiting, i.e., it evaluates both the expressions.
(A Or B) is
True.
Not It is the logical as well as bitwise NOT operator. Use to reverses the
logical state of its operand. If a condition is true, then Logical NOT
operator will make false.
Not(A And B)
is True.
Xor It is the logical as well as bitwise Logical Exclusive OR operator. It
returns True if both expressions are True or both expressions are False;
otherwise it returns False. This operator does not perform short-
circuiting, it always evaluates both expressions and there is no short-
circuiting counterpart of this operator.
A Xor B is
True.
AndAlso It is the logical AND operator. It works only on Boolean data. It
performs short-circuiting.
(A AndAlso B)
is False.
OrElse It is the logical OR operator. It works only on Boolean data. It
performs short-circuiting.
(A OrElse B) is
True.
IsFalse It determines whether an expression is False.
IsTrue It determines whether an expression is True.
Module Module1
Sub Main()
Dim var_w As Boolean = True
Dim var_x As Boolean = True
Dim var_y As Integer = 5
Dim var_z As Integer = 20
If (var_w And var_x) Then
Console.WriteLine("var_w And var_x - is true")
End If
If (var_w Or var_x) Then
Console.WriteLine("var_w Or var_x - is true")
End If
If (var_w Xor var_x) Then
Console.WriteLine("var_w Xor var_x - is true")
End If
If (var_y And var_z) Then
Console.WriteLine("var_y And var_z - is true")
End If
If (var_y Or var_z) Then
Console.WriteLine("var_y Or var_z - is true")
End If
'Only logical operators
If (var_w AndAlso var_x) Then
Console.WriteLine("var_w AndAlso var_x - is true")
End If
If (var_w OrElse var_x) Then
Console.WriteLine("var_w OrElse var_x - is true")
End If
var_w = False
var_x = True
If (var_w And var_x) Then
Console.WriteLine("var_w And var_x - is true")
Else
Console.WriteLine("var_w And var_x - is not true")
End If
If (Not (var_w And var_x)) Then
Console.WriteLine("var_w And var_x - is true")
End If
Console.ReadLine()
End Sub
End Module
VB.Net - Statements
A statement is a complete instruction in Visual Basic programs. It may contain keywords, operators,
variables, literal values, constants and expressions.
Statements could be categorized as −
Declaration statements − these are the statements where you name a variable, constant, or procedure,
and can also specify a data type.
Executable statements − these are the statements, which initiate actions. These statements can call a
method or function, loop or branch through blocks of code or assign values or expression to a variable
or constant. In the last case, it is called an Assignment statement.
Declaration Statements
The declaration statements are used to name and define procedures, variables, properties, arrays, and
constants. When you declare a programming element, you can also define its data type, access level,
and scope.
The programming elements you may declare include variables, constants, enumerations, classes,
structures, modules, interfaces, procedures, procedure parameters, function returns, external procedure
references, operators, properties, events, and delegates.
Following are the declaration statements in VB.Net –
Sr.No Statements and Description Example
1
Dim Statement
Declares and allocates storage
space for one or more variables.
Dim number As Integer
Dim quantity As Integer = 100
Dim message As String = "Hello!"
2
Const Statement
Declares and defines one or more
constants.
Const maximum As Long = 1000
Const naturalLogBase As Object
= CDec(2.7182818284)
3
Enum Statement
Declares an enumeration and
defines the values of its members.
Enum CoffeeMugSize
Jumbo
ExtraLarge
Large
Medium
Small
End Enum
4
Class Statement
Declares the name of a class and
introduces the definition of the
variables, properties, events, and
procedures that the class
comprises.
Class Box
Public length As Double
Public breadth As Double
Public height As Double
End Class
5
Structure Statement
Declares the name of a structure
and introduces the definition of
the variables, properties, events,
and procedures that the structure
comprises.
Structure Box
Public length As Double
Public breadth As Double
Public height As Double
End Structure
6
Module Statement
Declares the name of a module
and introduces the definition of
the variables, properties, events,
and procedures that the module
comprises.
Public Module myModule
Sub Main()
Dim user As String =
InputBox("What is your name?")
MsgBox("User name is" & user)
End Sub
End Module
7
Interface Statement
Declares the name of an interface
and introduces the definitions of
the members that the interface
comprises.
Public Interface MyInterface
Sub doSomething()
End Interface
8
Function Statement
Declares the name, parameters,
and code that define a Function
procedure.
Function myFunction
(ByVal n As Integer) As Double
Return 5.87 * n
End Function
9
Sub Statement
Declares the name, parameters,
and code that define a Sub
procedure.
Sub mySub(ByVal s As String)
Return
End Sub
10
Event Statement Public Event Finished()
Declares a user-defined event.
11
Delegate Statement
Used to declare a delegate.
Delegate Function MathOperator(
ByVal x As Double,
ByVal y As Double
) As Double
Executable Statements
An executable statement performs an action. Statements calling a procedure, branching to another place
in the code, looping through several statements, or evaluating an expression are executable statements.
An assignment statement is a special case of an executable statement.

More Related Content

Similar to Unit -II Introduction to visual programming.pdf

Unit I- Introduction to .NET Framework.pdf
Unit I- Introduction to .NET Framework.pdfUnit I- Introduction to .NET Framework.pdf
Unit I- Introduction to .NET Framework.pdfUjwala Junghare
 
dotNET frameworks
dotNET frameworksdotNET frameworks
dotNET frameworksnawal saad
 
Online advertising management system
Online advertising management systemOnline advertising management system
Online advertising management systemYesu Raj
 
Online advertising management system
Online advertising management systemOnline advertising management system
Online advertising management systemYesu Raj
 
Programming basics
Programming basicsProgramming basics
Programming basicsSenri DLN
 
Dot net-interview-questions-and-answers part i
Dot net-interview-questions-and-answers part iDot net-interview-questions-and-answers part i
Dot net-interview-questions-and-answers part iRakesh Joshi
 
Dot net-interview-questions-and-answers part i
Dot net-interview-questions-and-answers part iDot net-interview-questions-and-answers part i
Dot net-interview-questions-and-answers part iRakesh Joshi
 
01. Introduction to Programming
01. Introduction to Programming01. Introduction to Programming
01. Introduction to ProgrammingIntro C# Book
 
Dot net interview_questions
Dot net interview_questionsDot net interview_questions
Dot net interview_questionsnehadhamecha
 
Dot net interview_questions
Dot net interview_questionsDot net interview_questions
Dot net interview_questionsJayesh Kheradia
 
Lecture-1&2.pdf Visual Programming C# .net framework
Lecture-1&2.pdf Visual Programming C# .net frameworkLecture-1&2.pdf Visual Programming C# .net framework
Lecture-1&2.pdf Visual Programming C# .net frameworkAbdullahNadeem78
 
Dotnet Basics Presentation
Dotnet Basics PresentationDotnet Basics Presentation
Dotnet Basics PresentationSudhakar Sharma
 
1-.NET Introduction.pptx
1-.NET Introduction.pptx1-.NET Introduction.pptx
1-.NET Introduction.pptxRKAggarwal6
 
hjksjdhksjhcksjhckjhskdjhcskjhckjdppt.pptx
hjksjdhksjhcksjhckjhskdjhcskjhckjdppt.pptxhjksjdhksjhcksjhckjhskdjhcskjhckjdppt.pptx
hjksjdhksjhcksjhckjhskdjhcskjhckjdppt.pptxEliasPetros
 

Similar to Unit -II Introduction to visual programming.pdf (20)

Unit I- Introduction to .NET Framework.pdf
Unit I- Introduction to .NET Framework.pdfUnit I- Introduction to .NET Framework.pdf
Unit I- Introduction to .NET Framework.pdf
 
dotNET frameworks
dotNET frameworksdotNET frameworks
dotNET frameworks
 
Online advertising management system
Online advertising management systemOnline advertising management system
Online advertising management system
 
Online advertising management system
Online advertising management systemOnline advertising management system
Online advertising management system
 
Dotnet basics
Dotnet basicsDotnet basics
Dotnet basics
 
As pnet
As pnetAs pnet
As pnet
 
Vb.net ide
Vb.net ideVb.net ide
Vb.net ide
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
Dot net-interview-questions-and-answers part i
Dot net-interview-questions-and-answers part iDot net-interview-questions-and-answers part i
Dot net-interview-questions-and-answers part i
 
Dot net-interview-questions-and-answers part i
Dot net-interview-questions-and-answers part iDot net-interview-questions-and-answers part i
Dot net-interview-questions-and-answers part i
 
01. Introduction to Programming
01. Introduction to Programming01. Introduction to Programming
01. Introduction to Programming
 
Vb lecture
Vb lectureVb lecture
Vb lecture
 
Dot net interview_questions
Dot net interview_questionsDot net interview_questions
Dot net interview_questions
 
Dot net interview_questions
Dot net interview_questionsDot net interview_questions
Dot net interview_questions
 
Lecture-1&2.pdf Visual Programming C# .net framework
Lecture-1&2.pdf Visual Programming C# .net frameworkLecture-1&2.pdf Visual Programming C# .net framework
Lecture-1&2.pdf Visual Programming C# .net framework
 
Dotnet Basics Presentation
Dotnet Basics PresentationDotnet Basics Presentation
Dotnet Basics Presentation
 
Visual Studio 2012 introduction
Visual Studio  2012 introductionVisual Studio  2012 introduction
Visual Studio 2012 introduction
 
1-.NET Introduction.pptx
1-.NET Introduction.pptx1-.NET Introduction.pptx
1-.NET Introduction.pptx
 
hjksjdhksjhcksjhckjhskdjhcskjhckjdppt.pptx
hjksjdhksjhcksjhckjhskdjhcskjhckjdppt.pptxhjksjdhksjhcksjhckjhskdjhcskjhckjdppt.pptx
hjksjdhksjhcksjhckjhskdjhcskjhckjdppt.pptx
 
dot net
dot netdot net
dot net
 

Recently uploaded

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 

Recently uploaded (20)

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 

Unit -II Introduction to visual programming.pdf

  • 1. BSc. Sem V Paper-5S-. Net Technology and Java Programming UNIT II The .NET Framework and the Common Language Runtime VB .NET is only one component of a revolution in Windows-the .NET framework. This framework provides the new support for software development and operating system support in Windows, and it's more extensive than anything we've seen in Windows before. The .NET framework wraps the operating system with its own code, and your VB .NET programs actually deal with .NET code instead of dealing with the operating system itself. And it is specially designed to make working with the Internet easy. At the base of the .NET framework is the Common Language Runtime (CLR). The CLR is the module that actually runs your VB .NET applications. When you create a VB .NET application, what really happens is that your code is compiled into the CLR's Intermediate Language (named MSIL, or IL for short), much like bytecodes in Java. When you run the application, that IL code is translated into the binary code your computer can understand by some special compilers built into the CLR. Compilers translate your code into something that your machine's hardware, or other software, can deal with directly. In this way, Microsoft can one day create a CLR for operating systems other than Windows, and your VB .NET applications, compiled into IL, will run on them. The .NET Framework class library is the second major part of the .NET framework. The class library holds an immense amount of prewritten code that all the applications you create with Visual Basic, Visual C++, C#, and other Visual Studio languages build on. The class library gives your program the support it needs-for example, your program may create several forms, and as there is a class for forms in the class library, your program doesn't have to perform all the details of creating those forms from scratch. All your code has to do is declare a new form, and the CLR compilers can get the actual code that supports forms from the .NET Framework class library. In this way, your programs can be very small compared to earlier Windows applications; because you can rely on the millions of lines of code already written in the class library, not everything has to be in your application's executable (EXE) file. All this assumes that you're working on a machine that has the .NET framework, and therefore the CLR and the .NET Framework class library, installed. The code for all elements we use in a VB .NET application-forms, buttons, menus, and all the rest-all comes from the class library. And other Visual Studio applications use the same class library, making it easy to mix languages in your programming, even in the same application. Also, distributing applications is easier, because all the support you need is already on the machine you're installing your application to. As mentioned, the .NET framework organizes its classes into namespaces. For example, the .NET framework includes the namespaces Microsoft.VisualBasic, Microsoft.JScript, Microsoft.CSharp, and Microsoft.Win32. In fact, these namespaces contain relatively few classes; the real way we'll interact with the .NET framework class library in this book is through the System namespace.
  • 2. The System Namespaces When you want to use a Windows form, for example, you must use the System.Windows.Forms. Form class. A button in a Windows form comes from the System.Windows. Forms.Button class, and so on. There are many such classes, organized into various namespaces like System.Windows.Forms. Building VB .NET Applications To build applications in VB .NET, we have to get some terminology under our belts, because the .NET framework requires a new structure for applications. In particular, assemblies are now the building blocks of the .NET Framework; they form the fundamental unit of deployment, version control, reuse, security permissions, and more. An assembly provides the CLR with the information and compiled code it needs to know how to run your code, much as EXE files did for Windows in VB6. Assemblies You combine assemblies to form .NET applications, and although we won't deal with them directly very often, we need to get the terminology down. An assembly holds the Intermediate Language modules for your application. When you create an application in VB .NET and run it, VB .NET creates one or more assemblies, which are run by the CLR. That is, assemblies are how your applications interact with the .NET framework instead of the EXE or DLL files of VB6. The Visual Basic Integrated Development Environment The IDE, shown in the following Figure, has become more complex than in previous versions of Visual Basic, and being able to use it, or at least knowing what the various parts are called. Part of the reasons it's become more complex is that the same IDE is now shared by all Visual Studio languages, such as VB and C#. Visual Studio is a powerful and customizable programming environment that contains all the tools you need to build programs quickly and efficiently. It offers a set of tools that help you write and modify the code for your programs, and also detect and correct errors in your programs. Before you start learning more about VB.NET programming, it is important to understand the development environment and identify some of the frequently using programming tools in Visual Studio IDE.
  • 3. Fig:The Visual Basic Integrated Development Environment. 1. Menu Bar 2. Standard Toolbar 3. ToolBox 4. Forms Designer 5. Output Window 6. Solution Explorer 7. Properties Window File Menu You can use the file menu to save or open your vb.net project, edit, find and replace text. You can also use the View Menu to display the toolbox or solutions explorer if they are not visible. The project menu is used to add new forms, classes or modules. Tool bar The tool bar contains different shortcuts for opening/saving projects as well as running your project once it is ready for debugging. Status Bar It appears on the bottom of the vb.net IDE. It displays the status of the current operation. By default, it will display a “Ready” status but this will change once your run the application. Solutions explorer The solutions explorer appears at the right-most part of the vb.net IDE. It displays all the objects that you are using on your project including your forms, datasets, reports, modules, references and different resources that you have used on your project Tool box
  • 4. The vb.net tool box displays different visual objects/tools that you can use on your form. The most common tools include: ● Label ● Textbox ● Command Button ● Radio button ● Checkbox ● Listbox ● Combo box ● Picture box ● Date & Time Picker and ● Progress Bar Visual Basic.NET IDE is built out of a collection of different windows. Some windows are used for writing code, some for designing interfaces, and others for getting a general overview of files or classes in your application. Visual Studio organizes your work in projects and solutions. A solution can contain more than one project, such as a DLL and an executable that references that DLL. From the following chapters you will learn how to use these Visual Studio features for your programming needs. Console application Console applications are light weight programs run inside the command prompt (DOS) window. They are commonly used for test applications. Console Applications don't have graphical user interfaces and are run in the Command Prompt. It is text based code editor window. Windows Forms applications do have user interfaces. In Visual Studio 2010 you can start Console Application project as follows: File>New Project>Console Application
  • 5. Windows Application Windows *Forms* (WinForms) is the name given to the graphical application programming interface (API) included as a part of Microsoft .NET Framework, providing access to native Microsoft Windows interface elements by wrapping the extant Windows API in managed code. Windows Applications are form based standard Windows desktop applications for common day to day tasks. Microsoft word is an example of a Windows application. A Windows form application is an application that has a graphical user interface(GUI) like the Visual C# IDE. A console program on the other hand is a text application. There are not fancy controls like buttons or textboxes in a console application and they are run from the command prompt. In Visual Studio 2010 you can start Console Application project as follows: File>New Project>Windows Form Application Visual Basic Statements A Visual Basic statement is a complete instruction. It can contain: keywords—Words reserved for Visual Basic's use. operators—Symbols used to perform operations, like +, which performs addition and so on. variables—Symbolic names given to values stored in memory and declared with the Dim keyword. constants—The same as variables, except that constants are assigned a value that cannot then be altered. expressions—Combinations of terms and/or keywords that yield a value. For example, if the variable temperature holds the value 72, then the expression temperature + 3 yields the value 75. Each statement is one of the following: ● A declaration statement, which can name and create a variable, constant, or procedure and can also specify a data type. ● An executable statement, which can perform an action. The Visual Basic Keywords
  • 6. As with all programming languages, Visual Basic is built using keywords. These keywords are reserved for use by Visual Basic, and you use them to build your programs. Table: The Visual Basic keywords. Keyword Description AddHandler Visual Basic .NET Statement AddressOf Visual Basic .NET Statement Alias Used in the Declare statement And Boolean operator AndAlso Boolean operator Ansi Used in the Declare statement Append Used as a symbolic constant in the FileOpen function As Used in variable declaration (Dim, Friend, etc.) Assembly Assembly-level attribute specifier Auto Used in the Declare statement Binary Used in the Option Compare statement Boolean Used in variable declaration (intrinsic data type) ByRef Used in argument lists Byte Used in variable declaration (intrinsic data type)
  • 7. ByVal Used in argument lists Call Visual Basic .NET statement Case Used in the Select Case construct Catch Visual Basic .NET statement CBool Data-conversion function CByte Data-conversion function CChar Data-conversion function CDate Data-conversion function CDec Data-conversion function CDbl Data-conversion function Char Used in variable declaration (intrinsic data type) CInt Data-conversion function
  • 8. Class Visual Basic .NET statement CLng Data-conversion function 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 implementing platform True or False 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
  • 9. 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 platform 8 bytes on 64-bit platform Any type can be stored in a variable of type Object 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
  • 10. String Depends on implementing platform 0 to approximately 2 billion Unicode characters UInteger 4 bytes 0 through 4,294,967,295 (unsigned) ULong 8 bytes 0 through 18,446,744,073,709,551,615 (unsigned) User-Defined Depends on implementing platform Each member of the structure has a range determined by its data type and independent of the ranges of the other members UShort 2 bytes 0 through 65,535 (unsigned) Declaring Variables 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. Unlike VB6 and earlier versions of Visual Basic, you must declare all variables before using them by default in VB .NET. Variable Declaration in VB.Net The Dim statement is used for variable declaration and storage allocation for one or more variables. The Dim statement is used at module, class, structure, procedure or block level. Syntax for variable declaration in VB.Net is : [ < attributelist > ] [ accessmodifier ] [[ Shared ] [ Shadows ] | [ Static ]] [ ReadOnly ] Dim [ WithEvents ] variablelist Where, ● attributelist is a list of attributes that apply to the variable. Optional. ● accessmodifier defines the access levels of the variables, it has values as - Public, Protected, Friend, Protected Friend and Private. Optional. ● Shared declares a shared variable, which is not associated with any specific instance of a class or structure, rather available to all the instances of the class or structure. Optional.
  • 11. ● Shadows indicate that the variable re-declares and hides an identically named element, or set of overloaded elements, in a base class. Optional. ● Static indicates that the variable will retain its value, even when the after termination of the procedure in which it is declared. Optional. ● ReadOnly means the variable can be read, but not written. Optional. ● WithEvents specifies that the variable is used to respond to events raised by the instance assigned to the variable. Optional. ● Variablelist provides the list of variables declared. accessmodifier are: Public—Gives variables public access, which means there are no restrictions on their accessibility. You can use Public only at module, namespace, or file level (which means you can't use it inside a procedure). Note that if you specify Public, you can omit the Dim keyword if you want to. Protected—Gives variables protected access, which means they are accessible only from within their own class or from a class derived from that class. You can use Protected only at class level (which means you can't use it inside a procedure), because you use it to declare members of a class. Note that if you specify Protected, you can omit the Dim keyword if you want to. Friend—Gives variables friend access, which means they are accessible from within the program that contains their declaration, as well as from anywhere else in the same assembly. You can use Friend only at module, namespace, or file level (which means you can't use it inside a procedure). Note that if you specify Friend, you can omit the Dim keyword if you want to. Protected Friend—Gives variables both protected and friend access, which means they can be used by code in the same assembly, as well as by code in derived classes. Private—Gives variables private access, which means they are accessible only from within their declaration context (usually a class), including any nested procedures. You can use Private only at module, namespace, or file level (which means you can't use it inside a procedure). Note that if you specify Private, you can omit the Dim keyword if you want to. Each variable in the variable list has the following syntax and parts − variablename[([ boundslist])] [ As [ New ] datatype ] [ = initializer ] Where, ● variablename − is the name of the variable ● boundslist − optional. It provides a list of bounds of each dimension of an array variable. ● New − optional. It creates a new instance of the class when the Dim statement runs. ● datatype − Required if Option Strict is On. It specifies the data type of the variable. ● initializer − Optional if New is not specified. Expression that is evaluated and assigned to the variable when it is created. Some valid variable declarations along with their definition are shown here − Dim StudentID As Integer Dim StudentName As String Dim Salary As Double
  • 12. Dim count1, count2 As Integer Dim status As Boolean Dim exitButton As New System.Windows.Forms.Button Dim lastTime, nextTime As Date Variable Initialization in VB.Net Variables are initialized (assigned a value) with an equal sign followed by a constant expression. The general form of initialization is − variable_name = value; for example, Dim pi As Double pi = 3.14159 You can initialize a variable at the time of declaration as follows − Dim StudentID As Integer = 100 Dim StudentName As String = "Bill Smith" Example Try the following example which makes use of various types of variables − Module variablesNdataypes Sub Main() Dim a As Short Dim b As Integer Dim c As Double a = 10 b = 20 c = a + b Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c) Console.ReadLine() End Sub End Module When the above code is compiled and executed, it produces the following result − a = 10, b = 20, c = 30 Accepting Values from User The Console class in the System namespace provides a function ReadLine for accepting input from the user and store it into a variable. For example, Dim message As String message = Console.ReadLine The following example demonstrates it − Module variablesNdataypes
  • 13. Sub Main() Dim message As String Console.Write("Enter message: ") message = Console.ReadLine Console.WriteLine() Console.WriteLine("Your Message: {0}", message) Console.ReadLine() End Sub End Module When the above code is compiled and executed, it produces the following result (assume the user inputs Hello World) − Enter message: Hello World Your Message: Hello World Example The following example demonstrates use of some of the types − Module DataTypes Sub Main() Dim b As Byte Dim n As Integer Dim si As Single Dim d As Double Dim da As Date Dim c As Char Dim s As String Dim bl As Boolean b = 1 n = 1234567 si = 0.12345678901234566 d = 0.12345678901234566 da = Today c = "U"c s = "Me" If ScriptEngine = "VB" Then bl = True Else bl = False End If If bl Then 'the oath taking Console.Write(c & " and," & s & vbCrLf) Console.WriteLine("declaring on the day of: {0}", da) Console.WriteLine("We will learn VB.Net seriously") Console.WriteLine("Lets see what happens to the floating point variables:") Console.WriteLine("The Single: {0}, The Double: {1}", si, d) End If Console.ReadKey()
  • 14. End Sub End Module When the above code is compiled and executed, it produces the following result − U and, Me declaring on the day of: 12/4/2012 12:00:00 PM We will learn VB.Net seriously Lets see what happens to the floating point variables: The Single:0.1234568, The Double: 0.123456789012346 Scope of Variable in VB.NET The scope of a variable is where its value is kept, and can be referred to in the code. The scope of a variable determines the accessible range of a defined variable at the time of declaration in any block, module, and class. You can access it if the variable is in a particular region or scope in the same block. And if the variable goes beyond the region, its scope expires. The following are the methods to represent the scope of a variable in VB.NET. 1. Procedure Scope 2. Module Scope 3. Public Scope Procedure (local) scope A local variable is a type of variable defined within a procedure scope, block, or function. It is available with a code inside the procedure, and it can be declared using the Dim or static statement. These variables are not accessible from outside of the local method. However, the local variable can be easily accessed by the nested programming function in the same method. Ex: Dim X As Integer Local variables exist until the procedure in which they are declared is executed. Once a procedure is executed, the values of its local variables will be lost, and the resources used by these variables will be released. And when the block is executed again, all the local variables are rearranged. Module Local_scope Sub Main() Console.WriteLine(" Scope of local varibale within a function") local() ' call local() function without any object reference Console.WriteLine("press any key to exit...") Console.ReadKey() End Sub Sub local() Dim X As Integer
  • 15. X = 50 Console.WriteLine(" Value of Local value X is {0}", X) End Sub End Module A local variable is a variable declared inside a Sub or Function. A local variables’ scope is its enclosing block. It can be used from the Dim statement to the last statement of the enclosing block. Console.WriteLine(a) ' Compile error: "a" is declared after this statement. Dim a As Integer = 10 And here is a program that cannot be compiled because the variable value is used outside of its scope. Do Dim value As Integer = Console.ReadLine() Loop While value <> -1 ' Compile error: "value" is not accessible ' here. The program above can be corrected into one of the following: Dim value As Integer Do value = Console.ReadLine() Loop While value <> -1 Module Scope A variable declared inside a module sheet is called a module-level variable. The defined module variable is visible to all procedures within that module only, but it is not available for other module's procedures. The Dim or private statement at the top of the first procedure declaration can be declared the module-level variables. It means that these variables cannot be declared inside any procedure block. Further, these variables are useful to share information between the procedures in the same module. And one more thing about the module-level variable is that these variables can remains existence as long as the module is executed. Global (Public) Scope As the name defines, a global variable is a variable that is used to access the variables globally in a program. These variables can be accessed by all the procedures or modules available in a program. To access the variables globally in a program, you need to use the friend or public keyword with a variable in a module or class at the top of the first procedure function.
  • 16. Global scope is also known as the Namespace scope. Module Global_scope1 Public str As String = "Hello, Programmer." Public topic As String Public exp As Integer Sub Main() Console.WriteLine(" You have passed {0}", str) Console.WriteLine(" Enter the topic name") topic = Console.ReadLine Console.WriteLine(" Topic Name :{0}", topic) Console.WriteLine("How many years of experienced in {0}?", topic) exp = Console.ReadLine Console.WriteLine(" Your Experienced is {0} ", exp) Console.ReadKey() End Sub End Module Operators and statements: An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. VB.Net is rich in built-in operators and provides following types of commonly used operators − ● Arithmetic Operators ● Comparison Operators ● Logical/Bitwise Operators ● Bit Shift Operators ● Assignment Operators ● Miscellaneous Operators Visual Basic comes with plenty of built-in operators, which let you manipulate your data. For example, adding the values in intVariable1 and intVariable2 with the addition operator, +, and storing the result in intVariable3 with the assignment operator, =: Module Module1 Sub Main() Dim intVariable1 As Integer = 1234 Dim intVariable2 As Integer = 2345 Dim intVariable3 As Integer intVariable3 = intVariable1 + intVariable2 System.Console.WriteLine(intVariable3) End Sub End Module
  • 17. This code prints out the result of adding 1234 and 2345, which is 3579. An operator works on operands; for example, in the expression 5 + 4, 5 is operand1,+ is the operator, and 4 is operand2. Some operators in Visual Basic take two operands, and some take one. There are various types of operators in Visual Basic, and I'll go over them all here. Here are the Arithmetic operators (for example, the expression 5 + 4 yields a value of 9): ^ Exponentiation * Multiplication / Division Integer division Mod Modulus + Addition - Subtraction Operator Description Example ^ Raises one operand to the power of another B^A will give 49 + Adds two operands A + B will give 9 - Subtracts second operand from the first A - B will give -5 * Multiplies both operands A * B will give 14 / Divides one operand by another and returns a floating point result B / A will give 3.5 Divides one operand by another and returns an integer result B A will give 3 MOD Modulus Operator and remainder of after an integer division B MOD A will give 1
  • 18. These are the Assignment operators (for example, temperature = 72 stores the value 72 in the variable temperature): = Assignment ^= Exponentiation followed by assignment *= Multiplication followed by assignment /= Division followed by assignment = Integer division followed by assignment += Addition followed by assignment -= Subtraction followed by assignment &= Concatenation followed by assignment Conditional operators < (Less than)—True if operand1 is less than operand2 <= (Less than or equal to)—True if operand1 is less than or equal to operand2 > (Greater than)—True if operand1 is greater than operand2 >= (Greater than or equal to)—True if operand1 is greater than or equal to operand2 = (Equal to)—True if operand1 equals operand2 <> (Not equal to)—True if operand1 is not equal to operand2 Is—True if two object references refer to the same object Like—Performs string pattern matching Operator Description Example = Checks if the values of two operands are equal or not; if yes, then condition becomes true. (A = B) is not true. <> Checks if the values of two operands are equal or not; if values are not equal, then condition becomes true. (A <> B) is true. > Checks if the value of left operand is greater than the value of right operand; if yes, then condition becomes true. (A > B) is not true.
  • 19. < Checks if the value of left operand is less than the value of right operand; if yes, then condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then condition becomes true. (A >= B) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand; if yes, then condition becomes true. (A <= B) is true. Module Module1 Sub Main() Dim x As Integer = 11 Dim y As Integer = 5 If (x = y) Then Console.WriteLine("11=5 is True") Else Console.WriteLine(" 11=5 is False") End If If (x < y) Then Console.WriteLine(" 11<5 is True") Else Console.WriteLine(" 11<5 is False") End If If (x > y) Then Console.WriteLine(" 11>5 is True") Else Console.WriteLine(" 11>5 is False") End If x = 3 y = 7 If (x <= y) Then Console.WriteLine(" 3<=7 is True") End If If (y >= x) Then Console.WriteLine(" 7>=3 is True") End If Console.ReadLine()
  • 20. End Sub End Module Apart from the above, VB.Net provides three more comparison operators, which we will be using in forthcoming chapters; however, we give a brief description here. ● Is Operator − It compares two object reference variables and determines if two object references refer to the same object without performing value comparisons. If object1 and object2 both refer to the exact same object instance, result is True; otherwise, result is False. ● IsNot Operator − It also compares two object reference variables and determines if two object references refer to different objects. If object1 and object2 both refer to the exact same object instance, result is False; otherwise, result is True. ● Like Operator − It compares a string against a pattern. These are the String Concatenation operators (for example, "Hi "& " there " yields the string "Hi there".): & String concatenation + String concatenation These are the Logical/Bitwise operators: And— Performs an And operation (for logical operations: True if both operands are True, False otherwise; the same for bit-by-bit operations where you treat 0 as False and 1 as True). Not— Reverses the logical value of its operand, from True to False and False to True, or bitwise operations, turns 0 into 1 and 1 into 0. Or— Operator performs an Or operation (for logical operations: True if either operand is True, False otherwise; the same for bit-by-bit operations where you treat 0 as False and 1 as True). Xor— Operator performs an exclusive-Or operation (for logical operations: True if either operand, but not both, is True, and False otherwise; the same for bit-by-bit operations where you treat 0 as False and 1 as True). AndAlso— Operator A "short circuited" And operator; if the first operand is False, the second operand is not tested. OrElse— Operator A "short circuited" Or operator, if the first operand is True, the second is not tested. Operator Description Example
  • 21. And It is the logical as well as bitwise AND operator. If both the operands are true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions. (A And B) is False. Or It is the logical as well as bitwise OR operator. If any of the two operands is true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions. (A Or B) is True. Not It is the logical as well as bitwise NOT operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. Not(A And B) is True. Xor It is the logical as well as bitwise Logical Exclusive OR operator. It returns True if both expressions are True or both expressions are False; otherwise it returns False. This operator does not perform short- circuiting, it always evaluates both expressions and there is no short- circuiting counterpart of this operator. A Xor B is True. AndAlso It is the logical AND operator. It works only on Boolean data. It performs short-circuiting. (A AndAlso B) is False. OrElse It is the logical OR operator. It works only on Boolean data. It performs short-circuiting. (A OrElse B) is True. IsFalse It determines whether an expression is False.
  • 22. IsTrue It determines whether an expression is True. Module Module1 Sub Main() Dim var_w As Boolean = True Dim var_x As Boolean = True Dim var_y As Integer = 5 Dim var_z As Integer = 20 If (var_w And var_x) Then Console.WriteLine("var_w And var_x - is true") End If If (var_w Or var_x) Then Console.WriteLine("var_w Or var_x - is true") End If If (var_w Xor var_x) Then Console.WriteLine("var_w Xor var_x - is true") End If If (var_y And var_z) Then Console.WriteLine("var_y And var_z - is true") End If If (var_y Or var_z) Then Console.WriteLine("var_y Or var_z - is true") End If 'Only logical operators If (var_w AndAlso var_x) Then Console.WriteLine("var_w AndAlso var_x - is true") End If If (var_w OrElse var_x) Then Console.WriteLine("var_w OrElse var_x - is true") End If var_w = False var_x = True If (var_w And var_x) Then Console.WriteLine("var_w And var_x - is true") Else Console.WriteLine("var_w And var_x - is not true") End If If (Not (var_w And var_x)) Then Console.WriteLine("var_w And var_x - is true") End If Console.ReadLine() End Sub End Module
  • 23. VB.Net - Statements A statement is a complete instruction in Visual Basic programs. It may contain keywords, operators, variables, literal values, constants and expressions. Statements could be categorized as − Declaration statements − these are the statements where you name a variable, constant, or procedure, and can also specify a data type. Executable statements − these are the statements, which initiate actions. These statements can call a method or function, loop or branch through blocks of code or assign values or expression to a variable or constant. In the last case, it is called an Assignment statement. Declaration Statements The declaration statements are used to name and define procedures, variables, properties, arrays, and constants. When you declare a programming element, you can also define its data type, access level, and scope. The programming elements you may declare include variables, constants, enumerations, classes, structures, modules, interfaces, procedures, procedure parameters, function returns, external procedure references, operators, properties, events, and delegates. Following are the declaration statements in VB.Net – Sr.No Statements and Description Example 1 Dim Statement Declares and allocates storage space for one or more variables. Dim number As Integer Dim quantity As Integer = 100 Dim message As String = "Hello!" 2 Const Statement Declares and defines one or more constants. Const maximum As Long = 1000 Const naturalLogBase As Object = CDec(2.7182818284) 3 Enum Statement Declares an enumeration and defines the values of its members. Enum CoffeeMugSize Jumbo ExtraLarge Large Medium Small End Enum
  • 24. 4 Class Statement Declares the name of a class and introduces the definition of the variables, properties, events, and procedures that the class comprises. Class Box Public length As Double Public breadth As Double Public height As Double End Class 5 Structure Statement Declares the name of a structure and introduces the definition of the variables, properties, events, and procedures that the structure comprises. Structure Box Public length As Double Public breadth As Double Public height As Double End Structure 6 Module Statement Declares the name of a module and introduces the definition of the variables, properties, events, and procedures that the module comprises. Public Module myModule Sub Main() Dim user As String = InputBox("What is your name?") MsgBox("User name is" & user) End Sub End Module 7 Interface Statement Declares the name of an interface and introduces the definitions of the members that the interface comprises. Public Interface MyInterface Sub doSomething() End Interface 8 Function Statement Declares the name, parameters, and code that define a Function procedure. Function myFunction (ByVal n As Integer) As Double Return 5.87 * n End Function 9 Sub Statement Declares the name, parameters, and code that define a Sub procedure. Sub mySub(ByVal s As String) Return End Sub 10 Event Statement Public Event Finished()
  • 25. Declares a user-defined event. 11 Delegate Statement Used to declare a delegate. Delegate Function MathOperator( ByVal x As Double, ByVal y As Double ) As Double Executable Statements An executable statement performs an action. Statements calling a procedure, branching to another place in the code, looping through several statements, or evaluating an expression are executable statements. An assignment statement is a special case of an executable statement.