Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
Advance Web Technology
Anuradha Bhatia
INTRODUCTION & IMPLEMENTATION OF VB.NET CONTENTS
I. Introduction to VB.Net 1. VB.Net Overview 2. Difference between VB and VB.Net
II. Implementation of VB.Net 1. Features 2. VB.Net IDE
3. Data Types, Loops, Control Structures, Cases, Operators 4. Creating Forms
5. Procedures and Functions
6. Form Controls
a) Error Provider
b) Combo Box
c) Month Calendar
d) Radio Button
e) Text Box
f) Check Box
g) Checked List Box
h) Date Time Picker
III. Implementation of OOP 1. Creation of Class and Objects 2. Inheritance
3. Constructors
4. Exception Handling
Advance Web Technology
Anuradha Bhatia
IV. Component Based Programming 1. Working With Private Assembly and Shared Assembly. 2. Using Com Components developed in VB or other Languages .
Advance Web Technology
Anuradha Bhatia
I. Introduction to VB.Net 1. VB.Net Overview
1. Visual Basic .NET is Microsoft's Visual Basic on their .NET framework. 2. Visual Basic is an object oriented programming language. 3. Any programmer can develop applications quickly with Visual Basic. 4. It is a very user-friendly language. 5. The pre-programmed code is referred to as the class library. 6. The programs in the class library can be combined or modified in order to suit
the needs of programmers. 7. Programs in .NET run on the CLR or the Common Language Runtime
environment. 8. Regardless of computer, as long as this environment is present, programs
developed in a .NET language will run.
2. Difference between VB and VB.Net (Question: Differentiate between VB.Net compared to VB6 = 4 Marks)
There are quite a few differences between VB6 and VB.Net are:
1. The greatest change in VB6 and VB.Net is that of the runtime environment.
2. VB6 used VB-Runtime while VB.Net uses the .Net Common Language Runtime
(.Net CLR).
3. The CLR is much better designed and implemented than the VB-Runtime.
4. The CLR uses better code translation through a Just in Time compiler.
5. The CLR Garbage Collector is more efficient than VB6 one.
6. VB6 was not a type-safe language while VB.Net is a type safe language. There is
no variant type in VB.Net and no magical type conversions happen in VB.Net
7. VB6 used 'On Error Go to' syntax to handle exceptions at runtime. VB.Net uses
try…Catching…Finally syntax to handle exceptions at runtime.
8. A lot of code (like user interface code) in VB6 was hidden from the developer. In
VB.Net no code is hidden from developer and you can access and control each
part of your application.
9. VB.Net has much better object oriented support than VB6
10. VB6 does not allow for the development of multithreaded applications. In
VB.Net you can create multithreaded applications.
11. VB6 was mainly used for desktop Windows application. In VB.Net more and
more people will develop web applications, distributed applications, create .net
and web controls and components, write windows and web services.
12. In VB.Net, reflections are used to read the meta-data of types and emit. You can
also generate code to define and invoke types at runtime.
Advance Web Technology
Anuradha Bhatia
13. VB.Net uses the .Net framework class library along with a specialized VB library
(System.VisualBasic) as its core. As a result the VB.Net is much enhanced and
useful compared to the features offered in the VB6 standard library
14. VB.Net is platform independent because of the .Net framework. Programs
written in VB.Net can run on any platform where the .Net framework is present.
The platform includes both hardware and software (operating system)
platforms.
15. VB.Net also supports language interoperability with various .Net compliant
languages. This means that you can use and enhance the code written in other
.Net compliant languages.
16. The code written in VB.Net can also be used and enhanced by other .Net
compliant languages.
17. Although VB6 also provided this functionality through COM (Component Object
Model), it was limited and difficult to use and manage.
18. VB.Net makes it easier because of the presence of the Intermediate Language
(IL) and Common Language Specification (CLS) of the .Net architecture.
19. VB6 uses COM as component architecture. VB.Net uses assemblies as its
component architecture. The Assemblies architecture has removed a lot of
problems with COM including DLL-Hell and version control.
20. Components created in VB6 (COM) need to make and update registry entries.
VB.Net does not require any registry entries, making the deployment easier.
21. VB6 used ASP to build web applications. VB.Net uses ASP.Net to build web
applications.
22. VB6 used ADODB and RecordSets to implement data access applications. VB.Net
uses ADO.Net and DataSets to build data access applications. ADO.Net also.
II. Implementation of VB.Net 1. Features
(Question: Explain the features of VB.net = 8 marks)
1. Keyword and syntax highlighting
2. Autocomplete, which helps by automatically completing the syntax as you type a
dot (.) with objects, enumerations, namespaces and when you use the new
keyword.
3. Project and solution management with solution explorer that helps to manage
applications consisting of multiple files, which is what usually happens.
4. Help building user interface with simple drag and drop over form window.
5. Properties tab that allow you to set different properties on a number of
windows and web controls.
Advance Web Technology
Anuradha Bhatia
6. Standard debugger that allows you to debug your program by putting break
points for observing run-time behavior of program.
7. Dynamic Help on a number of topics using the Microsoft Development Network
(MSDN) library. Compilation and building applications.
8. Execution of your application with/without the debugger.
2. VB.Net IDE 1. Start Microsoft Visual Studio.Net and from the menu select File > New > Project. A
"New Project" dialog will now be displayed.
2. Select "Visual Basic Project" from "Project Type" and select "Console Application"
from "Templates". Type "MyHelloWorldApplication" (without ") in the "Name" text
box below, then click OK.
This will show you the initial default code for your Hello World application.
Advance Web Technology
Anuradha Bhatia
Change the name of module from Module1 to "MyHelloWorldApplication" and
type Console.WriteLine("Hello World") inside the Sub Main() functions' body
like whats shown below:
1. To compile and execute your application, select "Start" from the "Debug" menu
or to run the application without Debug press Ctrl+F5.
2. A new console window containing the words Hello World will now be displayed.
3. Press any key to terminate the program and close the console window.
4. Let's build on this code and include some moreVB.NET syntax.
5. Modify your code as below:
3. Data Types, Loops, Control Structures, Cases, Operators Data Types
(Question: Explain the various data types in VB.Net = 4 Marks)
Any kind of variable or element can take on a data type. There are several data
types in Visual Basic. To name the most popular and the most useful are the
following.
1. String - this data type is made up of empty text, a single letter or several letters,
or a group of words.
Advance Web Technology
Anuradha Bhatia
2. Boolean - this data type is either True or False, or 1 or 0 representing the former
and the latter, respectively.
3. Byte - this data type can take on values from 0 to 255
4. Integer - this data type can take on whole number values from -32,768 to 32,767
5. Long - this data type can take on whole number values from -2,147,483,648 to
2,147,483,647
6. Single - this data type can take on floating point numbers or numbers with
decimals from -3.402823E38 to - 1.401298E(-45) and from 1.401298E(-45) to
3.402823E38
7. Double - this data type can take on floating point numbers or numbers with
decimals from – 1.79769313486231E308 to – 4.94065645841247E(–324) and
from 4.94065645841247E(–324) to 1.79769313486231E308
8. Variant - this data type can take on any kind of value. Use this data type
sparingly because it takes up more than twice the memory of the other data
types.
Control Structures Flow Control And Conditional Statements If…Then…Else statement
(Question: Explain the if- then- else structure in VB.Net = 4 Marks)
1. Condition checking has always been the most basic and important construct in
any language. 2. VB.Net provides conditional statements in the form of the If...Then...Else
statement. 3. The structure of this statement is
If Boolean expression Then
Statement or block of statement
Else
Statement or block of statement
End If
Example
Advance Web Technology
Anuradha Bhatia
Cases
Select…Case statement
(Question: Explain the select case structure in VB.Net = 4 Marks)
If you need to perform a series of specific checks, Select...Case is present in
VB.Net and is just the ticket for this.
The general structure of Select...Case statement is as follows:
Loops
(Question: Explain the various loop structure in VB.Net = 4 Marks)
For…Next Loop
1. The most common type of loop in VB.Net is the For...Next loop.
2. The basic structure of the For...Next loop is exactly the same as in VB6 and is like
so:
For...Next loop that will write integers from 1 to 10 on the console
Do While…Loop
The general structure
1. The statements under Do While will run continuously as long as the Boolean
expression evaluates to true.
2. The similar code for printing integers 1 to 10 on Console using the Do
Advance Web Technology
Anuradha Bhatia
Do…Loop While
1. A Do...Loop While is similar to a Do While...Loop, except that it does not check the
condition before entering the first iteration (execution of code inside the body of
loop).
2. The general form of the Do...Loop While is:
3. The statements under the Do will be executed first and then the Boolean condition is
checked.
4. The loop will continue until the condition remains true.
5. The code which prints integers 1 to 10 on console using Do...Loop While is
The important point is that the statements in a Do...Loop While execute at least once.
Do…Loop Until
1. A Do...Loop Until is similar to the Do...Loop While, except that it continues to
execute the containing statements until the condition against the Until part
evaluates to True or the condition against the Until remains False.
2. The general form of the Do...Loop Until is as follows:
3. The statements under the Do will execute first and then the condition is
checked.
4. The loop will continue until the condition remains false. The following code will
print integers from 1 to 10 on console using the Do...Loop Until.
Again the statements in Do...Loop Until execute at least once.
Advance Web Technology
Anuradha Bhatia
Operators
(Question: Explain the various operators in VB.Net = 4 Marks)
Arithmetic Operators
Several common arithmetic operators are allowed in VB.Net like
+ (add)
- (subtract)
* (multiply)
/ (divide)
Mod (remainder or modulo)
Assignment Operators
Assignment operators are used to assign values to variables. Common
assignment operators in VB.Net are:
= (simple assignment)
+= (additive assignment)
-= (subtractive assignment)
Relational Operators
Relational operators are used for comparison purposes in conditional
statements.
The common relational operators in VB.Net are:
= (equality check)
<> (un-equality check)
> (greater than)
< (less than)
>= (greater than or equal to)
<= (less than or equal to)
4. Creating Forms
5. Procedures and Functions (Question: Explain the concept of procedure and function in VB.Net = 4 marks)
Methods - Sub Procedures and Functions
Methods are operations that can be performed on data. A method may take
some input values through its parameters and may return a value of a particular
data type. There are two types of methods in VB.Net: Sub Procedures and
Functions.
Advance Web Technology
Anuradha Bhatia
Sub Procedure
A sub procedure is a method that does not return any values. A sub procedure in
VB.Net is defined using the Sub keyword. A sub procedure may or may not
accept parameters. A method that does not take any parameters is called a
parameter less method. For example, the following is a parameter less sub
procedure.
Functions
A function is a type of method that returns values. A function, like a sub-
procedure may or may not accept parameters. For example, the following
function calculates and returns the sum of two integers supplied as parameters.
6. Form Controls
1. Checkbox: Enables the user to select or clear the associated component.
2. Checked List box: It displays a list of items with a check box on the left of each item.
3. Combo box: It displays an editable textbox with a drop down list of permissible values.
4. Date Time Picker: It enables the user to pick a date and time and to display the date and time in a specified format.
5. Month Calendar: Displays a monthly calendar wherein the user can select a date.
6. Radio Button: It enables the user to select one option among other radio buttons.
7. Error provider: Initializes a new instance of the Error Provider class and initializes the default settings for Blink Rate, Blink Style, and the Icon.
Advance Web Technology
Anuradha Bhatia
IV. Implementation of OOP 1. Creation of Class and Objects
(Question: Explain the concept of class and object in VB.Net = 4 marks)
Class Definition
A class definition starts with the keyword Class followed by the class name; and the class body, ended by the End Class statement. Following is the general form of a class definition:
[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ] _
Class name [ ( Of typelist ) ]
[ Inherits classname ]
[ Implements interfacenames ]
[ statements ]
End Class
Where,
1. Attributelist is a list of attributes that apply to the class. Optional. 2. Accessmodifier defines the access levels of the class; it has values as - Public,
Protected, Friend, Protected Friend and Private. Optional. 3. Shadows indicate that the variable re-declares and hides an identically named
element, or set of overloaded elements, in a base class. Optional. 4. MustInherit specifies that the class can be used only as a base class and that
you cannot create an object directly from it, i.e., an abstract class. Optional. 5. NotInheritable specifies that the class cannot be used as a base class. 6. Partial indicates a partial definition of the class. 7. Inherits specifies the base class it is inheriting from. 8. Implements specifies the interfaces the class is inheriting from.
The following example demonstrates a Box class, with three data members, length, breadth and height:
Module mybox
Class Box
Public length As Double ' Length of a box
Public breadth As Double ' Breadth of a box
Public height As Double ' Height of a box
End Class
Sub Main()
Dim Box1 As Box = New Box() ' Declare Box1 of type Box
Dim Box2 As Box = New Box() ' Declare Box2 of type Box
Advance Web Technology
Anuradha Bhatia
Dim volume As Double = 0.0 ' Store the volume of a box here
' box 1 specification
Box1.height = 5.0
Box1.length = 6.0
Box1.breadth = 7.0
' box 2 specification
Box2.height = 10.0
Box2.length = 12.0
Box2.breadth = 13.0
'volume of box 1
volume = Box1.height * Box1.length * Box1.breadth
Console.WriteLine("Volume of Box1 : {0}", volume)
'volume of box 2
volume = Box2.height * Box2.length * Box2.breadth
Console.WriteLine("Volume of Box2 : {0}", volume)
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
Volume of Box1 : 210 Volume of Box2 : 1560
Objects 1. An object is the concrete realization or instance on a model specified by the
class.
2. An object is created in memory using the „new‟ keyword and is referenced by
an identifier Called a reference.
For Example
Dim mycar as new car()
OR
Dim mycar as car
mycar=New car()
Advance Web Technology
Anuradha Bhatia
2. Inheritance (Question: Explain the concept of inheritance = 4 marks)
1. VB.Net, like C#, Java and contrary to C++, allows only single class inheritance.
Multiple inheritance of classes is not allowed in VB.Net
2. The Object class defined in the System namespace is implicitly the ultimate base
class of all the classes in VB.Net (and the .Net framework)
3. Interfaces, in VB.Net, can inherit more than one interface. So, multiple
inheritances of interfaces is allowed in VB.Net (again similar to C# and Java).
4. Structures in VB.Net, can only inherit (or implement) interfaces and cannot be
inherited.
5. Inheritance allows us to define a class in terms of another class which makes it
easier to create and maintain an application.
6. This also provides an opportunity to reuse the code functionality and fast
implementation time.
7. When creating a class, instead of writing completely new data members and
member functions, the programmer can designate that the new class should
inherit the members of an existing class.
8. This existing class is called the base class, and the new class is referred to as the
derived class.
Base & Derived Classes:
1. A class can be derived from more than one class or interface, which means that
it can inherit data and functions from multiple base classes or interfaces.
2. The syntax used in VB.Net for creating derived classes is as follows:
<access-specifier> Class <base_class>
...
End Class
Class <derived_class>: Inherits <base_class>
...
End Class
Consider a base class Shape and its derived class Rectangle:
' Base class
Class Shape
Protected width As Integer
Protected height As Integer
Public Sub setWidth(ByVal w As Integer)
width = w
End Sub
Public Sub setHeight(ByVal h As Integer)
Advance Web Technology
Anuradha Bhatia
height = h
End Sub
End Class
' Derived class
Class Rectangle : Inherits Shape
Public Function getArea() As Integer
Return (width * height)
End Function
End Class
Class RectangleTester
Shared Sub Main()
Dim rect As Rectangle = New Rectangle()
rect.setWidth(5)
rect.setHeight(7)
' Print the area of the object.
Console.WriteLine("Total area: {0}", rect.getArea())
Console.ReadKey()
End Sub
End Class
When the above code is compiled and executed, it produces the following result:
Total area: 35
Base Class Initialization
1. The derived class inherits the base class member variables and member methods.
2. Therefore, the super class object should be created before the subclass is created.
The super class or the base class is implicitly known as MyBase in VB.Net
3. The following program demonstrates this:
' Base class
Class Rectangle
Protected width As Double
Protected length As Double
Public Sub New(ByVal l As Double, ByVal w As Double)
length = l
width = w
End Sub
Public Function GetArea() As Double
Return (width * length)
End Function
Advance Web Technology
Anuradha Bhatia
Public Overridable Sub Display()
Console.WriteLine("Length: {0}", length)
Console.WriteLine("Width: {0}", width)
Console.WriteLine("Area: {0}", GetArea())
End Sub
'end class Rectangle
End Class
'Derived class
Class Tabletop : Inherits Rectangle
Private cost As Double
Public Sub New(ByVal l As Double, ByVal w As Double)
MyBase.New(l, w)
End Sub
Public Function GetCost() As Double
Dim cost As Double
cost = GetArea() * 70
Return cost
End Function
Public Overrides Sub Display()
MyBase.Display()
Console.WriteLine("Cost: {0}", GetCost())
End Sub
'end class Tabletop
End Class
Class RectangleTester
Shared Sub Main()
Dim t As Tabletop = New Tabletop(4.5, 7.5)
t.Display()
Console.ReadKey()
End Sub
End Class
When the above code is compiled and executed, it produces the following result:
Length: 4.5
Width: 7.5
Area: 33.75
Cost: 2362.5
Advance Web Technology
Anuradha Bhatia
3. Constructors (Question: Explain the concept of constructor and destructor in VB.Net = 8 marks)
Constructors
1. A class constructor is a special member Sub of a class that is executed whenever we create new objects of that class.
2. A constructor has the name New and it does not have any return type.
Following program explains the concept of constructor:
Imports System Module module1 Sub main() Dim obj as new A Obj.show() Console.readline() End sub Public class a Public function show() Console.writeline(“this is base class”) End function public Sub New ( ) Console.WriteLine (“This is demo of Constructor Creation!!!!!”) End Sub End Class End module
Destructor
1. A destructor is a special member Sub of a class that is executed whenever an object of its class goes out of scope.
2. A destructor has the name Finalize and it can neither return a value nor can it take any parameters. Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories, etc.
3. Destructors cannot be inherited or overloaded.
Following example explains the concept of destructor:
Class Line Private length As Double ' Length of a line Public Sub New() 'parameterised constructor Console.WriteLine("Object is being created") End Sub Protected Overrides Sub Finalize() ' destructor Console.WriteLine("Object is being deleted") End Sub
Advance Web Technology
Anuradha Bhatia
Public Sub setLength(ByVal len As Double) length = len End Sub Public Function getLength() As Double Return length End Function Shared Sub Main() Dim line As Line = New Line() 'set line length line.setLength(6.0) Console.WriteLine("Length of line : {0}", line.getLength()) Console.ReadKey() End Sub End Class
When the above code is compiled and executed, it produces the following result:
Object is being created Length of line : 6 Object is being deleted
Methods:
(Question: Differentiate between method and event in Vb.Net.- 2 marks each = 4 marks)
1. Methods are operations that can be performed on data.
2. It may take some input values through its parameters and may return a value of
a particular data type.
3. There are two types of methods:
• Sub Procedure
4. A sub procedure is the method that does not return any values.
• Functions
5. A function-n is a type of method that returns values.
6. Sub main is the main function which is nothing but method.
Events:
1. “Events” are a special mechanism to notify client code from the class.
2. Events can be raised for a certain situation and inform client code so that
appropriate action may be taken by client’s code.
3. Very common event examples are textbox’s text changed event, button’s click
event, connection’s close event.
Advance Web Technology
Anuradha Bhatia
(Question: What is the purpose of the text property of a button and text property of a form?
Purpose Button: Two (2) Marks, Purpose Form: Two (2) Marks = 4 marks)
Ans: Purpose of text property of a button:
1) It is used to display the name of the button at run time
2) It is used to distinguish between two buttons.
Purpose of text property of form:
1) It is used to give the unique name of the form.
2) It is used to display the name of the form at run time.
4. Exception Handling (Question: explain Exception handling in Vb.Net = 4 marks)
Structured Exception Handling
1. Structured exception handling tests specific pieces of the code and, as exceptions
occur, adapts your exception-handling code to the circumstances that caused the
exception.
2. It is significantly faster in large applications than unstructured exception handling
and allows more flexible response to errors as well as greater application reliability.
3. The Try...Catch...Finally control structure is fundamental to structured exception
handling.
4. It tests a piece of code, filters exceptions created by the execution of that code, and
reacts differently based on the type of thrown exception.
The Try...Catch...Finally block
5. Try...Catch...Finally control structures test a piece of code and direct how the
application should handle various categories of error.
6. Each of the structure's three constituent parts plays a specific role in this process.
7. The Try statement provides the code that is being tested for exceptions.
8. Catch clauses identify blocks of code that are associated with specific exceptions.
9. A Catch When block directs the code to execute under specific circumstances.
10. A Catch without a When clause reacts to any exception.
11. Therefore, your code might hold a series of specific Catch...When statements, each
reacting to a specific type of exception, followed by a general Catch block that reacts
to any exceptions that have not been intercepted by the preceding Catch...When
clauses.
12. Finally statement contains code that executes regardless of whether or not an
exception occurs within the Try block.
13. A Finally statement will execute even after an Exit Try or Exit Sub.
14. This code often performs clean-up tasks, such as closing files or clearing buffers.
Advance Web Technology
Anuradha Bhatia
V. Component Based Programming 1. Working With Private Assembly and Shared Assembly.
(Question: Explain Working with Private Assembly and Shared Assembly in VB.Net = 4 Marks)
1. A private assembly is normally used by a single application, and is stored in the
application's directory, or a sub-directory beneath.
2. A shared assembly is normally stored in the global assembly cache, which is a
repository of assemblies maintained by the .NET runtime. Shared assemblies are
usually libraries of code 1
3. In private assembly all the referred DLL's will be copied into global project
debug folder but in shared assembly will not.
4. Private assemblies are faster in accessing, when compare with shared
assemblies 3
5. Memory wise shared assembly is recommended.
6. The private assembly is used when only one project need to be developed for a
client but shared assembly is used when more than one project Need to be
developed
2. Using Com Components developed in VB or other Languages.
1. A .NET component always compiles to a dynamically linked library (DLL), which
is invoked and loaded into memory at run time.
2. What has not been explained is that DLLs are always loaded into the same
process space occupied by the host application (that is, the consumer).
3. This means that Windows manages the consumer application and component as
a single process.
4. The component shares memory and other computer resources with the
consumer.
3. In-Process vs. Out-Of-Process Components
1. The .NET in-process component architecture results in very fast and reliable
communication between the consumer and component.
2. They share the same process space, Windows does not have to pass messages
between the consumer and component.
3. The consumer is able to read and write the consumer's properties very quickly.
4. Not all service providers run as in-process components.
5. Thousands of legacy applications conforming to the Component Object Model
(COM) architecture have been built over the years.
6. These service providers use an entirely different mechanism (built on
Microsoft's ActiveX specification) to communicate with their client applications.
Advance Web Technology
Anuradha Bhatia
7. A COM-based ActiveX server publishes its interface as a type library, which is
roughly equivalent to a .NET application's manifest.
8. The type library contains the definitions of classes exposed by the COM server,
including the properties, methods, and events supported by each exposed class.
4. Integrating COM Components with .NET
1. Integrating a Microsoft ActiveX® component with a .NET application is very
similar to incorporating a .NET component into the same application.
2. First, the COM component's type library is added to the .NET application's
references. Next, objects are created and instantiated from the classes exposed
by the COM component. Visual Basic .NET code then manipulates the COM
object's properties, methods, and events.
3. COM components run in their own process thread, outside of the .NET
application's process space.
4. This means that Windows independently manages the COM component's
resource requirements, and passes messages between the component and its
consumer.
5. Although the COM out-of-process integration architecture is bound to be a
somewhat slower than .NET's in-process design, most users will never notice
the difference.
6. Follow these steps to create a .NET Windows application that incorporates
Microsoft Word as a COM-based ActiveX component (Microsoft Word must be
installed on your computer in order for this example to work):
7. Open Visual Studio .NET. On the File menu, click New, and then click Project.
8. In the New Project dialog box, under Project Types, select Visual Basic
Projects. Under Templates, select Windows application.
9. Name the new Windows application project COM Component Integration and
click OK.
10. In Solution Explorer, right-click References, and on the context menu, click Add
Reference.
11. In the Add Reference dialog box, click the COM tab and locate Microsoft
Word in the list of components. (The exact version of Word is relatively
unimportant for this demonstration.)
12. Highlight Microsoft Word, click Select to add it to the Selected Components
list, and click OK.
13. .NET responds by asking you whether you would like a wrapper (described
below) generated for the Word object library. Click Yes to indicate that you
want .NET to build the wrapper for you.