Unit I
Unit I
Unit I
NET-UNIT-I
The .NET Framework 4 does not automatically use its version of the common
language runtime to run applications that are built with earlier versions of the .NET
Framework. To run older applications with .NET Framework 4, you must compile
your application with the target .NET Framework version specified in the
properties for your project in Visual Studio, or you can specify the supported
runtime with the <supported Runtime> Element in an application configuration
file.
The purpose of DLR is to enable the dynamic languages to run on the .NET
Framework and provide the advantages of .NET interoperability to dynamic
languages. New System. Dynamic namespace has been added in this version of
.NET Framework to support the DLR Functionality.
Parallel Computing
Core services, including a new API that lets you extend caching, support for
compression for session-state data, and a new application preload manager
(autostart feature).
Web Forms, including more integrated support for ASP.NET routing,
enhanced support for Web standards, updated browser support, new features
for data controls, and new features for view state management.
Web Forms controls, including a new Chart control.
MVC, including new helper methods for views, support for partitioned
MVC applications, and asynchronous controllers.
Dynamic Data, including support for existing Web applications, support for
many-to-many relationships and inheritance, new field templates and
attributes, and enhanced data filtering.
Microsoft Ajax, including additional support for client-based Ajax
applications in the Microsoft Ajax Library.
Visual Web Developer, including improved IntelliSense for JScript, new
auto-complete snippets for HTML and ASP.NET markup, and enhanced
CSS compatibility.
Deployment, including new tools for automating typical deployment tasks.
Multi-targeting, including better filtering for features that are not available in
the target version of the .NET Framework
Multiple IIS site bindings support: Allows you to have multiple base
addresses with the same protocol on the same Web site.
Routing Service: Allows you to route messages based on content.
Support for WS-Discovery: Allows you to create and search for discoverable
services.
Standard endpoints: Predefined endpoints that allow you to specify only
certain properties.
Workflow services: Integrates WCF and WF by providing activities to send
and receive messages, the ability to correlate messages based on content, and
a workflow service host.
Improved workflow activity model: The Activity class provides the base
abstraction of workflow behavior.
Rich composite activity options: Workflows benefit from new flow-control
activities that model traditional flow-control structures, such as Flowchart,
Try Catch, and Switch<T>.
Expanded built-in activity library: New features of the activity library
include new flow-control activities, activities for manipulating member data,
and activities for controlling transactions.
VB.NET Environment
You can develop Visual Basic.NET applications using Visual Studio 2010
Integrated Development Environment (IDE).Microsoft Visual Studio 2010 was
released on April 12, 2010 with .NET Framework 4.0.Visual Studio 2010 IDE is a
powerful environment which contains all the tools you need to develop Visual
Basic.NET applications efficiently.
Microsoft provides the following development tools for VB.Net programming:
Visual Studio 2010 (VS)
Visual Basic 2010 Express (VBE)
Visual Web Developer
When we create a new window application in Visual Studio 2010, Visual Studio
2010 IDE looks like as given in the diagram:
Title Bar
Title bar shows the name of the current project, such as WindowsApplication1 in
the diagram.
Menu Bar
The Menu Bar provides various menus for controlling the various
operations of Visual Studio 2010 IDE. It contains various menus like File, Edit,
View, Debug etc.
Standard Toolbar
The Standard Toolbar provides some buttons as shortcuts to some of the menu
option.
Solution Explorer
The Solution Explorer window is a key part of Visual Studio, and is
frequently used by developers. It allows you to navigate around the various files
and other items that make up your projects and your solution. Generally,
double‐clicking on a file will open that file, either in a text editor or perhaps some
kind of visual design surface. Some files can be edited in various ways. For
example, you will notice that when the project was created, the file Form1.dbl was
automatically created, added to the project, and opened in design view. But there is
another way of working with Form1.dbl, in a source code editor. To see this in
action:
In Solution Explorer, right‐click Form1.dbl and select View Code.
When working with pieces of a UI it is common to work with these two
Views of a given source file, the design view and the code view.
Right‐click Form1.dbl again and this time select View Designer.
You may have noticed that once a file (or a view of a file) is opened, it
remains open until you specifically close it. Each file is assigned a tab towards the
top of the main work area, which looks like this:
You can use these tabs to switch between the views of a file, or between
multiple files that you have opened. You’ll also notice an X to the right of each
tab, which can be used to close that file or view. You may sometimes see red dots
next the file name on a tab. This is an indication that the file has been modified and
needs to be saved. Visual Studio will automatically save files when you build or
run, and there are also various ways to manually save one or more files. The easiest
way to manually save files is to use the Ctrl+S shortcut to save the current file, or
the Ctrl+Shift+S shortcut to save all changed files.
It is easy to change the layout of the UI in Visual Studio, where most things
can be repositioned, docked, stacked in tab sets, and hidden away at will. If at any
time you can’t find Solution Explorer you can always make it visible by selecting
View > Solution Explorer from the menu, or by using the keyboard shortcut
Ctrl+Alt+L.
the Toolbox (more in a moment) and can be dragged to the visual(Form) design
surface as needed.
By the way, the same concept also applies when working with other types
of application. As well as with traditional Windows Forms applications, developers
commonly work with visual design surfaces and toolboxes when developing
Windows Presentation Foundation (WPF) applications, ASP.NET Web
applications, Silver light applications, and others.
The Toolbox
The Toolbox may not be visible right now, but if not look down the left
Hand side of the Visual Studio window and you may see a vertical tab that
Says Toolbox.
If you don’t see the Toolbox Window then select View > Toolbox
from the menu (or type Ctrl+Alt+X).
With the Toolbox visible, click on the small icon at the top right
of the Toolbox window to “pin” the window in place.
You will notice that when the button is created on the form, several white
“handles” appear around the outside of the button. These handles perform two
functions; they indicate which item is currently selected to be worked with, and
they can be used to resize the selected item.
Single‐click the body of the form (not on the button) and you will see that
the handles move to the form, which is now selected to be worked on.
Single‐click on the button again, the handles move back to the button.
As you click between these two items, look to the lower right portion of the
screen. You should see a window called Properties, and you should see the
content of the window change as you select different items on a visual design
surface.
If you can’t see the properties window, select View > Properties Window
from the menu, or use the F4 keyboard shortcut.
If necessary, pin the properties window in place.
Properties Window
The properties window is another key part of Visual Studio where, if
you’re developing a user interface at least, you are likely to spend a lot of time.
Essentially each item that you work with (buttons, textboxes, checkboxes, etc.)
have properties, sometimes hundreds of them, and the properties window allows
you to inspect and change the values of these properties, in order to achieve the
desired functionality within your application.
Notice the mini toolbar at the top of the properties window; in fact many of
Visual Studio’s windows have a similar toolbar, which generally allows you to
view the information in the window in various ways. For example, the properties
window is currently grouping the various properties of the selected button control
by type (Accessibility, Appearance, etc.) because the first button on the toolbar is
selected.
Click on the second icon to order the properties alphabetically.
Notice that the properties window also has a drop‐down list (at the top) which
shows you which item is currently selected. Another way of changing the selected
item is to drop down this list and select a different item.
Drop‐down the list at the top of the properties window and select Form1.
Notice how the selected item handles moved from the button to the
form, and how the content of the properties window changed to reflect the
properties of the newly selected item.
Object Browser
Visual Studio’s Object Browser window allows you to browse and view
the content of assemblies.
In Solution Explorer, double‐click on the System.ServiceModel
reference that you just added.
You should see the Object Explorer window open, and the System. Service
Model assembly should be selected in the list of assemblies to the left side of the
screen. Similar to the .NET tab in the add reference dialog, this list shows
assemblies that are provided by the .NET Framework as well as other Assemblies
that you may have referenced in your project. At the top of the dialog is a toolbar
with a drop‐down list which allows you to apply filters to the list of assemblies that
are displayed.
In the Browse drop‐down list, select .NET Framework 4 to filter the list
of assemblies to only those assemblies available in that version of the
.NET Framework.
Click the small triangle icon next to the System.ServiceModel assembly.
Assemblies can contain types (classes, etc.) from one or more namespaces.
When you drill into an assembly in Object Browser, the first thing you see is the
namespaces that are present in the assembly:
Above you can see that the System .Service Model assembly contains types
from multiple namespaces. Note that the types in a namespace don’t have to be all
defined in one assembly. For example, you can see that this namespace contains
some types from the System namespace, but there will be types from the same
namespace in other assemblies too.
Click the triangle icon next to the System.ServiceModel namespace to
drill into it.
Drilling into a namespace displays the types that are present in that
namespace. The nature of the type is indicated by the icon displayed next
to it. For example, in the diagram above, Action Not Supported Exception
is a class while Audit Level is an enumeration.
In the upper‐right section of the Object Browser window you can see
more detail about the type that you have selected, and again the icon next to each
item indicates the nature of the item. In the case of the Basic Http Binding class,
you will see that the class has several methods, one of which is called
CreateBindingElements (), and also several properties, including by pass Proxy on
Local.
Single‐click on the CreateBindingElements method to select it.
In the lower‐right portion of the Object Browser window you can see even
more information about the thing that you have selected above.
3. This will open a New Project dialog box in front of you as given in the
following figure:
4. You can create console, web, WCF Services, Windows and so many
applications in VB.NET. Here we are going to create a Windows Application so
select Visual Basic language and Windows Forms Application from the pane and
give it a name as “First Project” as shown in the figure given below:
6. You can place a control on the form to develop a meaningful application and
you can also rename the form.
This will open a “Save Project” dialog box in front of you as given in the following
diagram:
Now provide the name for your location and also select the location where you
want to save your project. Solution name would be same as the Name. At last click
on the Save button this will save your project at the specified location.
4) A console application does not have a form window. It only contains a code-
behind file as shown in figure given below:
5) Now add the following highlighted code to code behind file of console
application that is Module.1vb:
Module Module1
Sub Main ()
Console.WriteLine ("My First Visual Basic.NET Console Application")
Console.ReadLine ()
End Sub
End Module
6) In the above code WriteLine method of the Console class is used to display a
message on the console and the ReadLine method of the Console class is used to
read the next line of characters from the console.
7) Now press F5 key to run the application. The application will produce the
following output:
8. Long
The Long type holds integers in the range -9223372036854775808 through
9223372036854775807. In Visual Basic .NET, long is a 64-bit integer data type.
The Visual Basic .NET long data type can allocate 8 bytes memory for storage.
The underlying .NET type is System.Int64.
9. Object
The Object type is the base type from which all other types are derived. Any
type can be stored in a variable of type Object. The Visual Basic .NET Object data
type replaces the Variant in VB 6 as the universal data type. The underlying .NET
type is System. Object. It can allocate 8 bytes on 64-bit platform.
10. Short
The Short type holds integers in the range -32768 through 32767. The Short
data type can allocate 2 bytes memory storage. The underlying .NET type is
System.Int16.
11. Single
The Single type holds a 32-bit value that conforms to IEEE standard 754 for
binary floating point arithmetic. The Single type holds floating point numbers in
the range -3.40282347E38 through 3.40282347E38. The smallest nonnegative
number (other than zero) that can be held in a Double is 1.401298E-45.the single
data can be used 4 bytes memory for storage. The underlying .NET type is System.
Single.
12. String
The String type holds a sequence of Unicode characters. The underlying .NET
type is System. String. It can have the range of 0 to approximately 2 billion
Unicode characters. It can used memory for storage depending on implementing
platform
Of the fundamental types, Boolean, Byte, Char, Date, Decimal, Double,
Integer, Long, Short, and Single (that is, all of them except Object and String) are
value types. Object and String are reference types.
Where,
Each variable in the variable list has the following syntax and parts:
Where,
Some valid variable declarations along with their definition are shown here:
variable_name = value;
for example,
Dim pi As Double
pi = 3.14159
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
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):
Scope of Variable
The scope of a variable or constant is the set of all code that can refer to it
without qualifying its name. A variable's scope is determined by where the variable
is declared. It's usually a good idea to make the scope of variables or constants as
narrow as possible (block scope is the narrowest). This helps conserve memory and
minimizes the chances of your code referring to the wrong item. I'll take a look at
the different kinds of scope in VB .NET here.
1. Block Scope
Module Module1
Sub Main()
Dim intValue As Integer = 1
If intValue = 1 Then
Dim strText As String = "No worries."
System.Console.WriteLine(strText)
End If
System.Console.WriteLine(strText) 'Will not work!
End Sub
End Module
2. Procedure Scope
Note also that if an element is declared inside a procedure but outside any
block within that procedure, the element can be thought of as having block scope,
where the block is the entire procedure.
3. Module Scope
When discussing scope, Visual Basic uses the term module level to apply
equally to modules, classes, and structures. You can declare elements at this level
by placing the declaration statement outside of any procedure or block within the
module, class, or structure.
When you make a declaration at the module level, the accessibility you choose
determines the scope. The namespace that contains the module, class, or structure
also affects the scope.
Elements for which you declare Private accessibility are available for
reference to every procedure in that module, but not to any code in a different
module. The Dim statement at module level defaults to Private accessibility, so it
is equivalent to using the Private statement. However, you can make the scope and
accessibility more obvious by using Private. In this example, I've declared
Function1 as private to Module2, so it's inaccessible in Module1 (VB .NET will
tag Module2.Function1 below as a syntax error):
Module Module1
Sub Main()
System.Console.WriteLine(Module2.Function1()) 'Will not work!
End Sub
End Module
Module Module2
Private Function Function1() As String
Return "Hello from Visual Basic"
End Function
End Module
4. Namespace Scope
e.g.
Dim i as Integer
Sum = Sum +i
Next
End Sub
A variable whose value is available to all procedures within the same Form or
Module are called Form-wide or Module-wide and can be accessed from within all
procedures in a component. In some situations the entire application must access a
certain variable. Such variable must be declared as Public.
Lifetime of a Variable: It is the period for which they retain their value.
Variables declared as Public exist for the lifetime of the application. Local
variables, declared within procedures with the Dim or Private statement, live as
long as the procedure.
You can force a local variable to preserve its value between procedure calls
with the Static keyword. The advantage of using static variables is that they help
you minimize the number of total variables in the application.
Variables declared in a Form outside any procedure take effect when the Form
is loaded and cease to exist when the Form is unloaded. If the Form is loaded
again, its variables are initialized, as if it’s being loaded for the first time.
Implicit Conversion
Implicit Conversion performs automatically in VB.NET, that is the compiler is
taking care of the conversion. A Widening conversion is one that casts data from a
data type with a narrower range of possible values to a data type with a wider
range of possible values. In implicit conversion the compiler will make conversion
for us without asking. Byte -> Short -> Integer -> Long -> Decimal -> Single ->
Double is an example of data compatibility.
Complier checks for type compatibility at compilation.
Explicit Conversion
Explicit Type Conversions
In some cases we have to perform conversions that are the compiler does not
automatically convert a type to another. This type of conversion is called explicit
conversion. An explicit conversion uses a type conversion keyword. With these
conversion keywords we hav to perform the Explicit Conversion. A Narrowing
conversion is one that casts data from a wider data type to a narrower data type.
In explicit conversion we specifically ask the compiler to convert the value
into another data type.CLR checks for data compatibility at runtime. Explicit
conversion is carried out using casts. When we cast one type to another, we
deliberately force the compiler to make the transformation.
You should never expect that the cast would give you best or correct result. Casts
are potentially unsafe. Casting of big data type into small may lead to loosing of
data.
In arithmetic expression, explicit casts are done before any of the other operations.
Example:
Module DataTypes
Sub Main()
Dim n As Integer
Dim da As Date
Dim bl As Boolean = True
n = 1234567
da = Today
Console.WriteLine(bl)
Console.WriteLine(CSByte(bl))
Console.WriteLine(CStr(bl))
Console.WriteLine(CStr(da))
Console.WriteLine(CChar(CChar(CStr(n))))
Console.WriteLine(CChar(CStr(da)))
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
True
-1
True
12/4/2012
1
1
VB.Net - Constants
The constants refer to fixed values that the program may not alter during its
execution. These fixed values are also called literals.Constants can be of any of the
basic data types like an integer constant, a floating constant, a character constant,
or a string literal. The constants are treated just like regular variables except that
their values cannot be modified after their definition.
Declaring Constants
In VB.Net, constants are declared using the Const statement. The Const
statement is used at module, class, structure, procedure, or block level for use in
place of literal values.
Where,
attributelist: specifies the list of attributes applied to the constants; you can
provide multiple attributes separated by commas. Optional.
accessmodifier: specifies which code can access these constants. Optional.
Values can be either of the: Public, Protected, Friend, Protected Friend, or
Private.
Shadows: this makes the constant hide a programming element of identical
name in a base class. Optional.
Constantlist: gives the list of names of constants declared. Required.
Where, each constant name has the following syntax and parts:
For example,
Example
Module constantsNenum
Sub Main()
Const PI = 3.14149
Dim radius, area As Single
radius = 7
area = PI * radius * radius
Console.WriteLine("Area = " & Str(area))
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
Area = 153.933
Constant Description
vbCrLf Carriage return/linefeed character combination.
vbCr Carriage return character.
vbLf Linefeed character.
vbNewLine Newline character.
vbNullChar Null character.
Not the same as a zero-length string (""); used for calling
vbNullString
external procedures.
Error number. User-defined error numbers should be greater
vbObjectError than this value. For example:
Err.Raise(Number) = vbObjectError + 1000
vbTab Tab character.
vbBack Backspace character.
VB.Net - Operators
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
Arithmetic Operators
Following table shows all the arithmetic operators supported by VB.Net. Assume
variable A holds 2 and variable B holds 7, then:
Show Examples
Comparison Operators
Following table shows all the comparison operators supported by VB.Net. Assume
variable A holds 10 and variable B holds 20, then:
Show Examples
Apart from the above, VB.Net provides three more comparison operators, we give
a brief description here.
Logical/Bitwise Operators
Following table shows all the logical operators supported by VB.Net. Assume
variable A holds Boolean value True and variable B holds Boolean value False,
then:
Show Examples
Bitwise operators work on bits and perform bit-by-bit operations. The truth tables
for &, |, and ^ are as follows:
Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
~A = 1100 0011
We have seen that the Bitwise operators supported by VB.Net are And, Or, Xor
and Not. The Bit shift operators are >> and << for left shift and right shift,
respectively.
Assume that the variable A holds 60 and variable B holds 13, then:
Show Examples
Assignment Operators
There are following assignment operators supported by VB.Net:
Show Examples
Miscellaneous Operators
There are few other important operators supported by VB.Net.
Show Examples
Show Examples
Operator Precedence
Await Highest
Exponentiation (^)
Unary identity and negation (+, -)
Multiplication and floating-point division (*, /)
Integer division (\)
Modulus arithmetic (Mod)
Addition and subtraction (+, -)
Arithmetic bit shift (<<, >>)
All comparison operators (=, <>, <, <=, >, >=, Is,
IsNot, Like, TypeOf...Is)
Negation (Not)
Conjunction (And, AndAlso)
Inclusive disjunction (Or, OrElse)
Exclusive disjunction (Xor) Lowest
Evaluation of Expressions
Expression is the combination of operator and operands. When you put the
operator in between the two operant then make a expression. when you evaluate an
expression it can be evaluated by using operator Precedence The end result of an
expression represents a value, which is typically of a familiar data type such as
Boolean, String, or a numeric type.
5+4
15 * System.Math.Sqrt(9) + x
763 < 23